L2Character.java 195 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936
  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 static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Queue;
  28. import java.util.Set;
  29. import java.util.concurrent.ConcurrentHashMap;
  30. import java.util.concurrent.Future;
  31. import java.util.concurrent.LinkedBlockingDeque;
  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. // set by the start of attack, in game ticks
  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.currentTimeMillis() + timeAtk;
  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. * @return True if the L2Character is attacking.
  3520. */
  3521. public final boolean isAttackingNow()
  3522. {
  3523. return _attackEndTime > System.currentTimeMillis();
  3524. }
  3525. /**
  3526. * Abort the attack of the L2Character and send Server->Client ActionFailed packet.
  3527. */
  3528. public final void abortAttack()
  3529. {
  3530. if (isAttackingNow())
  3531. {
  3532. sendPacket(ActionFailed.STATIC_PACKET);
  3533. }
  3534. }
  3535. /**
  3536. * Abort the cast of the L2Character and send Server->Client MagicSkillCanceld/ActionFailed packet.
  3537. */
  3538. public final void abortCast()
  3539. {
  3540. if (isCastingNow() || isCastingSimultaneouslyNow())
  3541. {
  3542. Future<?> future = _skillCast;
  3543. // cancels the skill hit scheduled task
  3544. if (future != null)
  3545. {
  3546. future.cancel(true);
  3547. _skillCast = null;
  3548. }
  3549. future = _skillCast2;
  3550. if (future != null)
  3551. {
  3552. future.cancel(true);
  3553. _skillCast2 = null;
  3554. }
  3555. // TODO: Handle removing spawned npc.
  3556. if (isChanneling())
  3557. {
  3558. getSkillChannelizer().stopChanneling();
  3559. }
  3560. if (_allSkillsDisabled)
  3561. {
  3562. enableAllSkills(); // this remains for forced skill use, e.g. scroll of escape
  3563. }
  3564. setIsCastingNow(false);
  3565. setIsCastingSimultaneouslyNow(false);
  3566. // safeguard for cannot be interrupt any more
  3567. _castInterruptTime = 0;
  3568. if (isPlayer())
  3569. {
  3570. getAI().notifyEvent(CtrlEvent.EVT_FINISH_CASTING); // setting back previous intention
  3571. }
  3572. broadcastPacket(new MagicSkillCanceld(getObjectId())); // broadcast packet to stop animations client-side
  3573. sendPacket(ActionFailed.STATIC_PACKET); // send an "action failed" packet to the caster
  3574. }
  3575. }
  3576. /**
  3577. * Update the position of the L2Character during a movement and return True if the movement is finished.<br>
  3578. * <B><U>Concept</U>:</B><br>
  3579. * 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>
  3580. * 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>
  3581. * 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>
  3582. * Note, that the current server position can differe from the current client position even if each movement is straight foward.<br>
  3583. * That's why, client send regularly a Client->Server ValidatePosition packet to eventually correct the gap on the server.<br>
  3584. * But, it's always the server position that is used in range calculation. At the end of the estimated movement time,<br>
  3585. * the L2Character position is automatically set to the destination position even if the movement is not finished.<br>
  3586. * <FONT COLOR=#FF0000><B><U>Caution</U>: The current Z position is obtained FROM THE CLIENT by the Client->Server ValidatePosition Packet.<br>
  3587. * But x and y positions must be calculated to avoid that players try to modify their movement speed.</B></FONT>
  3588. * @return True if the movement is finished
  3589. */
  3590. public boolean updatePosition()
  3591. {
  3592. // Get movement data
  3593. MoveData m = _move;
  3594. if (m == null)
  3595. {
  3596. return true;
  3597. }
  3598. if (!isVisible())
  3599. {
  3600. _move = null;
  3601. return true;
  3602. }
  3603. // Check if this is the first update
  3604. if (m._moveTimestamp == 0)
  3605. {
  3606. m._moveTimestamp = m._moveStartTime;
  3607. m._xAccurate = getX();
  3608. m._yAccurate = getY();
  3609. }
  3610. int gameTicks = GameTimeController.getInstance().getGameTicks();
  3611. // Check if the position has already been calculated
  3612. if (m._moveTimestamp == gameTicks)
  3613. {
  3614. return false;
  3615. }
  3616. int xPrev = getX();
  3617. int yPrev = getY();
  3618. int zPrev = getZ(); // the z coordinate may be modified by coordinate synchronizations
  3619. double dx, dy, dz;
  3620. if (Config.COORD_SYNCHRONIZE == 1)
  3621. // the only method that can modify x,y while moving (otherwise _move would/should be set null)
  3622. {
  3623. dx = m._xDestination - xPrev;
  3624. dy = m._yDestination - yPrev;
  3625. }
  3626. else
  3627. // otherwise we need saved temporary values to avoid rounding errors
  3628. {
  3629. dx = m._xDestination - m._xAccurate;
  3630. dy = m._yDestination - m._yAccurate;
  3631. }
  3632. final boolean isFloating = isFlying() || isInsideZone(ZoneId.WATER);
  3633. // Z coordinate will follow geodata or client values
  3634. if ((Config.COORD_SYNCHRONIZE == 2) && !isFloating && !m.disregardingGeodata && ((GameTimeController.getInstance().getGameTicks() % 10) == 0 // once a second to reduce possible cpu load
  3635. ) && GeoData.getInstance().hasGeo(xPrev, yPrev))
  3636. {
  3637. int geoHeight = GeoData.getInstance().getSpawnHeight(xPrev, yPrev, zPrev);
  3638. dz = m._zDestination - geoHeight;
  3639. // quite a big difference, compare to validatePosition packet
  3640. if (isPlayer() && (Math.abs(getActingPlayer().getClientZ() - geoHeight) > 200) && (Math.abs(getActingPlayer().getClientZ() - geoHeight) < 1500))
  3641. {
  3642. dz = m._zDestination - zPrev; // allow diff
  3643. }
  3644. else if (isInCombat() && (Math.abs(dz) > 200) && (((dx * dx) + (dy * dy)) < 40000)) // allow mob to climb up to pcinstance
  3645. {
  3646. dz = m._zDestination - zPrev; // climbing
  3647. }
  3648. else
  3649. {
  3650. zPrev = geoHeight;
  3651. }
  3652. }
  3653. else
  3654. {
  3655. dz = m._zDestination - zPrev;
  3656. }
  3657. double delta = (dx * dx) + (dy * dy);
  3658. if ((delta < 10000) && ((dz * dz) > 2500) // close enough, allows error between client and server geodata if it cannot be avoided
  3659. && !isFloating)
  3660. {
  3661. delta = Math.sqrt(delta);
  3662. }
  3663. else
  3664. {
  3665. delta = Math.sqrt(delta + (dz * dz));
  3666. }
  3667. double distFraction = Double.MAX_VALUE;
  3668. if (delta > 1)
  3669. {
  3670. final double distPassed = (getMoveSpeed() * (gameTicks - m._moveTimestamp)) / GameTimeController.TICKS_PER_SECOND;
  3671. distFraction = distPassed / delta;
  3672. }
  3673. // if (Config.DEVELOPER) _log.warning("Move Ticks:" + (gameTicks - m._moveTimestamp) + ", distPassed:" + distPassed + ", distFraction:" + distFraction);
  3674. if (distFraction > 1)
  3675. {
  3676. // Set the position of the L2Character to the destination
  3677. super.setXYZ(m._xDestination, m._yDestination, m._zDestination);
  3678. }
  3679. else
  3680. {
  3681. m._xAccurate += dx * distFraction;
  3682. m._yAccurate += dy * distFraction;
  3683. // Set the position of the L2Character to estimated after parcial move
  3684. super.setXYZ((int) (m._xAccurate), (int) (m._yAccurate), zPrev + (int) ((dz * distFraction) + 0.5));
  3685. }
  3686. revalidateZone(false);
  3687. // Set the timer of last position update to now
  3688. m._moveTimestamp = gameTicks;
  3689. if (distFraction > 1)
  3690. {
  3691. ThreadPoolManager.getInstance().executeAi(() ->
  3692. {
  3693. try
  3694. {
  3695. if (Config.MOVE_BASED_KNOWNLIST)
  3696. {
  3697. getKnownList().findObjects();
  3698. }
  3699. getAI().notifyEvent(CtrlEvent.EVT_ARRIVED);
  3700. }
  3701. catch (final Throwable e)
  3702. {
  3703. _log.log(Level.WARNING, "", e);
  3704. }
  3705. });
  3706. return true;
  3707. }
  3708. return false;
  3709. }
  3710. public void revalidateZone(boolean force)
  3711. {
  3712. if (getWorldRegion() == null)
  3713. {
  3714. return;
  3715. }
  3716. // This function is called too often from movement code
  3717. if (force)
  3718. {
  3719. _zoneValidateCounter = 4;
  3720. }
  3721. else
  3722. {
  3723. _zoneValidateCounter--;
  3724. if (_zoneValidateCounter < 0)
  3725. {
  3726. _zoneValidateCounter = 4;
  3727. }
  3728. else
  3729. {
  3730. return;
  3731. }
  3732. }
  3733. getWorldRegion().revalidateZones(this);
  3734. }
  3735. /**
  3736. * Stop movement of the L2Character (Called by AI Accessor only).<br>
  3737. * <B><U>Actions</U>:</B>
  3738. * <ul>
  3739. * <li>Delete movement data of the L2Character</li>
  3740. * <li>Set the current position (x,y,z), its current L2WorldRegion if necessary and its heading</li>
  3741. * <li>Remove the L2Object object from _gmList of GmListTable</li>
  3742. * <li>Remove object from _knownObjects and _knownPlayer of all surrounding L2WorldRegion L2Characters</li>
  3743. * </ul>
  3744. * <FONT COLOR=#FF0000><B><U>Caution</U>: This method DOESN'T send Server->Client packet StopMove/StopRotation</B></FONT>
  3745. * @param loc
  3746. */
  3747. public void stopMove(Location loc)
  3748. {
  3749. stopMove(loc, false);
  3750. }
  3751. public void stopMove(Location loc, boolean updateKnownObjects)
  3752. {
  3753. // Delete movement data of the L2Character
  3754. _move = null;
  3755. // if (getAI() != null)
  3756. // getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  3757. // Set the current position (x,y,z), its current L2WorldRegion if necessary and its heading
  3758. // All data are contained in a Location object
  3759. if (loc != null)
  3760. {
  3761. setXYZ(loc.getX(), loc.getY(), loc.getZ());
  3762. setHeading(loc.getHeading());
  3763. revalidateZone(true);
  3764. }
  3765. broadcastPacket(new StopMove(this));
  3766. if (Config.MOVE_BASED_KNOWNLIST && updateKnownObjects)
  3767. {
  3768. getKnownList().findObjects();
  3769. }
  3770. }
  3771. /**
  3772. * @return Returns the showSummonAnimation.
  3773. */
  3774. public boolean isShowSummonAnimation()
  3775. {
  3776. return _showSummonAnimation;
  3777. }
  3778. /**
  3779. * @param showSummonAnimation The showSummonAnimation to set.
  3780. */
  3781. public void setShowSummonAnimation(boolean showSummonAnimation)
  3782. {
  3783. _showSummonAnimation = showSummonAnimation;
  3784. }
  3785. /**
  3786. * Target a L2Object (add the target to the L2Character _target, _knownObject and L2Character to _KnownObject of the L2Object).<br>
  3787. * <B><U>Concept</U>:</B><br>
  3788. * The L2Object (including L2Character) targeted is identified in <B>_target</B> of the L2Character.<br>
  3789. * <B><U>Actions</U>:</B>
  3790. * <ul>
  3791. * <li>Set the _target of L2Character to L2Object</li>
  3792. * <li>If necessary, add L2Object to _knownObject of the L2Character</li>
  3793. * <li>If necessary, add L2Character to _KnownObject of the L2Object</li>
  3794. * <li>If object==null, cancel Attak or Cast</li>
  3795. * </ul>
  3796. * @param object L2object to target
  3797. */
  3798. public void setTarget(L2Object object)
  3799. {
  3800. if ((object != null) && !object.isVisible())
  3801. {
  3802. object = null;
  3803. }
  3804. if ((object != null) && (object != _target))
  3805. {
  3806. getKnownList().addKnownObject(object);
  3807. object.getKnownList().addKnownObject(this);
  3808. }
  3809. _target = object;
  3810. }
  3811. /**
  3812. * @return the identifier of the L2Object targeted or -1.
  3813. */
  3814. public final int getTargetId()
  3815. {
  3816. if (_target != null)
  3817. {
  3818. return _target.getObjectId();
  3819. }
  3820. return 0;
  3821. }
  3822. /**
  3823. * @return the L2Object targeted or null.
  3824. */
  3825. public final L2Object getTarget()
  3826. {
  3827. return _target;
  3828. }
  3829. /**
  3830. * Calculate movement data for a move to location action and add the L2Character to movingObjects of GameTimeController (only called by AI Accessor).<br>
  3831. * <B><U>Concept</U>:</B><br>
  3832. * 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>
  3833. * 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>
  3834. * 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>
  3835. * <B><U>Actions</U>:</B>
  3836. * <ul>
  3837. * <li>Get current position of the L2Character</li>
  3838. * <li>Calculate distance (dx,dy) between current position and destination including offset</li>
  3839. * <li>Create and Init a MoveData object</li>
  3840. * <li>Set the L2Character _move object to MoveData object</li>
  3841. * <li>Add the L2Character to movingObjects of the GameTimeController</li>
  3842. * <li>Create a task to notify the AI that L2Character arrives at a check point of the movement</li>
  3843. * </ul>
  3844. * <FONT COLOR=#FF0000><B><U>Caution</U>: This method DOESN'T send Server->Client packet MoveToPawn/CharMoveToLocation.</B></FONT><br>
  3845. * <B><U>Example of use</U>:</B>
  3846. * <ul>
  3847. * <li>AI : onIntentionMoveTo(Location), onIntentionPickUp(L2Object), onIntentionInteract(L2Object)</li>
  3848. * <li>FollowTask</li>
  3849. * </ul>
  3850. * @param x The X position of the destination
  3851. * @param y The Y position of the destination
  3852. * @param z The Y position of the destination
  3853. * @param offset The size of the interaction area of the L2Character targeted
  3854. */
  3855. public void moveToLocation(int x, int y, int z, int offset)
  3856. {
  3857. // Get the Move Speed of the L2Charcater
  3858. double speed = getMoveSpeed();
  3859. if ((speed <= 0) || isMovementDisabled())
  3860. {
  3861. return;
  3862. }
  3863. // Get current position of the L2Character
  3864. final int curX = super.getX();
  3865. final int curY = super.getY();
  3866. final int curZ = super.getZ();
  3867. // Calculate distance (dx,dy) between current position and destination
  3868. // TODO: improve Z axis move/follow support when dx,dy are small compared to dz
  3869. double dx = (x - curX);
  3870. double dy = (y - curY);
  3871. double dz = (z - curZ);
  3872. double distance = Math.sqrt((dx * dx) + (dy * dy));
  3873. final boolean verticalMovementOnly = isFlying() && (distance == 0) && (dz != 0);
  3874. if (verticalMovementOnly)
  3875. {
  3876. distance = Math.abs(dz);
  3877. }
  3878. // make water move short and use no geodata checks for swimming chars
  3879. // distance in a click can easily be over 3000
  3880. if (isInsideZone(ZoneId.WATER) && (distance > 700))
  3881. {
  3882. double divider = 700 / distance;
  3883. x = curX + (int) (divider * dx);
  3884. y = curY + (int) (divider * dy);
  3885. z = curZ + (int) (divider * dz);
  3886. dx = (x - curX);
  3887. dy = (y - curY);
  3888. dz = (z - curZ);
  3889. distance = Math.sqrt((dx * dx) + (dy * dy));
  3890. }
  3891. // Define movement angles needed
  3892. // ^
  3893. // | X (x,y)
  3894. // | /
  3895. // | /distance
  3896. // | /
  3897. // |/ angle
  3898. // X ---------->
  3899. // (curx,cury)
  3900. double cos;
  3901. double sin;
  3902. // Check if a movement offset is defined or no distance to go through
  3903. if ((offset > 0) || (distance < 1))
  3904. {
  3905. // approximation for moving closer when z coordinates are different
  3906. // TODO: handle Z axis movement better
  3907. offset -= Math.abs(dz);
  3908. if (offset < 5)
  3909. {
  3910. offset = 5;
  3911. }
  3912. // If no distance to go through, the movement is canceled
  3913. if ((distance < 1) || ((distance - offset) <= 0))
  3914. {
  3915. // Notify the AI that the L2Character is arrived at destination
  3916. getAI().notifyEvent(CtrlEvent.EVT_ARRIVED);
  3917. return;
  3918. }
  3919. // Calculate movement angles needed
  3920. sin = dy / distance;
  3921. cos = dx / distance;
  3922. distance -= (offset - 5); // due to rounding error, we have to move a bit closer to be in range
  3923. // Calculate the new destination with offset included
  3924. x = curX + (int) (distance * cos);
  3925. y = curY + (int) (distance * sin);
  3926. }
  3927. else
  3928. {
  3929. // Calculate movement angles needed
  3930. sin = dy / distance;
  3931. cos = dx / distance;
  3932. }
  3933. // Create and Init a MoveData object
  3934. MoveData m = new MoveData();
  3935. // GEODATA MOVEMENT CHECKS AND PATHFINDING
  3936. m.onGeodataPathIndex = -1; // Initialize not on geodata path
  3937. m.disregardingGeodata = false;
  3938. if (!isFlying() // flying chars not checked - even canSeeTarget doesn't work yet
  3939. && (!isInsideZone(ZoneId.WATER) || isInsideZone(ZoneId.SIEGE))) // swimming also not checked unless in siege zone - but distance is limited
  3940. {
  3941. final boolean isInVehicle = isPlayer() && (getActingPlayer().getVehicle() != null);
  3942. if (isInVehicle)
  3943. {
  3944. m.disregardingGeodata = true;
  3945. }
  3946. double originalDistance = distance;
  3947. int originalX = x;
  3948. int originalY = y;
  3949. int originalZ = z;
  3950. int gtx = (originalX - L2World.MAP_MIN_X) >> 4;
  3951. int gty = (originalY - L2World.MAP_MIN_Y) >> 4;
  3952. // Movement checks:
  3953. // when PATHFINDING > 0, for all characters except mobs returning home (could be changed later to teleport if pathfinding fails)
  3954. if (((Config.PATHFINDING > 0) && (!(isAttackable() && ((L2Attackable) this).isReturningToSpawnPoint()))) || (isPlayer() && !(isInVehicle && (distance > 1500))) || (isSummon() && !(getAI().getIntention() == AI_INTENTION_FOLLOW)) // assuming intention_follow only when following owner
  3955. || isAfraid() || (this instanceof L2RiftInvaderInstance))
  3956. {
  3957. if (isOnGeodataPath())
  3958. {
  3959. try
  3960. {
  3961. if ((gtx == _move.geoPathGtx) && (gty == _move.geoPathGty))
  3962. {
  3963. return;
  3964. }
  3965. _move.onGeodataPathIndex = -1; // Set not on geodata path
  3966. }
  3967. catch (NullPointerException e)
  3968. {
  3969. // nothing
  3970. }
  3971. }
  3972. if ((curX < L2World.MAP_MIN_X) || (curX > L2World.MAP_MAX_X) || (curY < L2World.MAP_MIN_Y) || (curY > L2World.MAP_MAX_Y))
  3973. {
  3974. // Temporary fix for character outside world region errors
  3975. _log.warning("Character " + getName() + " outside world area, in coordinates x:" + curX + " y:" + curY);
  3976. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  3977. if (isPlayer())
  3978. {
  3979. getActingPlayer().logout();
  3980. }
  3981. else if (isSummon())
  3982. {
  3983. return; // preventation when summon get out of world coords, player will not loose him, unsummon handled from pcinstance
  3984. }
  3985. else
  3986. {
  3987. onDecay();
  3988. }
  3989. return;
  3990. }
  3991. Location destiny = GeoData.getInstance().moveCheck(curX, curY, curZ, x, y, z, getInstanceId());
  3992. // location different if destination wasn't reached (or just z coord is different)
  3993. x = destiny.getX();
  3994. y = destiny.getY();
  3995. z = destiny.getZ();
  3996. dx = x - curX;
  3997. dy = y - curY;
  3998. dz = z - curZ;
  3999. distance = verticalMovementOnly ? Math.abs(dz * dz) : Math.sqrt((dx * dx) + (dy * dy));
  4000. }
  4001. // Pathfinding checks. Only when geodata setting is 2, the LoS check gives shorter result
  4002. // than the original movement was and the LoS gives a shorter distance than 2000
  4003. // This way of detecting need for pathfinding could be changed.
  4004. if ((Config.PATHFINDING > 0) && ((originalDistance - distance) > 30) && (distance < 2000) && !isAfraid())
  4005. {
  4006. // Path calculation
  4007. // Overrides previous movement check
  4008. if ((isPlayable() && !isInVehicle) || isMinion() || isInCombat())
  4009. {
  4010. m.geoPath = PathFinding.getInstance().findPath(curX, curY, curZ, originalX, originalY, originalZ, getInstanceId(), isPlayable());
  4011. if ((m.geoPath == null) || (m.geoPath.size() < 2)) // No path found
  4012. {
  4013. // * Even though there's no path found (remember geonodes aren't perfect),
  4014. // the mob is attacking and right now we set it so that the mob will go
  4015. // after target anyway, is dz is small enough.
  4016. // * With cellpathfinding this approach could be changed but would require taking
  4017. // off the geonodes and some more checks.
  4018. // * Summons will follow their masters no matter what.
  4019. // * Currently minions also must move freely since L2AttackableAI commands
  4020. // them to move along with their leader
  4021. if (isPlayer() || (!isPlayable() && !isMinion() && (Math.abs(z - curZ) > 140)) || (isSummon() && !((L2Summon) this).getFollowStatus()))
  4022. {
  4023. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  4024. return;
  4025. }
  4026. m.disregardingGeodata = true;
  4027. x = originalX;
  4028. y = originalY;
  4029. z = originalZ;
  4030. distance = originalDistance;
  4031. }
  4032. else
  4033. {
  4034. m.onGeodataPathIndex = 0; // on first segment
  4035. m.geoPathGtx = gtx;
  4036. m.geoPathGty = gty;
  4037. m.geoPathAccurateTx = originalX;
  4038. m.geoPathAccurateTy = originalY;
  4039. x = m.geoPath.get(m.onGeodataPathIndex).getX();
  4040. y = m.geoPath.get(m.onGeodataPathIndex).getY();
  4041. z = m.geoPath.get(m.onGeodataPathIndex).getZ();
  4042. // check for doors in the route
  4043. if (DoorData.getInstance().checkIfDoorsBetween(curX, curY, curZ, x, y, z, getInstanceId()))
  4044. {
  4045. m.geoPath = null;
  4046. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  4047. return;
  4048. }
  4049. for (int i = 0; i < (m.geoPath.size() - 1); i++)
  4050. {
  4051. if (DoorData.getInstance().checkIfDoorsBetween(m.geoPath.get(i), m.geoPath.get(i + 1), getInstanceId()))
  4052. {
  4053. m.geoPath = null;
  4054. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  4055. return;
  4056. }
  4057. }
  4058. dx = x - curX;
  4059. dy = y - curY;
  4060. dz = z - curZ;
  4061. distance = verticalMovementOnly ? Math.abs(dz * dz) : Math.sqrt((dx * dx) + (dy * dy));
  4062. sin = dy / distance;
  4063. cos = dx / distance;
  4064. }
  4065. }
  4066. }
  4067. // If no distance to go through, the movement is canceled
  4068. if ((distance < 1) && ((Config.PATHFINDING > 0) || isPlayable() || (this instanceof L2RiftInvaderInstance) || isAfraid()))
  4069. {
  4070. if (isSummon())
  4071. {
  4072. ((L2Summon) this).setFollowStatus(false);
  4073. }
  4074. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  4075. return;
  4076. }
  4077. }
  4078. // Apply Z distance for flying or swimming for correct timing calculations
  4079. if ((isFlying() || isInsideZone(ZoneId.WATER)) && !verticalMovementOnly)
  4080. {
  4081. distance = Math.sqrt((distance * distance) + (dz * dz));
  4082. }
  4083. // Caclulate the Nb of ticks between the current position and the destination
  4084. // One tick added for rounding reasons
  4085. int ticksToMove = 1 + (int) ((GameTimeController.TICKS_PER_SECOND * distance) / speed);
  4086. m._xDestination = x;
  4087. m._yDestination = y;
  4088. m._zDestination = z; // this is what was requested from client
  4089. // Calculate and set the heading of the L2Character
  4090. m._heading = 0; // initial value for coordinate sync
  4091. // Does not broke heading on vertical movements
  4092. if (!verticalMovementOnly)
  4093. {
  4094. setHeading(Util.calculateHeadingFrom(cos, sin));
  4095. }
  4096. m._moveStartTime = GameTimeController.getInstance().getGameTicks();
  4097. // Set the L2Character _move object to MoveData object
  4098. _move = m;
  4099. // Add the L2Character to movingObjects of the GameTimeController
  4100. // The GameTimeController manage objects movement
  4101. GameTimeController.getInstance().registerMovingObject(this);
  4102. // Create a task to notify the AI that L2Character arrives at a check point of the movement
  4103. if ((ticksToMove * GameTimeController.MILLIS_IN_TICK) > 3000)
  4104. {
  4105. ThreadPoolManager.getInstance().scheduleAi(new NotifyAITask(this, CtrlEvent.EVT_ARRIVED_REVALIDATE), 2000);
  4106. }
  4107. // the CtrlEvent.EVT_ARRIVED will be sent when the character will actually arrive
  4108. // to destination by GameTimeController
  4109. }
  4110. public boolean moveToNextRoutePoint()
  4111. {
  4112. if (!isOnGeodataPath())
  4113. {
  4114. // Cancel the move action
  4115. _move = null;
  4116. return false;
  4117. }
  4118. // Get the Move Speed of the L2Charcater
  4119. double speed = getMoveSpeed();
  4120. if ((speed <= 0) || isMovementDisabled())
  4121. {
  4122. // Cancel the move action
  4123. _move = null;
  4124. return false;
  4125. }
  4126. MoveData md = _move;
  4127. if (md == null)
  4128. {
  4129. return false;
  4130. }
  4131. // Create and Init a MoveData object
  4132. MoveData m = new MoveData();
  4133. // Update MoveData object
  4134. m.onGeodataPathIndex = md.onGeodataPathIndex + 1; // next segment
  4135. m.geoPath = md.geoPath;
  4136. m.geoPathGtx = md.geoPathGtx;
  4137. m.geoPathGty = md.geoPathGty;
  4138. m.geoPathAccurateTx = md.geoPathAccurateTx;
  4139. m.geoPathAccurateTy = md.geoPathAccurateTy;
  4140. if (md.onGeodataPathIndex == (md.geoPath.size() - 2))
  4141. {
  4142. m._xDestination = md.geoPathAccurateTx;
  4143. m._yDestination = md.geoPathAccurateTy;
  4144. m._zDestination = md.geoPath.get(m.onGeodataPathIndex).getZ();
  4145. }
  4146. else
  4147. {
  4148. m._xDestination = md.geoPath.get(m.onGeodataPathIndex).getX();
  4149. m._yDestination = md.geoPath.get(m.onGeodataPathIndex).getY();
  4150. m._zDestination = md.geoPath.get(m.onGeodataPathIndex).getZ();
  4151. }
  4152. double dx = (m._xDestination - super.getX());
  4153. double dy = (m._yDestination - super.getY());
  4154. double distance = Math.sqrt((dx * dx) + (dy * dy));
  4155. // Calculate and set the heading of the L2Character
  4156. if (distance != 0)
  4157. {
  4158. setHeading(Util.calculateHeadingFrom(getX(), getY(), m._xDestination, m._yDestination));
  4159. }
  4160. // Caclulate the Nb of ticks between the current position and the destination
  4161. // One tick added for rounding reasons
  4162. int ticksToMove = 1 + (int) ((GameTimeController.TICKS_PER_SECOND * distance) / speed);
  4163. m._heading = 0; // initial value for coordinate sync
  4164. m._moveStartTime = GameTimeController.getInstance().getGameTicks();
  4165. // Set the L2Character _move object to MoveData object
  4166. _move = m;
  4167. // Add the L2Character to movingObjects of the GameTimeController
  4168. // The GameTimeController manage objects movement
  4169. GameTimeController.getInstance().registerMovingObject(this);
  4170. // Create a task to notify the AI that L2Character arrives at a check point of the movement
  4171. if ((ticksToMove * GameTimeController.MILLIS_IN_TICK) > 3000)
  4172. {
  4173. ThreadPoolManager.getInstance().scheduleAi(new NotifyAITask(this, CtrlEvent.EVT_ARRIVED_REVALIDATE), 2000);
  4174. }
  4175. // the CtrlEvent.EVT_ARRIVED will be sent when the character will actually arrive
  4176. // to destination by GameTimeController
  4177. // Send a Server->Client packet CharMoveToLocation to the actor and all L2PcInstance in its _knownPlayers
  4178. MoveToLocation msg = new MoveToLocation(this);
  4179. broadcastPacket(msg);
  4180. return true;
  4181. }
  4182. public boolean validateMovementHeading(int heading)
  4183. {
  4184. MoveData m = _move;
  4185. if (m == null)
  4186. {
  4187. return true;
  4188. }
  4189. boolean result = true;
  4190. if (m._heading != heading)
  4191. {
  4192. result = (m._heading == 0); // initial value or false
  4193. m._heading = heading;
  4194. }
  4195. return result;
  4196. }
  4197. /**
  4198. * Check if this object is inside the given radius around the given point.
  4199. * @param loc Location of the target
  4200. * @param radius the radius around the target
  4201. * @param checkZAxis should we check Z axis also
  4202. * @param strictCheck true if (distance < radius), false if (distance <= radius)
  4203. * @return true if the L2Character is inside the radius.
  4204. */
  4205. public final boolean isInsideRadius(ILocational loc, int radius, boolean checkZAxis, boolean strictCheck)
  4206. {
  4207. return isInsideRadius(loc.getX(), loc.getY(), loc.getZ(), radius, checkZAxis, strictCheck);
  4208. }
  4209. /**
  4210. * Check if this object is inside the given radius around the given point.
  4211. * @param x X position of the target
  4212. * @param y Y position of the target
  4213. * @param z Z position of the target
  4214. * @param radius the radius around the target
  4215. * @param checkZAxis should we check Z axis also
  4216. * @param strictCheck true if (distance < radius), false if (distance <= radius)
  4217. * @return true if the L2Character is inside the radius.
  4218. */
  4219. public final boolean isInsideRadius(int x, int y, int z, int radius, boolean checkZAxis, boolean strictCheck)
  4220. {
  4221. final double distance = calculateDistance(x, y, z, checkZAxis, true);
  4222. return (strictCheck) ? (distance < (radius * radius)) : (distance <= (radius * radius));
  4223. }
  4224. /**
  4225. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4226. * @return True if arrows are available.
  4227. */
  4228. protected boolean checkAndEquipArrows()
  4229. {
  4230. return true;
  4231. }
  4232. /**
  4233. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4234. * @return True if bolts are available.
  4235. */
  4236. protected boolean checkAndEquipBolts()
  4237. {
  4238. return true;
  4239. }
  4240. /**
  4241. * Add Exp and Sp to the L2Character.<br>
  4242. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li> <li>L2PetInstance</li>
  4243. * @param addToExp
  4244. * @param addToSp
  4245. */
  4246. public void addExpAndSp(long addToExp, int addToSp)
  4247. {
  4248. // Dummy method (overridden by players and pets)
  4249. }
  4250. /**
  4251. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4252. * @return the active weapon instance (always equiped in the right hand).
  4253. */
  4254. public abstract L2ItemInstance getActiveWeaponInstance();
  4255. /**
  4256. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4257. * @return the active weapon item (always equiped in the right hand).
  4258. */
  4259. public abstract L2Weapon getActiveWeaponItem();
  4260. /**
  4261. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4262. * @return the secondary weapon instance (always equiped in the left hand).
  4263. */
  4264. public abstract L2ItemInstance getSecondaryWeaponInstance();
  4265. /**
  4266. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4267. * @return the secondary {@link L2Item} item (always equiped in the left hand).
  4268. */
  4269. public abstract L2Item getSecondaryWeaponItem();
  4270. /**
  4271. * Manage hit process (called by Hit Task).<br>
  4272. * <B><U>Actions</U>:</B>
  4273. * <ul>
  4274. * <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>
  4275. * <li>If attack isn't aborted, send a message system (critical hit, missed...) to attacker/target if they are L2PcInstance</li>
  4276. * <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>
  4277. * <li>if attack isn't aborted and hit isn't missed, manage attack or cast break of the target (calculating rate, sending message...)</li>
  4278. * </ul>
  4279. * @param target The L2Character targeted
  4280. * @param damage Nb of HP to reduce
  4281. * @param crit True if hit is critical
  4282. * @param miss True if hit is missed
  4283. * @param soulshot True if SoulShot are charged
  4284. * @param shld True if shield is efficient
  4285. */
  4286. public void onHitTimer(L2Character target, int damage, boolean crit, boolean miss, boolean soulshot, byte shld)
  4287. {
  4288. // If the attacker/target is dead or use fake death, notify the AI with EVT_CANCEL
  4289. // and send a Server->Client packet ActionFailed (if attacker is a L2PcInstance)
  4290. if ((target == null) || isAlikeDead() || (isNpc() && ((L2Npc) this).isEventMob()))
  4291. {
  4292. getAI().notifyEvent(CtrlEvent.EVT_CANCEL);
  4293. return;
  4294. }
  4295. if ((isNpc() && target.isAlikeDead()) || target.isDead() || (!getKnownList().knowsObject(target) && !isDoor()))
  4296. {
  4297. // getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE, null);
  4298. getAI().notifyEvent(CtrlEvent.EVT_CANCEL);
  4299. sendPacket(ActionFailed.STATIC_PACKET);
  4300. return;
  4301. }
  4302. if (miss)
  4303. {
  4304. // Notify target AI
  4305. if (target.hasAI())
  4306. {
  4307. target.getAI().notifyEvent(CtrlEvent.EVT_EVADED, this);
  4308. }
  4309. notifyAttackAvoid(target, false);
  4310. }
  4311. // Send message about damage/crit or miss
  4312. sendDamageMessage(target, damage, false, crit, miss);
  4313. // Check Raidboss attack
  4314. // Character will be petrified if attacking a raid that's more
  4315. // than 8 levels lower
  4316. if (target.isRaid() && target.giveRaidCurse() && !Config.RAID_DISABLE_CURSE)
  4317. {
  4318. if (getLevel() > (target.getLevel() + 8))
  4319. {
  4320. Skill skill = CommonSkill.RAID_CURSE2.getSkill();
  4321. if (skill != null)
  4322. {
  4323. abortAttack();
  4324. abortCast();
  4325. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  4326. skill.applyEffects(target, this);
  4327. }
  4328. else
  4329. {
  4330. _log.warning("Skill 4515 at level 1 is missing in DP.");
  4331. }
  4332. damage = 0; // prevents messing up drop calculation
  4333. }
  4334. }
  4335. // If L2Character target is a L2PcInstance, send a system message
  4336. if (target.isPlayer())
  4337. {
  4338. L2PcInstance enemy = target.getActingPlayer();
  4339. enemy.getAI().clientStartAutoAttack();
  4340. }
  4341. if (!miss && (damage > 0))
  4342. {
  4343. L2Weapon weapon = getActiveWeaponItem();
  4344. boolean isBow = ((weapon != null) && ((weapon.getItemType() == WeaponType.BOW) || (weapon.getItemType() == WeaponType.CROSSBOW)));
  4345. int reflectedDamage = 0;
  4346. if (!isBow && !target.isInvul()) // Do not reflect if weapon is of type bow or target is invunlerable
  4347. {
  4348. // quick fix for no drop from raid if boss attack high-level char with damage reflection
  4349. if (!target.isRaid() || (getActingPlayer() == null) || (getActingPlayer().getLevel() <= (target.getLevel() + 8)))
  4350. {
  4351. // Reduce HP of the target and calculate reflection damage to reduce HP of attacker if necessary
  4352. double reflectPercent = target.getStat().calcStat(Stats.REFLECT_DAMAGE_PERCENT, 0, null, null);
  4353. if (reflectPercent > 0)
  4354. {
  4355. reflectedDamage = (int) ((reflectPercent / 100.) * damage);
  4356. if (reflectedDamage > target.getMaxHp())
  4357. {
  4358. reflectedDamage = target.getMaxHp();
  4359. }
  4360. }
  4361. }
  4362. }
  4363. // reduce targets HP
  4364. target.reduceCurrentHp(damage, this, null);
  4365. target.notifyDamageReceived(damage, this, null, crit, false);
  4366. if (reflectedDamage > 0)
  4367. {
  4368. reduceCurrentHp(reflectedDamage, target, true, false, null);
  4369. notifyDamageReceived(reflectedDamage, target, null, crit, false);
  4370. }
  4371. if (!isBow) // Do not absorb if weapon is of type bow
  4372. {
  4373. // Absorb HP from the damage inflicted
  4374. double absorbPercent = getStat().calcStat(Stats.ABSORB_DAMAGE_PERCENT, 0, null, null);
  4375. if (absorbPercent > 0)
  4376. {
  4377. int maxCanAbsorb = (int) (getMaxRecoverableHp() - getCurrentHp());
  4378. int absorbDamage = (int) ((absorbPercent / 100.) * damage);
  4379. if (absorbDamage > maxCanAbsorb)
  4380. {
  4381. absorbDamage = maxCanAbsorb; // Can't absord more than max hp
  4382. }
  4383. if (absorbDamage > 0)
  4384. {
  4385. setCurrentHp(getCurrentHp() + absorbDamage);
  4386. }
  4387. }
  4388. // Absorb MP from the damage inflicted
  4389. absorbPercent = getStat().calcStat(Stats.ABSORB_MANA_DAMAGE_PERCENT, 0, null, null);
  4390. if (absorbPercent > 0)
  4391. {
  4392. int maxCanAbsorb = (int) (getMaxRecoverableMp() - getCurrentMp());
  4393. int absorbDamage = (int) ((absorbPercent / 100.) * damage);
  4394. if (absorbDamage > maxCanAbsorb)
  4395. {
  4396. absorbDamage = maxCanAbsorb; // Can't absord more than max hp
  4397. }
  4398. if (absorbDamage > 0)
  4399. {
  4400. setCurrentMp(getCurrentMp() + absorbDamage);
  4401. }
  4402. }
  4403. }
  4404. // Notify AI with EVT_ATTACKED
  4405. if (target.hasAI())
  4406. {
  4407. target.getAI().notifyEvent(CtrlEvent.EVT_ATTACKED, this);
  4408. }
  4409. getAI().clientStartAutoAttack();
  4410. if (isSummon())
  4411. {
  4412. L2PcInstance owner = ((L2Summon) this).getOwner();
  4413. if (owner != null)
  4414. {
  4415. owner.getAI().clientStartAutoAttack();
  4416. }
  4417. }
  4418. // Manage attack or cast break of the target (calculating rate, sending message...)
  4419. if (!target.isRaid() && Formulas.calcAtkBreak(target, damage))
  4420. {
  4421. target.breakAttack();
  4422. target.breakCast();
  4423. }
  4424. if (_triggerSkills != null)
  4425. {
  4426. for (OptionsSkillHolder holder : _triggerSkills.values())
  4427. {
  4428. if ((!crit && (holder.getSkillType() == OptionsSkillType.ATTACK)) || ((holder.getSkillType() == OptionsSkillType.CRITICAL) && crit))
  4429. {
  4430. if (Rnd.get(100) < holder.getChance())
  4431. {
  4432. makeTriggerCast(holder.getSkill(), target);
  4433. }
  4434. }
  4435. }
  4436. }
  4437. // Launch weapon onCritical Special ability effect if available
  4438. if (crit && (weapon != null))
  4439. {
  4440. weapon.castOnCriticalSkill(this, target);
  4441. }
  4442. }
  4443. // Recharge any active auto-soulshot tasks for current creature.
  4444. rechargeShots(true, false);
  4445. }
  4446. /**
  4447. * Break an attack and send Server->Client ActionFailed packet and a System Message to the L2Character.
  4448. */
  4449. public void breakAttack()
  4450. {
  4451. if (isAttackingNow())
  4452. {
  4453. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  4454. abortAttack();
  4455. if (isPlayer())
  4456. {
  4457. // Send a system message
  4458. sendPacket(SystemMessageId.ATTACK_FAILED);
  4459. }
  4460. }
  4461. }
  4462. /**
  4463. * Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character.
  4464. */
  4465. public void breakCast()
  4466. {
  4467. // damage can only cancel magical & static skills
  4468. if (isCastingNow() && canAbortCast() && (getLastSkillCast() != null) && (getLastSkillCast().isMagic() || getLastSkillCast().isStatic()))
  4469. {
  4470. // Abort the cast of the L2Character and send Server->Client MagicSkillCanceld/ActionFailed packet.
  4471. abortCast();
  4472. if (isPlayer())
  4473. {
  4474. // Send a system message
  4475. sendPacket(SystemMessageId.CASTING_INTERRUPTED);
  4476. }
  4477. }
  4478. }
  4479. /**
  4480. * Reduce the arrow number of the L2Character.<br>
  4481. * <B><U> Overridden in </U> :</B> <li>L2PcInstance</li>
  4482. * @param bolts
  4483. */
  4484. protected void reduceArrowCount(boolean bolts)
  4485. {
  4486. // default is to do nothing
  4487. }
  4488. /**
  4489. * Manage Forced attack (shift + select target).<br>
  4490. * <B><U>Actions</U>:</B>
  4491. * <ul>
  4492. * <li>If L2Character or target is in a town area, send a system message TARGET_IN_PEACEZONE a Server->Client packet ActionFailed</li>
  4493. * <li>If target is confused, send a Server->Client packet ActionFailed</li>
  4494. * <li>If L2Character is a L2ArtefactInstance, send a Server->Client packet ActionFailed</li>
  4495. * <li>Send a Server->Client packet MyTargetSelected to start attack and Notify AI with AI_INTENTION_ATTACK</li>
  4496. * </ul>
  4497. * @param player The L2PcInstance to attack
  4498. */
  4499. @Override
  4500. public void onForcedAttack(L2PcInstance player)
  4501. {
  4502. if (isInsidePeaceZone(player))
  4503. {
  4504. // If L2Character or target is in a peace zone, send a system message TARGET_IN_PEACEZONE a Server->Client packet ActionFailed
  4505. player.sendPacket(SystemMessageId.TARGET_IN_PEACEZONE);
  4506. player.sendPacket(ActionFailed.STATIC_PACKET);
  4507. return;
  4508. }
  4509. if (player.isInOlympiadMode() && (player.getTarget() != null) && player.getTarget().isPlayable())
  4510. {
  4511. L2PcInstance target = null;
  4512. L2Object object = player.getTarget();
  4513. if ((object != null) && object.isPlayable())
  4514. {
  4515. target = object.getActingPlayer();
  4516. }
  4517. if ((target == null) || (target.isInOlympiadMode() && (!player.isOlympiadStart() || (player.getOlympiadGameId() != target.getOlympiadGameId()))))
  4518. {
  4519. // if L2PcInstance is in Olympia and the match isn't already start, send a Server->Client packet ActionFailed
  4520. player.sendPacket(ActionFailed.STATIC_PACKET);
  4521. return;
  4522. }
  4523. }
  4524. if ((player.getTarget() != null) && !player.getTarget().canBeAttacked() && !player.getAccessLevel().allowPeaceAttack())
  4525. {
  4526. // If target is not attackable, send a Server->Client packet ActionFailed
  4527. player.sendPacket(ActionFailed.STATIC_PACKET);
  4528. return;
  4529. }
  4530. if (player.isConfused())
  4531. {
  4532. // If target is confused, send a Server->Client packet ActionFailed
  4533. player.sendPacket(ActionFailed.STATIC_PACKET);
  4534. return;
  4535. }
  4536. // GeoData Los Check or dz > 1000
  4537. if (!GeoData.getInstance().canSeeTarget(player, this))
  4538. {
  4539. player.sendPacket(SystemMessageId.CANT_SEE_TARGET);
  4540. player.sendPacket(ActionFailed.STATIC_PACKET);
  4541. return;
  4542. }
  4543. if (player.getBlockCheckerArena() != -1)
  4544. {
  4545. player.sendPacket(ActionFailed.STATIC_PACKET);
  4546. return;
  4547. }
  4548. // Notify AI with AI_INTENTION_ATTACK
  4549. player.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, this);
  4550. }
  4551. /**
  4552. * @param attacker
  4553. * @return True if inside peace zone.
  4554. */
  4555. public boolean isInsidePeaceZone(L2PcInstance attacker)
  4556. {
  4557. return isInsidePeaceZone(attacker, this);
  4558. }
  4559. public boolean isInsidePeaceZone(L2PcInstance attacker, L2Object target)
  4560. {
  4561. return (!attacker.getAccessLevel().allowPeaceAttack() && isInsidePeaceZone((L2Object) attacker, target));
  4562. }
  4563. public boolean isInsidePeaceZone(L2Object attacker, L2Object target)
  4564. {
  4565. if (target == null)
  4566. {
  4567. return false;
  4568. }
  4569. if (!(target.isPlayable() && attacker.isPlayable()))
  4570. {
  4571. return false;
  4572. }
  4573. if (InstanceManager.getInstance().getInstance(getInstanceId()).isPvPInstance())
  4574. {
  4575. return false;
  4576. }
  4577. if (TerritoryWarManager.PLAYER_WITH_WARD_CAN_BE_KILLED_IN_PEACEZONE && TerritoryWarManager.getInstance().isTWInProgress())
  4578. {
  4579. if (target.isPlayer() && target.getActingPlayer().isCombatFlagEquipped())
  4580. {
  4581. return false;
  4582. }
  4583. }
  4584. if (Config.ALT_GAME_KARMA_PLAYER_CAN_BE_KILLED_IN_PEACEZONE)
  4585. {
  4586. // allows red to be attacked and red to attack flagged players
  4587. if ((target.getActingPlayer() != null) && (target.getActingPlayer().getKarma() > 0))
  4588. {
  4589. return false;
  4590. }
  4591. if ((attacker.getActingPlayer() != null) && (attacker.getActingPlayer().getKarma() > 0) && (target.getActingPlayer() != null) && (target.getActingPlayer().getPvpFlag() > 0))
  4592. {
  4593. return false;
  4594. }
  4595. }
  4596. return (target.isInsideZone(ZoneId.PEACE) || attacker.isInsideZone(ZoneId.PEACE));
  4597. }
  4598. /**
  4599. * @return true if this character is inside an active grid.
  4600. */
  4601. public boolean isInActiveRegion()
  4602. {
  4603. L2WorldRegion region = getWorldRegion();
  4604. return ((region != null) && (region.isActive()));
  4605. }
  4606. /**
  4607. * @return True if the L2Character has a Party in progress.
  4608. */
  4609. public boolean isInParty()
  4610. {
  4611. return false;
  4612. }
  4613. /**
  4614. * @return the L2Party object of the L2Character.
  4615. */
  4616. public L2Party getParty()
  4617. {
  4618. return null;
  4619. }
  4620. /**
  4621. * @param target
  4622. * @param weapon
  4623. * @return the Attack Speed of the L2Character (delay (in milliseconds) before next attack).
  4624. */
  4625. public int calculateTimeBetweenAttacks(L2Character target, L2Weapon weapon)
  4626. {
  4627. if ((weapon != null) && !isTransformed())
  4628. {
  4629. switch (weapon.getItemType())
  4630. {
  4631. case BOW:
  4632. return (1500 * 345) / getPAtkSpd();
  4633. case CROSSBOW:
  4634. return (1200 * 345) / getPAtkSpd();
  4635. case DAGGER:
  4636. // atkSpd /= 1.15;
  4637. break;
  4638. }
  4639. }
  4640. return Formulas.calcPAtkSpd(this, target, getPAtkSpd());
  4641. }
  4642. public int calculateReuseTime(L2Character target, L2Weapon weapon)
  4643. {
  4644. if ((weapon == null) || isTransformed())
  4645. {
  4646. return 0;
  4647. }
  4648. int reuse = weapon.getReuseDelay();
  4649. // only bows should continue for now
  4650. if (reuse == 0)
  4651. {
  4652. return 0;
  4653. }
  4654. reuse *= getStat().getWeaponReuseModifier(target);
  4655. double atkSpd = getStat().getPAtkSpd();
  4656. switch (weapon.getItemType())
  4657. {
  4658. case BOW:
  4659. case CROSSBOW:
  4660. return (int) ((reuse * 345) / atkSpd);
  4661. default:
  4662. return (int) ((reuse * 312) / atkSpd);
  4663. }
  4664. }
  4665. /**
  4666. * Add a skill to the L2Character _skills and its Func objects to the calculator set of the L2Character.<br>
  4667. * <B><U>Concept</U>:</B><br>
  4668. * All skills own by a L2Character are identified in <B>_skills</B><br>
  4669. * <B><U>Actions</U>:</B>
  4670. * <ul>
  4671. * <li>Replace oldSkill by newSkill or Add the newSkill</li>
  4672. * <li>If an old skill has been replaced, remove all its Func objects of L2Character calculator set</li>
  4673. * <li>Add Func objects of newSkill to the calculator set of the L2Character</li>
  4674. * </ul>
  4675. * <B><U>Overridden in</U>:</B>
  4676. * <ul>
  4677. * <li>L2PcInstance : Save update in the character_skills table of the database</li>
  4678. * </ul>
  4679. * @param newSkill The L2Skill to add to the L2Character
  4680. * @return The L2Skill replaced or null if just added a new L2Skill
  4681. */
  4682. @Override
  4683. public Skill addSkill(Skill newSkill)
  4684. {
  4685. Skill oldSkill = null;
  4686. if (newSkill != null)
  4687. {
  4688. // Replace oldSkill by newSkill or Add the newSkill
  4689. oldSkill = _skills.put(newSkill.getId(), newSkill);
  4690. // If an old skill has been replaced, remove all its Func objects
  4691. if (oldSkill != null)
  4692. {
  4693. removeStatsOwner(oldSkill);
  4694. }
  4695. // Add Func objects of newSkill to the calculator set of the L2Character
  4696. addStatFuncs(newSkill.getStatFuncs(null, this));
  4697. if (newSkill.isPassive())
  4698. {
  4699. newSkill.applyEffects(this, null, this, false, true, false, 0);
  4700. }
  4701. }
  4702. return oldSkill;
  4703. }
  4704. public Skill removeSkill(Skill skill, boolean cancelEffect)
  4705. {
  4706. return (skill != null) ? removeSkill(skill.getId(), cancelEffect) : null;
  4707. }
  4708. public Skill removeSkill(int skillId)
  4709. {
  4710. return removeSkill(skillId, true);
  4711. }
  4712. public Skill removeSkill(int skillId, boolean cancelEffect)
  4713. {
  4714. // Remove the skill from the L2Character _skills
  4715. Skill oldSkill = _skills.remove(skillId);
  4716. // Remove all its Func objects from the L2Character calculator set
  4717. if (oldSkill != null)
  4718. {
  4719. // Stop casting if this skill is used right now
  4720. if ((getLastSkillCast() != null) && isCastingNow())
  4721. {
  4722. if (oldSkill.getId() == getLastSkillCast().getId())
  4723. {
  4724. abortCast();
  4725. }
  4726. }
  4727. if ((getLastSimultaneousSkillCast() != null) && isCastingSimultaneouslyNow())
  4728. {
  4729. if (oldSkill.getId() == getLastSimultaneousSkillCast().getId())
  4730. {
  4731. abortCast();
  4732. }
  4733. }
  4734. // Stop effects.
  4735. if (cancelEffect || oldSkill.isToggle() || oldSkill.isPassive())
  4736. {
  4737. removeStatsOwner(oldSkill);
  4738. stopSkillEffects(true, oldSkill.getId());
  4739. }
  4740. }
  4741. return oldSkill;
  4742. }
  4743. /**
  4744. * <B><U>Concept</U>:</B><br>
  4745. * All skills own by a L2Character are identified in <B>_skills</B> the L2Character
  4746. * @return all skills own by the L2Character in a table of L2Skill.
  4747. */
  4748. public final Collection<Skill> getAllSkills()
  4749. {
  4750. return new ArrayList<>(_skills.values());
  4751. }
  4752. /**
  4753. * @return the map containing this character skills.
  4754. */
  4755. @Override
  4756. public Map<Integer, Skill> getSkills()
  4757. {
  4758. return _skills;
  4759. }
  4760. /**
  4761. * Return the level of a skill owned by the L2Character.
  4762. * @param skillId The identifier of the L2Skill whose level must be returned
  4763. * @return The level of the L2Skill identified by skillId
  4764. */
  4765. @Override
  4766. public int getSkillLevel(int skillId)
  4767. {
  4768. final Skill skill = getKnownSkill(skillId);
  4769. return (skill == null) ? -1 : skill.getLevel();
  4770. }
  4771. /**
  4772. * @param skillId The identifier of the L2Skill to check the knowledge
  4773. * @return the skill from the known skill.
  4774. */
  4775. @Override
  4776. public final Skill getKnownSkill(int skillId)
  4777. {
  4778. return _skills.get(skillId);
  4779. }
  4780. /**
  4781. * Return the number of buffs affecting this L2Character.
  4782. * @return The number of Buffs affecting this L2Character
  4783. */
  4784. public int getBuffCount()
  4785. {
  4786. return _effectList.getBuffCount();
  4787. }
  4788. public int getDanceCount()
  4789. {
  4790. return _effectList.getDanceCount();
  4791. }
  4792. /**
  4793. * Manage the magic skill launching task (MP, HP, Item consumation...) and display the magic skill animation on client.<br>
  4794. * <B><U>Actions</U>:</B>
  4795. * <ul>
  4796. * <li>Send a Server->Client packet MagicSkillLaunched (to display magic skill animation) to all L2PcInstance of L2Charcater _knownPlayers</li>
  4797. * <li>Consumme MP, HP and Item if necessary</li>
  4798. * <li>Send a Server->Client packet StatusUpdate with MP modification to the L2PcInstance</li>
  4799. * <li>Launch the magic skill in order to calculate its effects</li>
  4800. * <li>If the skill type is PDAM, notify the AI of the target with AI_INTENTION_ATTACK</li>
  4801. * <li>Notify the AI of the L2Character with EVT_FINISH_CASTING</li>
  4802. * </ul>
  4803. * <FONT COLOR=#FF0000><B><U>Caution</U>: A magic skill casting MUST BE in progress</B></FONT>
  4804. * @param mut
  4805. */
  4806. public void onMagicLaunchedTimer(MagicUseTask mut)
  4807. {
  4808. final Skill skill = mut.getSkill();
  4809. L2Object[] targets = mut.getTargets();
  4810. if ((skill == null) || (targets == null))
  4811. {
  4812. abortCast();
  4813. return;
  4814. }
  4815. if (targets.length == 0)
  4816. {
  4817. switch (skill.getTargetType())
  4818. {
  4819. // only AURA-type skills can be cast without target
  4820. case AURA:
  4821. case FRONT_AURA:
  4822. case BEHIND_AURA:
  4823. case AURA_CORPSE_MOB:
  4824. break;
  4825. default:
  4826. abortCast();
  4827. return;
  4828. }
  4829. }
  4830. // Escaping from under skill's radius and peace zone check. First version, not perfect in AoE skills.
  4831. int escapeRange = 0;
  4832. if (skill.getEffectRange() > escapeRange)
  4833. {
  4834. escapeRange = skill.getEffectRange();
  4835. }
  4836. else if ((skill.getCastRange() < 0) && (skill.getAffectRange() > 80))
  4837. {
  4838. escapeRange = skill.getAffectRange();
  4839. }
  4840. if ((targets.length > 0) && (escapeRange > 0))
  4841. {
  4842. int _skiprange = 0;
  4843. int _skipgeo = 0;
  4844. int _skippeace = 0;
  4845. final List<L2Object> targetList = new ArrayList<>();
  4846. for (L2Object target : targets)
  4847. {
  4848. if (target instanceof L2Character)
  4849. {
  4850. if (!isInsideRadius(target.getX(), target.getY(), target.getZ(), escapeRange + getTemplate().getCollisionRadius(), true, false))
  4851. {
  4852. _skiprange++;
  4853. continue;
  4854. }
  4855. if ((escapeRange > 0) && !GeoData.getInstance().canSeeTarget(this, target))
  4856. {
  4857. _skipgeo++;
  4858. continue;
  4859. }
  4860. if (skill.isBad())
  4861. {
  4862. if (isPlayer())
  4863. {
  4864. if (((L2Character) target).isInsidePeaceZone(getActingPlayer()))
  4865. {
  4866. _skippeace++;
  4867. continue;
  4868. }
  4869. }
  4870. else
  4871. {
  4872. if (((L2Character) target).isInsidePeaceZone(this, target))
  4873. {
  4874. _skippeace++;
  4875. continue;
  4876. }
  4877. }
  4878. }
  4879. targetList.add(target);
  4880. }
  4881. }
  4882. if (targetList.isEmpty())
  4883. {
  4884. if (isPlayer())
  4885. {
  4886. if (_skiprange > 0)
  4887. {
  4888. sendPacket(SystemMessageId.DIST_TOO_FAR_CASTING_STOPPED);
  4889. }
  4890. else if (_skipgeo > 0)
  4891. {
  4892. sendPacket(SystemMessageId.CANT_SEE_TARGET);
  4893. }
  4894. else if (_skippeace > 0)
  4895. {
  4896. sendPacket(SystemMessageId.A_MALICIOUS_SKILL_CANNOT_BE_USED_IN_PEACE_ZONE);
  4897. }
  4898. }
  4899. abortCast();
  4900. return;
  4901. }
  4902. mut.setTargets(targetList.toArray(new L2Object[targetList.size()]));
  4903. }
  4904. // Ensure that a cast is in progress
  4905. // Check if player is using fake death.
  4906. // Static skills can be used while faking death.
  4907. if ((mut.isSimultaneous() && !isCastingSimultaneouslyNow()) || (!mut.isSimultaneous() && !isCastingNow()) || (isAlikeDead() && !skill.isStatic()))
  4908. {
  4909. // now cancels both, simultaneous and normal
  4910. getAI().notifyEvent(CtrlEvent.EVT_CANCEL);
  4911. return;
  4912. }
  4913. if (!skill.isToggle())
  4914. {
  4915. broadcastPacket(new MagicSkillLaunched(this, skill.getDisplayId(), skill.getDisplayLevel(), targets));
  4916. }
  4917. mut.setPhase(2);
  4918. if (mut.getSkillTime() == 0)
  4919. {
  4920. onMagicHitTimer(mut);
  4921. }
  4922. else
  4923. {
  4924. _skillCast = ThreadPoolManager.getInstance().scheduleEffect(mut, 400);
  4925. }
  4926. }
  4927. // Runs in the end of skill casting
  4928. public void onMagicHitTimer(MagicUseTask mut)
  4929. {
  4930. final Skill skill = mut.getSkill();
  4931. final L2Object[] targets = mut.getTargets();
  4932. if ((skill == null) || (targets == null))
  4933. {
  4934. abortCast();
  4935. return;
  4936. }
  4937. try
  4938. {
  4939. // Go through targets table
  4940. for (L2Object tgt : targets)
  4941. {
  4942. if (tgt.isPlayable())
  4943. {
  4944. if (isPlayer() && tgt.isSummon())
  4945. {
  4946. ((L2Summon) tgt).updateAndBroadcastStatus(1);
  4947. }
  4948. }
  4949. else if (isPlayable() && tgt.isAttackable())
  4950. {
  4951. L2Character target = (L2Character) tgt;
  4952. if (skill.getEffectPoint() > 0)
  4953. {
  4954. ((L2Attackable) target).reduceHate(this, skill.getEffectPoint());
  4955. }
  4956. else if (skill.getEffectPoint() < 0)
  4957. {
  4958. ((L2Attackable) target).addDamageHate(this, 0, -skill.getEffectPoint());
  4959. }
  4960. }
  4961. }
  4962. rechargeShots(skill.useSoulShot(), skill.useSpiritShot());
  4963. final StatusUpdate su = new StatusUpdate(this);
  4964. boolean isSendStatus = false;
  4965. // Consume MP of the L2Character and Send the Server->Client packet StatusUpdate with current HP and MP to all other L2PcInstance to inform
  4966. double mpConsume = getStat().getMpConsume(skill);
  4967. if (mpConsume > 0)
  4968. {
  4969. if (mpConsume > getCurrentMp())
  4970. {
  4971. sendPacket(SystemMessageId.NOT_ENOUGH_MP);
  4972. abortCast();
  4973. return;
  4974. }
  4975. getStatus().reduceMp(mpConsume);
  4976. su.addAttribute(StatusUpdate.CUR_MP, (int) getCurrentMp());
  4977. isSendStatus = true;
  4978. }
  4979. // Consume HP if necessary and Send the Server->Client packet StatusUpdate with current HP and MP to all other L2PcInstance to inform
  4980. if (skill.getHpConsume() > 0)
  4981. {
  4982. double consumeHp = skill.getHpConsume();
  4983. if (consumeHp >= getCurrentHp())
  4984. {
  4985. sendPacket(SystemMessageId.NOT_ENOUGH_HP);
  4986. abortCast();
  4987. return;
  4988. }
  4989. getStatus().reduceHp(consumeHp, this, true);
  4990. su.addAttribute(StatusUpdate.CUR_HP, (int) getCurrentHp());
  4991. isSendStatus = true;
  4992. }
  4993. // Send a Server->Client packet StatusUpdate with MP modification to the L2PcInstance
  4994. if (isSendStatus)
  4995. {
  4996. sendPacket(su);
  4997. }
  4998. if (isPlayer())
  4999. {
  5000. // Consume Charges
  5001. if (skill.getChargeConsume() > 0)
  5002. {
  5003. getActingPlayer().decreaseCharges(skill.getChargeConsume());
  5004. }
  5005. // Consume Souls if necessary
  5006. if (skill.getMaxSoulConsumeCount() > 0)
  5007. {
  5008. if (!getActingPlayer().decreaseSouls(skill.getMaxSoulConsumeCount(), skill))
  5009. {
  5010. abortCast();
  5011. return;
  5012. }
  5013. }
  5014. }
  5015. // Launch the magic skill in order to calculate its effects
  5016. callSkill(mut.getSkill(), mut.getTargets());
  5017. }
  5018. catch (NullPointerException e)
  5019. {
  5020. _log.log(Level.WARNING, "", e);
  5021. }
  5022. if (mut.getSkillTime() > 0)
  5023. {
  5024. mut.setCount(mut.getCount() + 1);
  5025. }
  5026. mut.setPhase(3);
  5027. if (mut.getSkillTime() == 0)
  5028. {
  5029. onMagicFinalizer(mut);
  5030. }
  5031. else
  5032. {
  5033. if (mut.isSimultaneous())
  5034. {
  5035. _skillCast2 = ThreadPoolManager.getInstance().scheduleEffect(mut, 0);
  5036. }
  5037. else
  5038. {
  5039. _skillCast = ThreadPoolManager.getInstance().scheduleEffect(mut, 0);
  5040. }
  5041. }
  5042. }
  5043. // Runs after skillTime
  5044. public void onMagicFinalizer(MagicUseTask mut)
  5045. {
  5046. if (mut.isSimultaneous())
  5047. {
  5048. _skillCast2 = null;
  5049. setIsCastingSimultaneouslyNow(false);
  5050. return;
  5051. }
  5052. // Cleanup
  5053. _skillCast = null;
  5054. _castInterruptTime = 0;
  5055. // Stop casting
  5056. setIsCastingNow(false);
  5057. final Skill skill = mut.getSkill();
  5058. final L2Object target = mut.getTargets().length > 0 ? mut.getTargets()[0] : null;
  5059. // On each repeat recharge shots before cast.
  5060. if (mut.getCount() > 0)
  5061. {
  5062. rechargeShots(mut.getSkill().useSoulShot(), mut.getSkill().useSpiritShot());
  5063. }
  5064. // Attack target after skill use
  5065. if ((skill.nextActionIsAttack()) && (getTarget() instanceof L2Character) && (getTarget() != this) && (target != null) && (getTarget() == target) && target.canBeAttacked())
  5066. {
  5067. if ((getAI().getNextIntention() == null) || (getAI().getNextIntention().getCtrlIntention() != CtrlIntention.AI_INTENTION_MOVE_TO))
  5068. {
  5069. getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  5070. }
  5071. }
  5072. if (skill.isBad() && (skill.getTargetType() != L2TargetType.UNLOCKABLE))
  5073. {
  5074. getAI().clientStartAutoAttack();
  5075. }
  5076. // Notify the AI of the L2Character with EVT_FINISH_CASTING
  5077. getAI().notifyEvent(CtrlEvent.EVT_FINISH_CASTING);
  5078. // Notify DP Scripts
  5079. notifyQuestEventSkillFinished(skill, target);
  5080. // If character is a player, then wipe their current cast state and check if a skill is queued.
  5081. // If there is a queued skill, launch it and wipe the queue.
  5082. if (isPlayer())
  5083. {
  5084. L2PcInstance currPlayer = getActingPlayer();
  5085. SkillUseHolder queuedSkill = currPlayer.getQueuedSkill();
  5086. currPlayer.setCurrentSkill(null, false, false);
  5087. if (queuedSkill != null)
  5088. {
  5089. currPlayer.setQueuedSkill(null, false, false);
  5090. // DON'T USE : Recursive call to useMagic() method
  5091. // currPlayer.useMagic(queuedSkill.getSkill(), queuedSkill.isCtrlPressed(), queuedSkill.isShiftPressed());
  5092. ThreadPoolManager.getInstance().executeGeneral(new QueuedMagicUseTask(currPlayer, queuedSkill.getSkill(), queuedSkill.isCtrlPressed(), queuedSkill.isShiftPressed()));
  5093. }
  5094. }
  5095. if (isChanneling())
  5096. {
  5097. getSkillChannelizer().stopChanneling();
  5098. }
  5099. }
  5100. // Quest event ON_SPELL_FNISHED
  5101. protected void notifyQuestEventSkillFinished(Skill skill, L2Object target)
  5102. {
  5103. }
  5104. /**
  5105. * Launch the magic skill and calculate its effects on each target contained in the targets table.
  5106. * @param skill The L2Skill to use
  5107. * @param targets The table of L2Object targets
  5108. */
  5109. public void callSkill(Skill skill, L2Object[] targets)
  5110. {
  5111. try
  5112. {
  5113. L2Weapon activeWeapon = getActiveWeaponItem();
  5114. // Check if the toggle skill effects are already in progress on the L2Character
  5115. if (skill.isToggle() && isAffectedBySkill(skill.getId()))
  5116. {
  5117. return;
  5118. }
  5119. // Initial checks
  5120. for (L2Object obj : targets)
  5121. {
  5122. if ((obj == null) || !obj.isCharacter())
  5123. {
  5124. continue;
  5125. }
  5126. final L2Character target = (L2Character) obj;
  5127. // Check raid monster attack and check buffing characters who attack raid monsters.
  5128. L2Character targetsAttackTarget = null;
  5129. L2Character targetsCastTarget = null;
  5130. if (target.hasAI())
  5131. {
  5132. targetsAttackTarget = target.getAI().getAttackTarget();
  5133. targetsCastTarget = target.getAI().getCastTarget();
  5134. }
  5135. 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)))))
  5136. {
  5137. final CommonSkill curse = skill.isMagic() ? CommonSkill.RAID_CURSE : CommonSkill.RAID_CURSE2;
  5138. Skill curseSkill = curse.getSkill();
  5139. if (curseSkill != null)
  5140. {
  5141. abortAttack();
  5142. abortCast();
  5143. getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  5144. curseSkill.applyEffects(target, this);
  5145. }
  5146. else
  5147. {
  5148. _log.warning("Skill ID " + curse.getId() + " level " + curse.getLevel() + " is missing in DP!");
  5149. }
  5150. return;
  5151. }
  5152. // Check if over-hit is possible
  5153. if (skill.isOverhit())
  5154. {
  5155. if (target.isAttackable())
  5156. {
  5157. ((L2Attackable) target).overhitEnabled(true);
  5158. }
  5159. }
  5160. // Static skills not trigger any chance skills
  5161. if (!skill.isStatic())
  5162. {
  5163. // Launch weapon Special ability skill effect if available
  5164. if ((activeWeapon != null) && !target.isDead())
  5165. {
  5166. activeWeapon.castOnMagicSkill(this, target, skill);
  5167. }
  5168. if (_triggerSkills != null)
  5169. {
  5170. for (OptionsSkillHolder holder : _triggerSkills.values())
  5171. {
  5172. if ((skill.isMagic() && (holder.getSkillType() == OptionsSkillType.MAGIC)) || (skill.isPhysical() && (holder.getSkillType() == OptionsSkillType.ATTACK)))
  5173. {
  5174. if (Rnd.get(100) < holder.getChance())
  5175. {
  5176. makeTriggerCast(holder.getSkill(), target);
  5177. }
  5178. }
  5179. }
  5180. }
  5181. }
  5182. }
  5183. // Launch the magic skill and calculate its effects
  5184. skill.activateSkill(this, targets);
  5185. L2PcInstance player = getActingPlayer();
  5186. if (player != null)
  5187. {
  5188. for (L2Object target : targets)
  5189. {
  5190. // EVT_ATTACKED and PvPStatus
  5191. if (target instanceof L2Character)
  5192. {
  5193. if (skill.getEffectPoint() <= 0)
  5194. {
  5195. if ((target.isPlayable() || target.isTrap()) && skill.isBad())
  5196. {
  5197. // Casted on target_self but don't harm self
  5198. if (!target.equals(this))
  5199. {
  5200. // Combat-mode check
  5201. if (target.isPlayer())
  5202. {
  5203. target.getActingPlayer().getAI().clientStartAutoAttack();
  5204. }
  5205. else if (target.isSummon() && ((L2Character) target).hasAI())
  5206. {
  5207. L2PcInstance owner = ((L2Summon) target).getOwner();
  5208. if (owner != null)
  5209. {
  5210. owner.getAI().clientStartAutoAttack();
  5211. }
  5212. }
  5213. // attack of the own pet does not flag player
  5214. // triggering trap not flag trap owner
  5215. if ((player.getSummon() != target) && !isTrap() && !((skill.getEffectPoint() == 0) && (skill.getAffectRange() > 0)))
  5216. {
  5217. player.updatePvPStatus((L2Character) target);
  5218. }
  5219. }
  5220. }
  5221. else if (target.isAttackable())
  5222. {
  5223. switch (skill.getId())
  5224. {
  5225. case 51: // Lure
  5226. case 511: // Temptation
  5227. break;
  5228. default:
  5229. // add attacker into list
  5230. ((L2Character) target).addAttackerToAttackByList(this);
  5231. }
  5232. }
  5233. // notify target AI about the attack
  5234. if (((L2Character) target).hasAI() && !skill.hasEffectType(L2EffectType.HATE))
  5235. {
  5236. ((L2Character) target).getAI().notifyEvent(CtrlEvent.EVT_ATTACKED, this);
  5237. }
  5238. }
  5239. else
  5240. {
  5241. if (target.isPlayer())
  5242. {
  5243. // Casting non offensive skill on player with pvp flag set or with karma
  5244. if (!(target.equals(this) || target.equals(player)) && ((target.getActingPlayer().getPvpFlag() > 0) || (target.getActingPlayer().getKarma() > 0)))
  5245. {
  5246. player.updatePvPStatus();
  5247. }
  5248. }
  5249. else if (target.isAttackable())
  5250. {
  5251. player.updatePvPStatus();
  5252. }
  5253. }
  5254. }
  5255. }
  5256. // Mobs in range 1000 see spell
  5257. Collection<L2Object> objs = player.getKnownList().getKnownObjects().values();
  5258. for (L2Object spMob : objs)
  5259. {
  5260. if ((spMob != null) && spMob.isNpc())
  5261. {
  5262. final L2Npc npcMob = (L2Npc) spMob;
  5263. if ((npcMob.isInsideRadius(player, 1000, true, true)))
  5264. {
  5265. EventDispatcher.getInstance().notifyEventAsync(new OnNpcSkillSee(npcMob, player, skill, targets, isSummon()), npcMob);
  5266. // On Skill See logic
  5267. if (npcMob.isAttackable())
  5268. {
  5269. final L2Attackable attackable = (L2Attackable) npcMob;
  5270. int skillEffectPoint = skill.getEffectPoint();
  5271. if (player.hasSummon())
  5272. {
  5273. if ((targets.length == 1) && Util.contains(targets, player.getSummon()))
  5274. {
  5275. skillEffectPoint = 0;
  5276. }
  5277. }
  5278. if (skillEffectPoint > 0)
  5279. {
  5280. if (attackable.hasAI() && (attackable.getAI().getIntention() == AI_INTENTION_ATTACK))
  5281. {
  5282. L2Object npcTarget = attackable.getTarget();
  5283. for (L2Object skillTarget : targets)
  5284. {
  5285. if ((npcTarget == skillTarget) || (npcMob == skillTarget))
  5286. {
  5287. L2Character originalCaster = isSummon() ? getSummon() : player;
  5288. attackable.addDamageHate(originalCaster, 0, (skillEffectPoint * 150) / (attackable.getLevel() + 7));
  5289. }
  5290. }
  5291. }
  5292. }
  5293. }
  5294. }
  5295. }
  5296. }
  5297. }
  5298. // Notify AI
  5299. if (skill.isBad() && !skill.hasEffectType(L2EffectType.HATE))
  5300. {
  5301. for (L2Object target : targets)
  5302. {
  5303. if ((target instanceof L2Character) && ((L2Character) target).hasAI())
  5304. {
  5305. // notify target AI about the attack
  5306. ((L2Character) target).getAI().notifyEvent(CtrlEvent.EVT_ATTACKED, this);
  5307. }
  5308. }
  5309. }
  5310. }
  5311. catch (Exception e)
  5312. {
  5313. _log.log(Level.WARNING, getClass().getSimpleName() + ": callSkill() failed.", e);
  5314. }
  5315. }
  5316. /**
  5317. * @param target
  5318. * @return True if the L2Character is behind the target and can't be seen.
  5319. */
  5320. public boolean isBehind(L2Object target)
  5321. {
  5322. double angleChar, angleTarget, angleDiff, maxAngleDiff = 60;
  5323. if (target == null)
  5324. {
  5325. return false;
  5326. }
  5327. if (target instanceof L2Character)
  5328. {
  5329. L2Character target1 = (L2Character) target;
  5330. angleChar = Util.calculateAngleFrom(this, target1);
  5331. angleTarget = Util.convertHeadingToDegree(target1.getHeading());
  5332. angleDiff = angleChar - angleTarget;
  5333. if (angleDiff <= (-360 + maxAngleDiff))
  5334. {
  5335. angleDiff += 360;
  5336. }
  5337. if (angleDiff >= (360 - maxAngleDiff))
  5338. {
  5339. angleDiff -= 360;
  5340. }
  5341. if (Math.abs(angleDiff) <= maxAngleDiff)
  5342. {
  5343. return true;
  5344. }
  5345. }
  5346. return false;
  5347. }
  5348. public boolean isBehindTarget()
  5349. {
  5350. return isBehind(getTarget());
  5351. }
  5352. /**
  5353. * @param target
  5354. * @return True if the target is facing the L2Character.
  5355. */
  5356. public boolean isInFrontOf(L2Character target)
  5357. {
  5358. double angleChar, angleTarget, angleDiff, maxAngleDiff = 60;
  5359. if (target == null)
  5360. {
  5361. return false;
  5362. }
  5363. angleTarget = Util.calculateAngleFrom(target, this);
  5364. angleChar = Util.convertHeadingToDegree(target.getHeading());
  5365. angleDiff = angleChar - angleTarget;
  5366. if (angleDiff <= (-360 + maxAngleDiff))
  5367. {
  5368. angleDiff += 360;
  5369. }
  5370. if (angleDiff >= (360 - maxAngleDiff))
  5371. {
  5372. angleDiff -= 360;
  5373. }
  5374. return Math.abs(angleDiff) <= maxAngleDiff;
  5375. }
  5376. /**
  5377. * @param target
  5378. * @param maxAngle
  5379. * @return true if target is in front of L2Character (shield def etc)
  5380. */
  5381. public boolean isFacing(L2Object target, int maxAngle)
  5382. {
  5383. double angleChar, angleTarget, angleDiff, maxAngleDiff;
  5384. if (target == null)
  5385. {
  5386. return false;
  5387. }
  5388. maxAngleDiff = maxAngle / 2.;
  5389. angleTarget = Util.calculateAngleFrom(this, target);
  5390. angleChar = Util.convertHeadingToDegree(getHeading());
  5391. angleDiff = angleChar - angleTarget;
  5392. if (angleDiff <= (-360 + maxAngleDiff))
  5393. {
  5394. angleDiff += 360;
  5395. }
  5396. if (angleDiff >= (360 - maxAngleDiff))
  5397. {
  5398. angleDiff -= 360;
  5399. }
  5400. return Math.abs(angleDiff) <= maxAngleDiff;
  5401. }
  5402. public boolean isInFrontOfTarget()
  5403. {
  5404. L2Object target = getTarget();
  5405. if (target instanceof L2Character)
  5406. {
  5407. return isInFrontOf((L2Character) target);
  5408. }
  5409. return false;
  5410. }
  5411. /**
  5412. * @return the Level Modifier ((level + 89) / 100).
  5413. */
  5414. public double getLevelMod()
  5415. {
  5416. return ((getLevel() + 89) / 100d);
  5417. }
  5418. public final void setSkillCast(Future<?> newSkillCast)
  5419. {
  5420. _skillCast = newSkillCast;
  5421. }
  5422. /**
  5423. * Sets _isCastingNow to true and _castInterruptTime is calculated from end time (ticks)
  5424. * @param newSkillCastEndTick
  5425. */
  5426. public final void forceIsCasting(int newSkillCastEndTick)
  5427. {
  5428. setIsCastingNow(true);
  5429. // for interrupt -400 ms
  5430. _castInterruptTime = newSkillCastEndTick - 4;
  5431. }
  5432. private boolean _AIdisabled = false;
  5433. public void updatePvPFlag(int value)
  5434. {
  5435. // Overridden in L2PcInstance
  5436. }
  5437. /**
  5438. * @return a multiplier based on weapon random damage
  5439. */
  5440. public final double getRandomDamageMultiplier()
  5441. {
  5442. L2Weapon activeWeapon = getActiveWeaponItem();
  5443. int random;
  5444. if (activeWeapon != null)
  5445. {
  5446. random = activeWeapon.getRandomDamage();
  5447. }
  5448. else
  5449. {
  5450. random = 5 + (int) Math.sqrt(getLevel());
  5451. }
  5452. return (1 + ((double) Rnd.get(0 - random, random) / 100));
  5453. }
  5454. public final long getAttackEndTime()
  5455. {
  5456. return _attackEndTime;
  5457. }
  5458. public int getBowAttackEndTime()
  5459. {
  5460. return _disableBowAttackEndTime;
  5461. }
  5462. /**
  5463. * Not Implemented.
  5464. * @return
  5465. */
  5466. public abstract int getLevel();
  5467. public final double calcStat(Stats stat, double init)
  5468. {
  5469. return getStat().calcStat(stat, init, null, null);
  5470. }
  5471. // Stat - NEED TO REMOVE ONCE L2CHARSTAT IS COMPLETE
  5472. public final double calcStat(Stats stat, double init, L2Character target, Skill skill)
  5473. {
  5474. return getStat().calcStat(stat, init, target, skill);
  5475. }
  5476. public int getAccuracy()
  5477. {
  5478. return getStat().getAccuracy();
  5479. }
  5480. public final float getAttackSpeedMultiplier()
  5481. {
  5482. return getStat().getAttackSpeedMultiplier();
  5483. }
  5484. public final double getCriticalDmg(L2Character target, double init)
  5485. {
  5486. return getStat().getCriticalDmg(target, init);
  5487. }
  5488. public int getCriticalHit(L2Character target, Skill skill)
  5489. {
  5490. return getStat().getCriticalHit(target, skill);
  5491. }
  5492. public int getEvasionRate(L2Character target)
  5493. {
  5494. return getStat().getEvasionRate(target);
  5495. }
  5496. public final int getMagicalAttackRange(Skill skill)
  5497. {
  5498. return getStat().getMagicalAttackRange(skill);
  5499. }
  5500. public final int getMaxCp()
  5501. {
  5502. return getStat().getMaxCp();
  5503. }
  5504. public final int getMaxRecoverableCp()
  5505. {
  5506. return getStat().getMaxRecoverableCp();
  5507. }
  5508. public int getMAtk(L2Character target, Skill skill)
  5509. {
  5510. return getStat().getMAtk(target, skill);
  5511. }
  5512. public int getMAtkSpd()
  5513. {
  5514. return getStat().getMAtkSpd();
  5515. }
  5516. public int getMaxMp()
  5517. {
  5518. return getStat().getMaxMp();
  5519. }
  5520. public int getMaxRecoverableMp()
  5521. {
  5522. return getStat().getMaxRecoverableMp();
  5523. }
  5524. public int getMaxHp()
  5525. {
  5526. return getStat().getMaxHp();
  5527. }
  5528. public int getMaxRecoverableHp()
  5529. {
  5530. return getStat().getMaxRecoverableHp();
  5531. }
  5532. public final int getMCriticalHit(L2Character target, Skill skill)
  5533. {
  5534. return getStat().getMCriticalHit(target, skill);
  5535. }
  5536. public int getMDef(L2Character target, Skill skill)
  5537. {
  5538. return getStat().getMDef(target, skill);
  5539. }
  5540. public double getMReuseRate(Skill skill)
  5541. {
  5542. return getStat().getMReuseRate(skill);
  5543. }
  5544. public int getPAtk(L2Character target)
  5545. {
  5546. return getStat().getPAtk(target);
  5547. }
  5548. public int getPAtkSpd()
  5549. {
  5550. return getStat().getPAtkSpd();
  5551. }
  5552. public int getPDef(L2Character target)
  5553. {
  5554. return getStat().getPDef(target);
  5555. }
  5556. public final int getPhysicalAttackRange()
  5557. {
  5558. return getStat().getPhysicalAttackRange();
  5559. }
  5560. public double getMovementSpeedMultiplier()
  5561. {
  5562. return getStat().getMovementSpeedMultiplier();
  5563. }
  5564. public double getRunSpeed()
  5565. {
  5566. return getStat().getRunSpeed();
  5567. }
  5568. public double getWalkSpeed()
  5569. {
  5570. return getStat().getWalkSpeed();
  5571. }
  5572. public final double getSwimRunSpeed()
  5573. {
  5574. return getStat().getSwimRunSpeed();
  5575. }
  5576. public final double getSwimWalkSpeed()
  5577. {
  5578. return getStat().getSwimWalkSpeed();
  5579. }
  5580. public double getMoveSpeed()
  5581. {
  5582. return getStat().getMoveSpeed();
  5583. }
  5584. public final int getShldDef()
  5585. {
  5586. return getStat().getShldDef();
  5587. }
  5588. public int getSTR()
  5589. {
  5590. return getStat().getSTR();
  5591. }
  5592. public int getDEX()
  5593. {
  5594. return getStat().getDEX();
  5595. }
  5596. public int getCON()
  5597. {
  5598. return getStat().getCON();
  5599. }
  5600. public int getINT()
  5601. {
  5602. return getStat().getINT();
  5603. }
  5604. public int getWIT()
  5605. {
  5606. return getStat().getWIT();
  5607. }
  5608. public int getMEN()
  5609. {
  5610. return getStat().getMEN();
  5611. }
  5612. // Status - NEED TO REMOVE ONCE L2CHARTATUS IS COMPLETE
  5613. public void addStatusListener(L2Character object)
  5614. {
  5615. getStatus().addStatusListener(object);
  5616. }
  5617. public void reduceCurrentHp(double i, L2Character attacker, Skill skill)
  5618. {
  5619. reduceCurrentHp(i, attacker, true, false, skill);
  5620. }
  5621. public void reduceCurrentHpByDOT(double i, L2Character attacker, Skill skill)
  5622. {
  5623. reduceCurrentHp(i, attacker, !skill.isToggle(), true, skill);
  5624. }
  5625. public void reduceCurrentHp(double i, L2Character attacker, boolean awake, boolean isDOT, Skill skill)
  5626. {
  5627. if (Config.L2JMOD_CHAMPION_ENABLE && isChampion() && (Config.L2JMOD_CHAMPION_HP != 0))
  5628. {
  5629. getStatus().reduceHp(i / Config.L2JMOD_CHAMPION_HP, attacker, awake, isDOT, false);
  5630. }
  5631. else
  5632. {
  5633. getStatus().reduceHp(i, attacker, awake, isDOT, false);
  5634. }
  5635. }
  5636. public void reduceCurrentMp(double i)
  5637. {
  5638. getStatus().reduceMp(i);
  5639. }
  5640. @Override
  5641. public void removeStatusListener(L2Character object)
  5642. {
  5643. getStatus().removeStatusListener(object);
  5644. }
  5645. protected void stopHpMpRegeneration()
  5646. {
  5647. getStatus().stopHpMpRegeneration();
  5648. }
  5649. public final double getCurrentCp()
  5650. {
  5651. return getStatus().getCurrentCp();
  5652. }
  5653. public final void setCurrentCp(double newCp)
  5654. {
  5655. getStatus().setCurrentCp(newCp);
  5656. }
  5657. public final double getCurrentHp()
  5658. {
  5659. return getStatus().getCurrentHp();
  5660. }
  5661. public final void setCurrentHp(double newHp)
  5662. {
  5663. getStatus().setCurrentHp(newHp);
  5664. }
  5665. public final void setCurrentHpMp(double newHp, double newMp)
  5666. {
  5667. getStatus().setCurrentHpMp(newHp, newMp);
  5668. }
  5669. public final double getCurrentMp()
  5670. {
  5671. return getStatus().getCurrentMp();
  5672. }
  5673. public final void setCurrentMp(double newMp)
  5674. {
  5675. getStatus().setCurrentMp(newMp);
  5676. }
  5677. /**
  5678. * @return the max weight that the L2Character can load.
  5679. */
  5680. public int getMaxLoad()
  5681. {
  5682. if (isPlayer() || isPet())
  5683. {
  5684. // Weight Limit = (CON Modifier*69000) * Skills
  5685. // Source http://l2p.bravehost.com/weightlimit.html (May 2007)
  5686. double baseLoad = Math.floor(BaseStats.CON.calcBonus(this) * 69000 * Config.ALT_WEIGHT_LIMIT);
  5687. return (int) calcStat(Stats.WEIGHT_LIMIT, baseLoad, this, null);
  5688. }
  5689. return 0;
  5690. }
  5691. public int getBonusWeightPenalty()
  5692. {
  5693. if (isPlayer() || isPet())
  5694. {
  5695. return (int) calcStat(Stats.WEIGHT_PENALTY, 1, this, null);
  5696. }
  5697. return 0;
  5698. }
  5699. /**
  5700. * @return the current weight of the L2Character.
  5701. */
  5702. public int getCurrentLoad()
  5703. {
  5704. if (isPlayer() || isPet())
  5705. {
  5706. return getInventory().getTotalWeight();
  5707. }
  5708. return 0;
  5709. }
  5710. public boolean isChampion()
  5711. {
  5712. return false;
  5713. }
  5714. /**
  5715. * Send system message about damage.
  5716. * @param target
  5717. * @param damage
  5718. * @param mcrit
  5719. * @param pcrit
  5720. * @param miss
  5721. */
  5722. public void sendDamageMessage(L2Character target, int damage, boolean mcrit, boolean pcrit, boolean miss)
  5723. {
  5724. }
  5725. public byte getAttackElement()
  5726. {
  5727. return getStat().getAttackElement();
  5728. }
  5729. public int getAttackElementValue(byte attackAttribute)
  5730. {
  5731. return getStat().getAttackElementValue(attackAttribute);
  5732. }
  5733. public int getDefenseElementValue(byte defenseAttribute)
  5734. {
  5735. return getStat().getDefenseElementValue(defenseAttribute);
  5736. }
  5737. public final void startPhysicalAttackMuted()
  5738. {
  5739. abortAttack();
  5740. }
  5741. public void disableCoreAI(boolean val)
  5742. {
  5743. _AIdisabled = val;
  5744. }
  5745. public boolean isCoreAIDisabled()
  5746. {
  5747. return _AIdisabled;
  5748. }
  5749. /**
  5750. * @return true
  5751. */
  5752. public boolean giveRaidCurse()
  5753. {
  5754. return true;
  5755. }
  5756. /**
  5757. * Check if target is affected with special buff
  5758. * @see CharEffectList#isAffected(EffectFlag)
  5759. * @param flag int
  5760. * @return boolean
  5761. */
  5762. public boolean isAffected(EffectFlag flag)
  5763. {
  5764. return _effectList.isAffected(flag);
  5765. }
  5766. public void broadcastSocialAction(int id)
  5767. {
  5768. broadcastPacket(new SocialAction(getObjectId(), id));
  5769. }
  5770. public Team getTeam()
  5771. {
  5772. return _team;
  5773. }
  5774. public void setTeam(Team team)
  5775. {
  5776. _team = team;
  5777. }
  5778. public void addOverrideCond(PcCondOverride... excs)
  5779. {
  5780. for (PcCondOverride exc : excs)
  5781. {
  5782. _exceptions |= exc.getMask();
  5783. }
  5784. }
  5785. public void removeOverridedCond(PcCondOverride... excs)
  5786. {
  5787. for (PcCondOverride exc : excs)
  5788. {
  5789. _exceptions &= ~exc.getMask();
  5790. }
  5791. }
  5792. public boolean canOverrideCond(PcCondOverride excs)
  5793. {
  5794. return (_exceptions & excs.getMask()) == excs.getMask();
  5795. }
  5796. public void setOverrideCond(long masks)
  5797. {
  5798. _exceptions = masks;
  5799. }
  5800. public void setLethalable(boolean val)
  5801. {
  5802. _lethalable = val;
  5803. }
  5804. public boolean isLethalable()
  5805. {
  5806. return _lethalable;
  5807. }
  5808. public Map<Integer, OptionsSkillHolder> getTriggerSkills()
  5809. {
  5810. if (_triggerSkills == null)
  5811. {
  5812. synchronized (this)
  5813. {
  5814. if (_triggerSkills == null)
  5815. {
  5816. _triggerSkills = new ConcurrentHashMap<>();
  5817. }
  5818. }
  5819. }
  5820. return _triggerSkills;
  5821. }
  5822. public void addTriggerSkill(OptionsSkillHolder holder)
  5823. {
  5824. getTriggerSkills().put(holder.getSkillId(), holder);
  5825. }
  5826. public void removeTriggerSkill(OptionsSkillHolder holder)
  5827. {
  5828. getTriggerSkills().remove(holder.getSkillId());
  5829. }
  5830. public void makeTriggerCast(Skill skill, L2Character target, boolean ignoreTargetType)
  5831. {
  5832. try
  5833. {
  5834. if ((skill == null))
  5835. {
  5836. return;
  5837. }
  5838. if (skill.checkCondition(this, target, false))
  5839. {
  5840. if (isSkillDisabled(skill))
  5841. {
  5842. return;
  5843. }
  5844. if (skill.getReuseDelay() > 0)
  5845. {
  5846. disableSkill(skill, skill.getReuseDelay());
  5847. }
  5848. // @formatter:off
  5849. final L2Object[] targets = !ignoreTargetType ? skill.getTargetList(this, false, target) : new L2Character[]{ target };
  5850. // @formatter:on
  5851. if (targets.length == 0)
  5852. {
  5853. return;
  5854. }
  5855. for (L2Object obj : targets)
  5856. {
  5857. if ((obj != null) && obj.isCharacter())
  5858. {
  5859. target = (L2Character) obj;
  5860. break;
  5861. }
  5862. }
  5863. if (Config.ALT_VALIDATE_TRIGGER_SKILLS && isPlayable() && (target != null) && target.isPlayable())
  5864. {
  5865. final L2PcInstance player = getActingPlayer();
  5866. if (!player.checkPvpSkill(target, skill))
  5867. {
  5868. return;
  5869. }
  5870. }
  5871. broadcastPacket(new MagicSkillUse(this, target, skill.getDisplayId(), skill.getLevel(), 0, 0));
  5872. broadcastPacket(new MagicSkillLaunched(this, skill.getDisplayId(), skill.getLevel(), targets));
  5873. // Launch the magic skill and calculate its effects
  5874. skill.activateSkill(this, targets);
  5875. }
  5876. }
  5877. catch (Exception e)
  5878. {
  5879. _log.log(Level.WARNING, "", e);
  5880. }
  5881. }
  5882. public void makeTriggerCast(Skill skill, L2Character target)
  5883. {
  5884. makeTriggerCast(skill, target, false);
  5885. }
  5886. /**
  5887. * Dummy method overriden in {@link L2PcInstance}
  5888. * @return {@code true} if current player can revive and shows 'To Village' button upon death, {@code false} otherwise.
  5889. */
  5890. public boolean canRevive()
  5891. {
  5892. return true;
  5893. }
  5894. /**
  5895. * Dummy method overriden in {@link L2PcInstance}
  5896. * @param val
  5897. */
  5898. public void setCanRevive(boolean val)
  5899. {
  5900. }
  5901. /**
  5902. * Dummy method overriden in {@link L2Attackable}
  5903. * @return {@code true} if there is a loot to sweep, {@code false} otherwise.
  5904. */
  5905. public boolean isSweepActive()
  5906. {
  5907. return false;
  5908. }
  5909. /**
  5910. * Dummy method overriden in {@link L2PcInstance}
  5911. * @return {@code true} if player is on event, {@code false} otherwise.
  5912. */
  5913. public boolean isOnEvent()
  5914. {
  5915. return false;
  5916. }
  5917. /**
  5918. * Dummy method overriden in {@link L2PcInstance}
  5919. * @return the clan id of current character.
  5920. */
  5921. public int getClanId()
  5922. {
  5923. return 0;
  5924. }
  5925. /**
  5926. * Dummy method overriden in {@link L2PcInstance}
  5927. * @return the clan of current character.
  5928. */
  5929. public L2Clan getClan()
  5930. {
  5931. return null;
  5932. }
  5933. /**
  5934. * Dummy method overriden in {@link L2PcInstance}
  5935. * @return {@code true} if player is in academy, {@code false} otherwise.
  5936. */
  5937. public boolean isAcademyMember()
  5938. {
  5939. return false;
  5940. }
  5941. /**
  5942. * Dummy method overriden in {@link L2PcInstance}
  5943. * @return the pledge type of current character.
  5944. */
  5945. public int getPledgeType()
  5946. {
  5947. return 0;
  5948. }
  5949. /**
  5950. * Dummy method overriden in {@link L2PcInstance}
  5951. * @return the alliance id of current character.
  5952. */
  5953. public int getAllyId()
  5954. {
  5955. return 0;
  5956. }
  5957. /**
  5958. * Notifies to listeners that current character received damage.
  5959. * @param damage
  5960. * @param attacker
  5961. * @param skill
  5962. * @param critical
  5963. * @param damageOverTime
  5964. */
  5965. public void notifyDamageReceived(double damage, L2Character attacker, Skill skill, boolean critical, boolean damageOverTime)
  5966. {
  5967. EventDispatcher.getInstance().notifyEventAsync(new OnCreatureDamageReceived(attacker, this, damage, skill, critical, damageOverTime), this);
  5968. EventDispatcher.getInstance().notifyEventAsync(new OnCreatureDamageDealt(attacker, this, damage, skill, critical, damageOverTime), attacker);
  5969. }
  5970. /**
  5971. * Notifies to listeners that current character avoid attack.
  5972. * @param target
  5973. * @param isDot
  5974. */
  5975. public void notifyAttackAvoid(final L2Character target, final boolean isDot)
  5976. {
  5977. EventDispatcher.getInstance().notifyEventAsync(new OnCreatureAttackAvoid(this, target, isDot), target);
  5978. }
  5979. /**
  5980. * @return {@link WeaponType} of current character's weapon or basic weapon type.
  5981. */
  5982. public final WeaponType getAttackType()
  5983. {
  5984. final L2Weapon weapon = getActiveWeaponItem();
  5985. if (weapon != null)
  5986. {
  5987. return weapon.getItemType();
  5988. }
  5989. else if (isTransformed())
  5990. {
  5991. final TransformTemplate template = getTransformation().getTemplate(getActingPlayer());
  5992. if (template != null)
  5993. {
  5994. return template.getBaseAttackType();
  5995. }
  5996. }
  5997. return getTemplate().getBaseAttackType();
  5998. }
  5999. public final boolean isInCategory(CategoryType type)
  6000. {
  6001. return CategoryData.getInstance().isInCategory(type, getId());
  6002. }
  6003. /**
  6004. * @return the character that summoned this NPC.
  6005. */
  6006. public L2Character getSummoner()
  6007. {
  6008. return _summoner;
  6009. }
  6010. /**
  6011. * @param summoner the summoner of this NPC.
  6012. */
  6013. public void setSummoner(L2Character summoner)
  6014. {
  6015. _summoner = summoner;
  6016. }
  6017. @Override
  6018. public boolean isCharacter()
  6019. {
  6020. return true;
  6021. }
  6022. /**
  6023. * @return {@code true} if current character is casting channeling skill, {@code false} otherwise.
  6024. */
  6025. public final boolean isChanneling()
  6026. {
  6027. return (_channelizer != null) && _channelizer.isChanneling();
  6028. }
  6029. public final SkillChannelizer getSkillChannelizer()
  6030. {
  6031. if (_channelizer == null)
  6032. {
  6033. _channelizer = new SkillChannelizer(this);
  6034. }
  6035. return _channelizer;
  6036. }
  6037. /**
  6038. * @return {@code true} if current character is affected by channeling skill, {@code false} otherwise.
  6039. */
  6040. public final boolean isChannelized()
  6041. {
  6042. return (_channelized != null) && !_channelized.isChannelized();
  6043. }
  6044. public final SkillChannelized getSkillChannelized()
  6045. {
  6046. if (_channelized == null)
  6047. {
  6048. _channelized = new SkillChannelized();
  6049. }
  6050. return _channelized;
  6051. }
  6052. public void addInvulAgainst(SkillHolder holder)
  6053. {
  6054. final InvulSkillHolder invulHolder = getInvulAgainstSkills().get(holder.getSkillId());
  6055. if (invulHolder != null)
  6056. {
  6057. invulHolder.increaseInstances();
  6058. return;
  6059. }
  6060. getInvulAgainstSkills().put(holder.getSkillId(), new InvulSkillHolder(holder));
  6061. }
  6062. public void removeInvulAgainst(SkillHolder holder)
  6063. {
  6064. final InvulSkillHolder invulHolder = getInvulAgainstSkills().get(holder.getSkillId());
  6065. if ((invulHolder != null) && (invulHolder.decreaseInstances() < 1))
  6066. {
  6067. getInvulAgainstSkills().remove(holder.getSkillId());
  6068. }
  6069. }
  6070. public boolean isInvulAgainst(int skillId, int skillLvl)
  6071. {
  6072. if (_invulAgainst != null)
  6073. {
  6074. final SkillHolder holder = getInvulAgainstSkills().get(skillId);
  6075. return ((holder != null) && ((holder.getSkillLvl() < 1) || (holder.getSkillLvl() == skillLvl)));
  6076. }
  6077. return false;
  6078. }
  6079. private Map<Integer, InvulSkillHolder> getInvulAgainstSkills()
  6080. {
  6081. if (_invulAgainst == null)
  6082. {
  6083. synchronized (this)
  6084. {
  6085. if (_invulAgainst == null)
  6086. {
  6087. _invulAgainst = new ConcurrentHashMap<>();
  6088. }
  6089. }
  6090. }
  6091. return _invulAgainst;
  6092. }
  6093. @Override
  6094. public Queue<AbstractEventListener> getListeners(EventType type)
  6095. {
  6096. final Queue<AbstractEventListener> objectListenres = super.getListeners(type);
  6097. final Queue<AbstractEventListener> templateListeners = getTemplate().getListeners(type);
  6098. final Queue<AbstractEventListener> globalListeners = isNpc() && !isMonster() ? Containers.Npcs().getListeners(type) : isMonster() ? Containers.Monsters().getListeners(type) : isPlayer() ? Containers.Players().getListeners(type) : EmptyQueue.emptyQueue();
  6099. // Attempt to do not create collection
  6100. if (objectListenres.isEmpty() && templateListeners.isEmpty() && globalListeners.isEmpty())
  6101. {
  6102. return EmptyQueue.emptyQueue();
  6103. }
  6104. else if (!objectListenres.isEmpty() && templateListeners.isEmpty() && globalListeners.isEmpty())
  6105. {
  6106. return objectListenres;
  6107. }
  6108. else if (!templateListeners.isEmpty() && objectListenres.isEmpty() && globalListeners.isEmpty())
  6109. {
  6110. return templateListeners;
  6111. }
  6112. else if (!globalListeners.isEmpty() && objectListenres.isEmpty() && templateListeners.isEmpty())
  6113. {
  6114. return globalListeners;
  6115. }
  6116. final Queue<AbstractEventListener> both = new LinkedBlockingDeque<>(objectListenres.size() + templateListeners.size() + globalListeners.size());
  6117. both.addAll(objectListenres);
  6118. both.addAll(templateListeners);
  6119. both.addAll(globalListeners);
  6120. return both;
  6121. }
  6122. public Race getRace()
  6123. {
  6124. return getTemplate().getRace();
  6125. }
  6126. public boolean isInDuel()
  6127. {
  6128. return false;
  6129. }
  6130. public int getDuelId()
  6131. {
  6132. return 0;
  6133. }
  6134. public byte getSiegeState()
  6135. {
  6136. return 0;
  6137. }
  6138. public int getSiegeSide()
  6139. {
  6140. return 0;
  6141. }
  6142. }