Formulas.java 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  1. /*
  2. * Copyright (C) 2004-2014 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.stats;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.SevenSigns;
  25. import com.l2jserver.gameserver.SevenSignsFestival;
  26. import com.l2jserver.gameserver.datatables.HitConditionBonus;
  27. import com.l2jserver.gameserver.datatables.KarmaData;
  28. import com.l2jserver.gameserver.enums.ShotType;
  29. import com.l2jserver.gameserver.instancemanager.CastleManager;
  30. import com.l2jserver.gameserver.instancemanager.ClanHallManager;
  31. import com.l2jserver.gameserver.instancemanager.FortManager;
  32. import com.l2jserver.gameserver.instancemanager.SiegeManager;
  33. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  34. import com.l2jserver.gameserver.model.L2SiegeClan;
  35. import com.l2jserver.gameserver.model.StatsSet;
  36. import com.l2jserver.gameserver.model.actor.L2Attackable;
  37. import com.l2jserver.gameserver.model.actor.L2Character;
  38. import com.l2jserver.gameserver.model.actor.instance.L2CubicInstance;
  39. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  40. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  41. import com.l2jserver.gameserver.model.actor.instance.L2SiegeFlagInstance;
  42. import com.l2jserver.gameserver.model.actor.instance.L2StaticObjectInstance;
  43. import com.l2jserver.gameserver.model.effects.L2EffectType;
  44. import com.l2jserver.gameserver.model.entity.Castle;
  45. import com.l2jserver.gameserver.model.entity.ClanHall;
  46. import com.l2jserver.gameserver.model.entity.Fort;
  47. import com.l2jserver.gameserver.model.entity.Siege;
  48. import com.l2jserver.gameserver.model.items.L2Armor;
  49. import com.l2jserver.gameserver.model.items.L2Item;
  50. import com.l2jserver.gameserver.model.items.L2Weapon;
  51. import com.l2jserver.gameserver.model.items.type.ArmorType;
  52. import com.l2jserver.gameserver.model.items.type.WeaponType;
  53. import com.l2jserver.gameserver.model.skills.BuffInfo;
  54. import com.l2jserver.gameserver.model.skills.Skill;
  55. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncArmorSet;
  56. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncAtkAccuracy;
  57. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncAtkCritical;
  58. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncAtkEvasion;
  59. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncGatesMDefMod;
  60. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncGatesPDefMod;
  61. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncHenna;
  62. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncMAtkCritical;
  63. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncMAtkMod;
  64. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncMAtkSpeed;
  65. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncMDefMod;
  66. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncMaxCpMul;
  67. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncMaxHpMul;
  68. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncMaxMpMul;
  69. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncMoveSpeed;
  70. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncPAtkMod;
  71. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncPAtkSpeed;
  72. import com.l2jserver.gameserver.model.stats.functions.formulas.FuncPDefMod;
  73. import com.l2jserver.gameserver.model.zone.ZoneId;
  74. import com.l2jserver.gameserver.model.zone.type.L2CastleZone;
  75. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  76. import com.l2jserver.gameserver.model.zone.type.L2FortZone;
  77. import com.l2jserver.gameserver.model.zone.type.L2MotherTreeZone;
  78. import com.l2jserver.gameserver.network.Debug;
  79. import com.l2jserver.gameserver.network.SystemMessageId;
  80. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  81. import com.l2jserver.gameserver.util.Util;
  82. import com.l2jserver.util.Rnd;
  83. /**
  84. * Global calculations.
  85. */
  86. public final class Formulas
  87. {
  88. private static final Logger _log = Logger.getLogger(Formulas.class.getName());
  89. /** Regeneration Task period. */
  90. private static final int HP_REGENERATE_PERIOD = 3000; // 3 secs
  91. public static final byte SHIELD_DEFENSE_FAILED = 0; // no shield defense
  92. public static final byte SHIELD_DEFENSE_SUCCEED = 1; // normal shield defense
  93. public static final byte SHIELD_DEFENSE_PERFECT_BLOCK = 2; // perfect block
  94. private static final byte MELEE_ATTACK_RANGE = 40;
  95. /**
  96. * Return the period between 2 regeneration task (3s for L2Character, 5 min for L2DoorInstance).
  97. * @param cha
  98. * @return
  99. */
  100. public static int getRegeneratePeriod(L2Character cha)
  101. {
  102. return cha.isDoor() ? HP_REGENERATE_PERIOD * 100 : HP_REGENERATE_PERIOD;
  103. }
  104. /**
  105. * Return the standard NPC Calculator set containing ACCURACY_COMBAT and EVASION_RATE.<br>
  106. * <B><U>Concept</U>:</B><br>
  107. * A calculator is created to manage and dynamically calculate the effect of a character property (ex : MAX_HP, REGENERATE_HP_RATE...). In fact, each calculator is a table of Func object in which each Func represents a mathematic function : <br>
  108. * FuncAtkAccuracy -> Math.sqrt(_player.getDEX())*6+_player.getLevel()<br>
  109. * To reduce cache memory use, L2NPCInstances who don't have skills share the same Calculator set called <B>NPC_STD_CALCULATOR</B>.<br>
  110. * @return
  111. */
  112. public static Calculator[] getStdNPCCalculators()
  113. {
  114. Calculator[] std = new Calculator[Stats.NUM_STATS];
  115. std[Stats.MAX_HP.ordinal()] = new Calculator();
  116. std[Stats.MAX_HP.ordinal()].addFunc(FuncMaxHpMul.getInstance());
  117. std[Stats.MAX_MP.ordinal()] = new Calculator();
  118. std[Stats.MAX_MP.ordinal()].addFunc(FuncMaxMpMul.getInstance());
  119. std[Stats.POWER_ATTACK.ordinal()] = new Calculator();
  120. std[Stats.POWER_ATTACK.ordinal()].addFunc(FuncPAtkMod.getInstance());
  121. std[Stats.MAGIC_ATTACK.ordinal()] = new Calculator();
  122. std[Stats.MAGIC_ATTACK.ordinal()].addFunc(FuncMAtkMod.getInstance());
  123. std[Stats.POWER_DEFENCE.ordinal()] = new Calculator();
  124. std[Stats.POWER_DEFENCE.ordinal()].addFunc(FuncPDefMod.getInstance());
  125. std[Stats.MAGIC_DEFENCE.ordinal()] = new Calculator();
  126. std[Stats.MAGIC_DEFENCE.ordinal()].addFunc(FuncMDefMod.getInstance());
  127. std[Stats.CRITICAL_RATE.ordinal()] = new Calculator();
  128. std[Stats.CRITICAL_RATE.ordinal()].addFunc(FuncAtkCritical.getInstance());
  129. std[Stats.MCRITICAL_RATE.ordinal()] = new Calculator();
  130. std[Stats.MCRITICAL_RATE.ordinal()].addFunc(FuncMAtkCritical.getInstance());
  131. std[Stats.ACCURACY_COMBAT.ordinal()] = new Calculator();
  132. std[Stats.ACCURACY_COMBAT.ordinal()].addFunc(FuncAtkAccuracy.getInstance());
  133. std[Stats.EVASION_RATE.ordinal()] = new Calculator();
  134. std[Stats.EVASION_RATE.ordinal()].addFunc(FuncAtkEvasion.getInstance());
  135. std[Stats.POWER_ATTACK_SPEED.ordinal()] = new Calculator();
  136. std[Stats.POWER_ATTACK_SPEED.ordinal()].addFunc(FuncPAtkSpeed.getInstance());
  137. std[Stats.MAGIC_ATTACK_SPEED.ordinal()] = new Calculator();
  138. std[Stats.MAGIC_ATTACK_SPEED.ordinal()].addFunc(FuncMAtkSpeed.getInstance());
  139. std[Stats.MOVE_SPEED.ordinal()] = new Calculator();
  140. std[Stats.MOVE_SPEED.ordinal()].addFunc(FuncMoveSpeed.getInstance());
  141. return std;
  142. }
  143. public static Calculator[] getStdDoorCalculators()
  144. {
  145. Calculator[] std = new Calculator[Stats.NUM_STATS];
  146. // Add the FuncAtkAccuracy to the Standard Calculator of ACCURACY_COMBAT
  147. std[Stats.ACCURACY_COMBAT.ordinal()] = new Calculator();
  148. std[Stats.ACCURACY_COMBAT.ordinal()].addFunc(FuncAtkAccuracy.getInstance());
  149. // Add the FuncAtkEvasion to the Standard Calculator of EVASION_RATE
  150. std[Stats.EVASION_RATE.ordinal()] = new Calculator();
  151. std[Stats.EVASION_RATE.ordinal()].addFunc(FuncAtkEvasion.getInstance());
  152. // SevenSigns PDEF Modifier
  153. std[Stats.POWER_DEFENCE.ordinal()] = new Calculator();
  154. std[Stats.POWER_DEFENCE.ordinal()].addFunc(FuncGatesPDefMod.getInstance());
  155. // SevenSigns MDEF Modifier
  156. std[Stats.MAGIC_DEFENCE.ordinal()] = new Calculator();
  157. std[Stats.MAGIC_DEFENCE.ordinal()].addFunc(FuncGatesMDefMod.getInstance());
  158. return std;
  159. }
  160. /**
  161. * Add basics Func objects to L2PcInstance and L2Summon.<br>
  162. * <B><U>Concept</U>:</B><br>
  163. * A calculator is created to manage and dynamically calculate the effect of a character property (ex : MAX_HP, REGENERATE_HP_RATE...). In fact, each calculator is a table of Func object in which each Func represents a mathematic function : <br>
  164. * FuncAtkAccuracy -> Math.sqrt(_player.getDEX())*6+_player.getLevel()<br>
  165. * @param cha L2PcInstance or L2Summon that must obtain basic Func objects
  166. */
  167. public static void addFuncsToNewCharacter(L2Character cha)
  168. {
  169. if (cha.isPlayer())
  170. {
  171. cha.addStatFunc(FuncMaxHpMul.getInstance());
  172. cha.addStatFunc(FuncMaxCpMul.getInstance());
  173. cha.addStatFunc(FuncMaxMpMul.getInstance());
  174. cha.addStatFunc(FuncPAtkMod.getInstance());
  175. cha.addStatFunc(FuncMAtkMod.getInstance());
  176. cha.addStatFunc(FuncPDefMod.getInstance());
  177. cha.addStatFunc(FuncMDefMod.getInstance());
  178. cha.addStatFunc(FuncAtkCritical.getInstance());
  179. cha.addStatFunc(FuncMAtkCritical.getInstance());
  180. cha.addStatFunc(FuncAtkAccuracy.getInstance());
  181. cha.addStatFunc(FuncAtkEvasion.getInstance());
  182. cha.addStatFunc(FuncPAtkSpeed.getInstance());
  183. cha.addStatFunc(FuncMAtkSpeed.getInstance());
  184. cha.addStatFunc(FuncMoveSpeed.getInstance());
  185. cha.addStatFunc(FuncHenna.getInstance(Stats.STAT_STR));
  186. cha.addStatFunc(FuncHenna.getInstance(Stats.STAT_DEX));
  187. cha.addStatFunc(FuncHenna.getInstance(Stats.STAT_INT));
  188. cha.addStatFunc(FuncHenna.getInstance(Stats.STAT_MEN));
  189. cha.addStatFunc(FuncHenna.getInstance(Stats.STAT_CON));
  190. cha.addStatFunc(FuncHenna.getInstance(Stats.STAT_WIT));
  191. cha.addStatFunc(FuncArmorSet.getInstance(Stats.STAT_STR));
  192. cha.addStatFunc(FuncArmorSet.getInstance(Stats.STAT_DEX));
  193. cha.addStatFunc(FuncArmorSet.getInstance(Stats.STAT_INT));
  194. cha.addStatFunc(FuncArmorSet.getInstance(Stats.STAT_MEN));
  195. cha.addStatFunc(FuncArmorSet.getInstance(Stats.STAT_CON));
  196. cha.addStatFunc(FuncArmorSet.getInstance(Stats.STAT_WIT));
  197. }
  198. else if (cha.isSummon())
  199. {
  200. cha.addStatFunc(FuncMaxHpMul.getInstance());
  201. cha.addStatFunc(FuncMaxMpMul.getInstance());
  202. cha.addStatFunc(FuncPAtkMod.getInstance());
  203. cha.addStatFunc(FuncMAtkMod.getInstance());
  204. cha.addStatFunc(FuncPDefMod.getInstance());
  205. cha.addStatFunc(FuncMDefMod.getInstance());
  206. cha.addStatFunc(FuncAtkCritical.getInstance());
  207. cha.addStatFunc(FuncMAtkCritical.getInstance());
  208. cha.addStatFunc(FuncAtkAccuracy.getInstance());
  209. cha.addStatFunc(FuncAtkEvasion.getInstance());
  210. cha.addStatFunc(FuncMoveSpeed.getInstance());
  211. cha.addStatFunc(FuncPAtkSpeed.getInstance());
  212. cha.addStatFunc(FuncMAtkSpeed.getInstance());
  213. }
  214. }
  215. /**
  216. * Calculate the HP regen rate (base + modifiers).
  217. * @param cha
  218. * @return
  219. */
  220. public static final double calcHpRegen(L2Character cha)
  221. {
  222. double init = cha.isPlayer() ? cha.getActingPlayer().getTemplate().getBaseHpRegen(cha.getLevel()) : cha.getTemplate().getBaseHpReg();
  223. double hpRegenMultiplier = cha.isRaid() ? Config.RAID_HP_REGEN_MULTIPLIER : Config.HP_REGEN_MULTIPLIER;
  224. double hpRegenBonus = 0;
  225. if (Config.L2JMOD_CHAMPION_ENABLE && cha.isChampion())
  226. {
  227. hpRegenMultiplier *= Config.L2JMOD_CHAMPION_HP_REGEN;
  228. }
  229. if (cha.isPlayer())
  230. {
  231. L2PcInstance player = cha.getActingPlayer();
  232. // SevenSigns Festival modifier
  233. if (SevenSignsFestival.getInstance().isFestivalInProgress() && player.isFestivalParticipant())
  234. {
  235. hpRegenMultiplier *= calcFestivalRegenModifier(player);
  236. }
  237. else
  238. {
  239. double siegeModifier = calcSiegeRegenModifier(player);
  240. if (siegeModifier > 0)
  241. {
  242. hpRegenMultiplier *= siegeModifier;
  243. }
  244. }
  245. if (player.isInsideZone(ZoneId.CLAN_HALL) && (player.getClan() != null) && (player.getClan().getHideoutId() > 0))
  246. {
  247. L2ClanHallZone zone = ZoneManager.getInstance().getZone(player, L2ClanHallZone.class);
  248. int posChIndex = zone == null ? -1 : zone.getResidenceId();
  249. int clanHallIndex = player.getClan().getHideoutId();
  250. if ((clanHallIndex > 0) && (clanHallIndex == posChIndex))
  251. {
  252. ClanHall clansHall = ClanHallManager.getInstance().getClanHallById(clanHallIndex);
  253. if (clansHall != null)
  254. {
  255. if (clansHall.getFunction(ClanHall.FUNC_RESTORE_HP) != null)
  256. {
  257. hpRegenMultiplier *= 1 + ((double) clansHall.getFunction(ClanHall.FUNC_RESTORE_HP).getLvl() / 100);
  258. }
  259. }
  260. }
  261. }
  262. if (player.isInsideZone(ZoneId.CASTLE) && (player.getClan() != null) && (player.getClan().getCastleId() > 0))
  263. {
  264. L2CastleZone zone = ZoneManager.getInstance().getZone(player, L2CastleZone.class);
  265. int posCastleIndex = zone == null ? -1 : zone.getResidenceId();
  266. int castleIndex = player.getClan().getCastleId();
  267. if ((castleIndex > 0) && (castleIndex == posCastleIndex))
  268. {
  269. Castle castle = CastleManager.getInstance().getCastleById(castleIndex);
  270. if (castle != null)
  271. {
  272. if (castle.getFunction(Castle.FUNC_RESTORE_HP) != null)
  273. {
  274. hpRegenMultiplier *= 1 + ((double) castle.getFunction(Castle.FUNC_RESTORE_HP).getLvl() / 100);
  275. }
  276. }
  277. }
  278. }
  279. if (player.isInsideZone(ZoneId.FORT) && (player.getClan() != null) && (player.getClan().getFortId() > 0))
  280. {
  281. L2FortZone zone = ZoneManager.getInstance().getZone(player, L2FortZone.class);
  282. int posFortIndex = zone == null ? -1 : zone.getResidenceId();
  283. int fortIndex = player.getClan().getFortId();
  284. if ((fortIndex > 0) && (fortIndex == posFortIndex))
  285. {
  286. Fort fort = FortManager.getInstance().getFortById(fortIndex);
  287. if (fort != null)
  288. {
  289. if (fort.getFunction(Fort.FUNC_RESTORE_HP) != null)
  290. {
  291. hpRegenMultiplier *= 1 + ((double) fort.getFunction(Fort.FUNC_RESTORE_HP).getLvl() / 100);
  292. }
  293. }
  294. }
  295. }
  296. // Mother Tree effect is calculated at last
  297. if (player.isInsideZone(ZoneId.MOTHER_TREE))
  298. {
  299. L2MotherTreeZone zone = ZoneManager.getInstance().getZone(player, L2MotherTreeZone.class);
  300. int hpBonus = zone == null ? 0 : zone.getHpRegenBonus();
  301. hpRegenBonus += hpBonus;
  302. }
  303. // Calculate Movement bonus
  304. if (player.isSitting())
  305. {
  306. hpRegenMultiplier *= 1.5; // Sitting
  307. }
  308. else if (!player.isMoving())
  309. {
  310. hpRegenMultiplier *= 1.1; // Staying
  311. }
  312. else if (player.isRunning())
  313. {
  314. hpRegenMultiplier *= 0.7; // Running
  315. }
  316. // Add CON bonus
  317. init *= cha.getLevelMod() * BaseStats.CON.calcBonus(cha);
  318. }
  319. else if (cha.isPet())
  320. {
  321. init = ((L2PetInstance) cha).getPetLevelData().getPetRegenHP() * Config.PET_HP_REGEN_MULTIPLIER;
  322. }
  323. return (cha.calcStat(Stats.REGENERATE_HP_RATE, Math.max(1, init), null, null) * hpRegenMultiplier) + hpRegenBonus;
  324. }
  325. /**
  326. * Calculate the MP regen rate (base + modifiers).
  327. * @param cha
  328. * @return
  329. */
  330. public static final double calcMpRegen(L2Character cha)
  331. {
  332. double init = cha.isPlayer() ? cha.getActingPlayer().getTemplate().getBaseMpRegen(cha.getLevel()) : cha.getTemplate().getBaseMpReg();
  333. double mpRegenMultiplier = cha.isRaid() ? Config.RAID_MP_REGEN_MULTIPLIER : Config.MP_REGEN_MULTIPLIER;
  334. double mpRegenBonus = 0;
  335. if (cha.isPlayer())
  336. {
  337. L2PcInstance player = cha.getActingPlayer();
  338. // SevenSigns Festival modifier
  339. if (SevenSignsFestival.getInstance().isFestivalInProgress() && player.isFestivalParticipant())
  340. {
  341. mpRegenMultiplier *= calcFestivalRegenModifier(player);
  342. }
  343. // Mother Tree effect is calculated at last'
  344. if (player.isInsideZone(ZoneId.MOTHER_TREE))
  345. {
  346. L2MotherTreeZone zone = ZoneManager.getInstance().getZone(player, L2MotherTreeZone.class);
  347. int mpBonus = zone == null ? 0 : zone.getMpRegenBonus();
  348. mpRegenBonus += mpBonus;
  349. }
  350. if (player.isInsideZone(ZoneId.CLAN_HALL) && (player.getClan() != null) && (player.getClan().getHideoutId() > 0))
  351. {
  352. L2ClanHallZone zone = ZoneManager.getInstance().getZone(player, L2ClanHallZone.class);
  353. int posChIndex = zone == null ? -1 : zone.getResidenceId();
  354. int clanHallIndex = player.getClan().getHideoutId();
  355. if ((clanHallIndex > 0) && (clanHallIndex == posChIndex))
  356. {
  357. ClanHall clansHall = ClanHallManager.getInstance().getClanHallById(clanHallIndex);
  358. if (clansHall != null)
  359. {
  360. if (clansHall.getFunction(ClanHall.FUNC_RESTORE_MP) != null)
  361. {
  362. mpRegenMultiplier *= 1 + ((double) clansHall.getFunction(ClanHall.FUNC_RESTORE_MP).getLvl() / 100);
  363. }
  364. }
  365. }
  366. }
  367. if (player.isInsideZone(ZoneId.CASTLE) && (player.getClan() != null) && (player.getClan().getCastleId() > 0))
  368. {
  369. L2CastleZone zone = ZoneManager.getInstance().getZone(player, L2CastleZone.class);
  370. int posCastleIndex = zone == null ? -1 : zone.getResidenceId();
  371. int castleIndex = player.getClan().getCastleId();
  372. if ((castleIndex > 0) && (castleIndex == posCastleIndex))
  373. {
  374. Castle castle = CastleManager.getInstance().getCastleById(castleIndex);
  375. if (castle != null)
  376. {
  377. if (castle.getFunction(Castle.FUNC_RESTORE_MP) != null)
  378. {
  379. mpRegenMultiplier *= 1 + ((double) castle.getFunction(Castle.FUNC_RESTORE_MP).getLvl() / 100);
  380. }
  381. }
  382. }
  383. }
  384. if (player.isInsideZone(ZoneId.FORT) && (player.getClan() != null) && (player.getClan().getFortId() > 0))
  385. {
  386. L2FortZone zone = ZoneManager.getInstance().getZone(player, L2FortZone.class);
  387. int posFortIndex = zone == null ? -1 : zone.getResidenceId();
  388. int fortIndex = player.getClan().getFortId();
  389. if ((fortIndex > 0) && (fortIndex == posFortIndex))
  390. {
  391. Fort fort = FortManager.getInstance().getFortById(fortIndex);
  392. if (fort != null)
  393. {
  394. if (fort.getFunction(Fort.FUNC_RESTORE_MP) != null)
  395. {
  396. mpRegenMultiplier *= 1 + ((double) fort.getFunction(Fort.FUNC_RESTORE_MP).getLvl() / 100);
  397. }
  398. }
  399. }
  400. }
  401. // Calculate Movement bonus
  402. if (player.isSitting())
  403. {
  404. mpRegenMultiplier *= 1.5; // Sitting
  405. }
  406. else if (!player.isMoving())
  407. {
  408. mpRegenMultiplier *= 1.1; // Staying
  409. }
  410. else if (player.isRunning())
  411. {
  412. mpRegenMultiplier *= 0.7; // Running
  413. }
  414. // Add MEN bonus
  415. init *= cha.getLevelMod() * BaseStats.MEN.calcBonus(cha);
  416. }
  417. else if (cha.isPet())
  418. {
  419. init = ((L2PetInstance) cha).getPetLevelData().getPetRegenMP() * Config.PET_MP_REGEN_MULTIPLIER;
  420. }
  421. return (cha.calcStat(Stats.REGENERATE_MP_RATE, Math.max(1, init), null, null) * mpRegenMultiplier) + mpRegenBonus;
  422. }
  423. /**
  424. * Calculate the CP regen rate (base + modifiers).
  425. * @param cha
  426. * @return
  427. */
  428. public static final double calcCpRegen(L2Character cha)
  429. {
  430. double init = cha.isPlayer() ? cha.getActingPlayer().getTemplate().getBaseCpRegen(cha.getLevel()) : cha.getTemplate().getBaseHpReg();
  431. double cpRegenMultiplier = Config.CP_REGEN_MULTIPLIER;
  432. double cpRegenBonus = 0;
  433. if (cha.isPlayer())
  434. {
  435. L2PcInstance player = cha.getActingPlayer();
  436. // Calculate Movement bonus
  437. if (player.isSitting())
  438. {
  439. cpRegenMultiplier *= 1.5; // Sitting
  440. }
  441. else if (!player.isMoving())
  442. {
  443. cpRegenMultiplier *= 1.1; // Staying
  444. }
  445. else if (player.isRunning())
  446. {
  447. cpRegenMultiplier *= 0.7; // Running
  448. }
  449. }
  450. else
  451. {
  452. // Calculate Movement bonus
  453. if (!cha.isMoving())
  454. {
  455. cpRegenMultiplier *= 1.1; // Staying
  456. }
  457. else if (cha.isRunning())
  458. {
  459. cpRegenMultiplier *= 0.7; // Running
  460. }
  461. }
  462. // Apply CON bonus
  463. init *= cha.getLevelMod() * BaseStats.CON.calcBonus(cha);
  464. return (cha.calcStat(Stats.REGENERATE_CP_RATE, Math.max(1, init), null, null) * cpRegenMultiplier) + cpRegenBonus;
  465. }
  466. public static final double calcFestivalRegenModifier(L2PcInstance activeChar)
  467. {
  468. final int[] festivalInfo = SevenSignsFestival.getInstance().getFestivalForPlayer(activeChar);
  469. final int oracle = festivalInfo[0];
  470. final int festivalId = festivalInfo[1];
  471. int[] festivalCenter;
  472. // If the player isn't found in the festival, leave the regen rate as it is.
  473. if (festivalId < 0)
  474. {
  475. return 0;
  476. }
  477. // Retrieve the X and Y coords for the center of the festival arena the player is in.
  478. if (oracle == SevenSigns.CABAL_DAWN)
  479. {
  480. festivalCenter = SevenSignsFestival.FESTIVAL_DAWN_PLAYER_SPAWNS[festivalId];
  481. }
  482. else
  483. {
  484. festivalCenter = SevenSignsFestival.FESTIVAL_DUSK_PLAYER_SPAWNS[festivalId];
  485. }
  486. // Check the distance between the player and the player spawn point, in the center of the arena.
  487. double distToCenter = activeChar.calculateDistance(festivalCenter[0], festivalCenter[1], 0, false, false);
  488. if (Config.DEBUG)
  489. {
  490. _log.info("Distance: " + distToCenter + ", RegenMulti: " + ((distToCenter * 2.5) / 50));
  491. }
  492. return 1.0 - (distToCenter * 0.0005); // Maximum Decreased Regen of ~ -65%;
  493. }
  494. public static final double calcSiegeRegenModifier(L2PcInstance activeChar)
  495. {
  496. if ((activeChar == null) || (activeChar.getClan() == null))
  497. {
  498. return 0;
  499. }
  500. Siege siege = SiegeManager.getInstance().getSiege(activeChar.getX(), activeChar.getY(), activeChar.getZ());
  501. if ((siege == null) || !siege.isInProgress())
  502. {
  503. return 0;
  504. }
  505. L2SiegeClan siegeClan = siege.getAttackerClan(activeChar.getClan().getId());
  506. if ((siegeClan == null) || siegeClan.getFlag().isEmpty() || !Util.checkIfInRange(200, activeChar, siegeClan.getFlag().get(0), true))
  507. {
  508. return 0;
  509. }
  510. return 1.5; // If all is true, then modifier will be 50% more
  511. }
  512. public static double calcBlowDamage(L2Character attacker, L2Character target, Skill skill, byte shld, boolean ss)
  513. {
  514. double defence = target.getPDef(attacker);
  515. switch (shld)
  516. {
  517. case Formulas.SHIELD_DEFENSE_SUCCEED:
  518. {
  519. defence += target.getShldDef();
  520. break;
  521. }
  522. case Formulas.SHIELD_DEFENSE_PERFECT_BLOCK: // perfect block
  523. {
  524. return 1;
  525. }
  526. }
  527. final boolean isPvP = attacker.isPlayable() && target.isPlayable();
  528. final boolean isPvE = attacker.isPlayable() && target.isAttackable();
  529. double power = skill.getPower(isPvP, isPvE);
  530. double damage = 0;
  531. double proximityBonus = attacker.isBehindTarget() ? 1.2 : attacker.isInFrontOfTarget() ? 1 : 1.1; // Behind: +20% - Side: +10% (TODO: values are unconfirmed, possibly custom, remove or update when confirmed);
  532. double ssboost = ss ? 2 : 1;
  533. double pvpBonus = 1;
  534. if (isPvP)
  535. {
  536. // Damage bonuses in PvP fight
  537. pvpBonus = attacker.calcStat(Stats.PVP_PHYS_SKILL_DMG, 1, null, null);
  538. // Defense bonuses in PvP fight
  539. defence *= target.calcStat(Stats.PVP_PHYS_SKILL_DEF, 1, null, null);
  540. }
  541. // Initial damage
  542. double baseMod = ((77 * (power + (attacker.getPAtk(target) * ssboost))) / defence);
  543. // Critical
  544. double criticalMod = (attacker.calcStat(Stats.CRITICAL_DAMAGE, 1, target, skill));
  545. double criticalVulnMod = (target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE, 1, target, skill));
  546. double criticalAddMod = ((attacker.getStat().calcStat(Stats.CRITICAL_DAMAGE_ADD, 0) * 6.1 * 77) / defence);
  547. double criticalAddVuln = target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE_ADD, 0, target, skill);
  548. // Trait, elements
  549. double weaponTraitMod = calcWeaponTraitBonus(attacker, target);
  550. double generalTraitMod = calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false);
  551. double attributeMod = calcAttributeBonus(attacker, target, skill);
  552. double weaponMod = attacker.getRandomDamageMultiplier();
  553. double penaltyMod = 1;
  554. if ((target instanceof L2Attackable) && !target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 2))
  555. {
  556. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 1;
  557. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  558. {
  559. penaltyMod *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  560. }
  561. else
  562. {
  563. penaltyMod *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  564. }
  565. }
  566. damage = (baseMod * criticalMod * criticalVulnMod * proximityBonus * pvpBonus) + criticalAddMod + criticalAddVuln;
  567. damage *= weaponTraitMod;
  568. damage *= generalTraitMod;
  569. damage *= attributeMod;
  570. damage *= weaponMod;
  571. damage *= penaltyMod;
  572. if (attacker.isDebug())
  573. {
  574. final StatsSet set = new StatsSet();
  575. set.set("skillPower", skill.getPower(isPvP, isPvE));
  576. set.set("ssboost", ssboost);
  577. set.set("proximityBonus", proximityBonus);
  578. set.set("pvpBonus", pvpBonus);
  579. set.set("baseMod", baseMod);
  580. set.set("criticalMod", criticalMod);
  581. set.set("criticalVulnMod", criticalVulnMod);
  582. set.set("criticalAddMod", criticalAddMod);
  583. set.set("criticalAddVuln", criticalAddVuln);
  584. set.set("weaponTraitMod", weaponTraitMod);
  585. set.set("generalTraitMod", generalTraitMod);
  586. set.set("attributeMod", attributeMod);
  587. set.set("weaponMod", weaponMod);
  588. set.set("penaltyMod", penaltyMod);
  589. set.set("damage", (int) damage);
  590. Debug.sendSkillDebug(attacker, target, skill, set);
  591. }
  592. return Math.max(damage, 1);
  593. }
  594. public static double calcBackstabDamage(L2Character attacker, L2Character target, Skill skill, byte shld, boolean ss)
  595. {
  596. double defence = target.getPDef(attacker);
  597. switch (shld)
  598. {
  599. case Formulas.SHIELD_DEFENSE_SUCCEED:
  600. {
  601. defence += target.getShldDef();
  602. break;
  603. }
  604. case Formulas.SHIELD_DEFENSE_PERFECT_BLOCK: // perfect block
  605. {
  606. return 1;
  607. }
  608. }
  609. boolean isPvP = attacker.isPlayable() && target.isPlayer();
  610. boolean isPvE = attacker.isPlayable() && target.isAttackable();
  611. double damage = 0;
  612. double proximityBonus = attacker.isBehindTarget() ? 1.2 : attacker.isInFrontOfTarget() ? 1 : 1.1; // Behind: +20% - Side: +10% (TODO: values are unconfirmed, possibly custom, remove or update when confirmed)
  613. double ssboost = ss ? 2 : 1;
  614. double pvpBonus = 1;
  615. if (isPvP)
  616. {
  617. // Damage bonuses in PvP fight
  618. pvpBonus = attacker.calcStat(Stats.PVP_PHYS_SKILL_DMG, 1, null, null);
  619. // Defense bonuses in PvP fight
  620. defence *= target.calcStat(Stats.PVP_PHYS_SKILL_DEF, 1, null, null);
  621. }
  622. // Initial damage
  623. double baseMod = ((77 * (skill.getPower(isPvP, isPvE) + attacker.getPAtk(target))) / defence) * ssboost;
  624. // Critical
  625. double criticalMod = (attacker.calcStat(Stats.CRITICAL_DAMAGE, 1, target, skill));
  626. double criticalVulnMod = (target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE, 1, target, skill));
  627. double criticalAddMod = ((attacker.calcStat(Stats.CRITICAL_DAMAGE_ADD, 0, target, skill) * 6.1 * 77) / defence);
  628. double criticalAddVuln = target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE_ADD, 0, target, skill);
  629. // Trait, elements
  630. double generalTraitMod = calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false);
  631. double attributeMod = calcAttributeBonus(attacker, target, skill);
  632. double weaponMod = attacker.getRandomDamageMultiplier();
  633. double penaltyMod = 1;
  634. if (target.isAttackable() && !target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 2))
  635. {
  636. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 1;
  637. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  638. {
  639. penaltyMod *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  640. }
  641. else
  642. {
  643. penaltyMod *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  644. }
  645. }
  646. damage = (baseMod * criticalMod * criticalVulnMod * proximityBonus * pvpBonus) + criticalAddMod + criticalAddVuln;
  647. damage *= generalTraitMod;
  648. damage *= attributeMod;
  649. damage *= weaponMod;
  650. damage *= penaltyMod;
  651. if (attacker.isDebug())
  652. {
  653. final StatsSet set = new StatsSet();
  654. set.set("skillPower", skill.getPower(isPvP, isPvE));
  655. set.set("ssboost", ssboost);
  656. set.set("proximityBonus", proximityBonus);
  657. set.set("pvpBonus", pvpBonus);
  658. set.set("baseMod", baseMod);
  659. set.set("criticalMod", criticalMod);
  660. set.set("criticalVulnMod", criticalVulnMod);
  661. set.set("criticalAddMod", criticalAddMod);
  662. set.set("criticalAddVuln", criticalAddVuln);
  663. set.set("generalTraitMod", generalTraitMod);
  664. set.set("attributeMod", attributeMod);
  665. set.set("weaponMod", weaponMod);
  666. set.set("penaltyMod", penaltyMod);
  667. set.set("damage", (int) damage);
  668. Debug.sendSkillDebug(attacker, target, skill, set);
  669. }
  670. return Math.max(damage, 1);
  671. }
  672. /**
  673. * Calculated damage caused by ATTACK of attacker on target.
  674. * @param attacker player or NPC that makes ATTACK
  675. * @param target player or NPC, target of ATTACK
  676. * @param skill
  677. * @param shld
  678. * @param crit if the ATTACK have critical success
  679. * @param ss if weapon item was charged by soulshot
  680. * @return
  681. */
  682. public static final double calcPhysDam(L2Character attacker, L2Character target, Skill skill, byte shld, boolean crit, boolean ss)
  683. {
  684. final boolean isPvP = attacker.isPlayable() && target.isPlayable();
  685. final boolean isPvE = attacker.isPlayable() && target.isAttackable();
  686. double damage = attacker.getPAtk(target);
  687. double defence = target.getPDef(attacker);
  688. // Defense bonuses in PvP fight
  689. if (isPvP)
  690. {
  691. defence *= (skill == null) ? target.calcStat(Stats.PVP_PHYSICAL_DEF, 1, null, null) : target.calcStat(Stats.PVP_PHYS_SKILL_DEF, 1, null, null);
  692. }
  693. switch (shld)
  694. {
  695. case SHIELD_DEFENSE_SUCCEED:
  696. {
  697. if (!Config.ALT_GAME_SHIELD_BLOCKS)
  698. {
  699. defence += target.getShldDef();
  700. }
  701. break;
  702. }
  703. case SHIELD_DEFENSE_PERFECT_BLOCK: // perfect block
  704. {
  705. return 1.;
  706. }
  707. }
  708. // Add soulshot boost.
  709. int ssBoost = ss ? 2 : 1;
  710. damage = (skill != null) ? ((damage * ssBoost) + skill.getPower(attacker, target, isPvP, isPvE)) : (damage * ssBoost);
  711. if (crit)
  712. {
  713. // Finally retail like formula
  714. damage = 2 * attacker.calcStat(Stats.CRITICAL_DAMAGE, 1, target, skill) * target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE, 1, target, null) * ((70 * damage) / defence);
  715. // Crit dmg add is almost useless in normal hits...
  716. damage += ((attacker.calcStat(Stats.CRITICAL_DAMAGE_ADD, 0, target, skill) * 70) / defence);
  717. damage += target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE_ADD, 0, target, skill);
  718. }
  719. else
  720. {
  721. damage = (70 * damage) / defence;
  722. }
  723. damage *= calcAttackTraitBonus(attacker, target);
  724. // Weapon random damage
  725. damage *= attacker.getRandomDamageMultiplier();
  726. if ((shld > 0) && Config.ALT_GAME_SHIELD_BLOCKS)
  727. {
  728. damage -= target.getShldDef();
  729. if (damage < 0)
  730. {
  731. damage = 0;
  732. }
  733. }
  734. if ((damage > 0) && (damage < 1))
  735. {
  736. damage = 1;
  737. }
  738. else if (damage < 0)
  739. {
  740. damage = 0;
  741. }
  742. // Dmg bonuses in PvP fight
  743. if (isPvP)
  744. {
  745. if (skill == null)
  746. {
  747. damage *= attacker.calcStat(Stats.PVP_PHYSICAL_DMG, 1, null, null);
  748. }
  749. else
  750. {
  751. damage *= attacker.calcStat(Stats.PVP_PHYS_SKILL_DMG, 1, null, null);
  752. }
  753. }
  754. // Physical skill dmg boost
  755. if (skill != null)
  756. {
  757. damage = attacker.calcStat(Stats.PHYSICAL_SKILL_POWER, damage, null, null);
  758. }
  759. damage *= calcAttributeBonus(attacker, target, skill);
  760. if (target.isAttackable())
  761. {
  762. final L2Weapon weapon = attacker.getActiveWeaponItem();
  763. if ((weapon != null) && ((weapon.getItemType() == WeaponType.BOW) || (weapon.getItemType() == WeaponType.CROSSBOW)))
  764. {
  765. if (skill != null)
  766. {
  767. damage *= attacker.calcStat(Stats.PVE_BOW_SKILL_DMG, 1, null, null);
  768. }
  769. else
  770. {
  771. damage *= attacker.calcStat(Stats.PVE_BOW_DMG, 1, null, null);
  772. }
  773. }
  774. else
  775. {
  776. damage *= attacker.calcStat(Stats.PVE_PHYSICAL_DMG, 1, null, null);
  777. }
  778. if (!target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 2))
  779. {
  780. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 1;
  781. if (skill != null)
  782. {
  783. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  784. {
  785. damage *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  786. }
  787. else
  788. {
  789. damage *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  790. }
  791. }
  792. else if (crit)
  793. {
  794. if (lvlDiff >= Config.NPC_CRIT_DMG_PENALTY.size())
  795. {
  796. damage *= Config.NPC_CRIT_DMG_PENALTY.get(Config.NPC_CRIT_DMG_PENALTY.size() - 1);
  797. }
  798. else
  799. {
  800. damage *= Config.NPC_CRIT_DMG_PENALTY.get(lvlDiff);
  801. }
  802. }
  803. else
  804. {
  805. if (lvlDiff >= Config.NPC_DMG_PENALTY.size())
  806. {
  807. damage *= Config.NPC_DMG_PENALTY.get(Config.NPC_DMG_PENALTY.size() - 1);
  808. }
  809. else
  810. {
  811. damage *= Config.NPC_DMG_PENALTY.get(lvlDiff);
  812. }
  813. }
  814. }
  815. }
  816. return damage;
  817. }
  818. public static final double calcMagicDam(L2Character attacker, L2Character target, Skill skill, byte shld, boolean sps, boolean bss, boolean mcrit)
  819. {
  820. int mDef = target.getMDef(attacker, skill);
  821. switch (shld)
  822. {
  823. case SHIELD_DEFENSE_SUCCEED:
  824. {
  825. mDef += target.getShldDef();
  826. break;
  827. }
  828. case SHIELD_DEFENSE_PERFECT_BLOCK:
  829. {
  830. return 1;
  831. }
  832. }
  833. int mAtk = attacker.getMAtk(target, skill);
  834. final boolean isPvP = attacker.isPlayable() && target.isPlayable();
  835. final boolean isPvE = attacker.isPlayable() && target.isAttackable();
  836. // PvP bonuses for defense
  837. if (isPvP)
  838. {
  839. if (skill.isMagic())
  840. {
  841. mDef *= target.calcStat(Stats.PVP_MAGICAL_DEF, 1, null, null);
  842. }
  843. else
  844. {
  845. mDef *= target.calcStat(Stats.PVP_PHYS_SKILL_DEF, 1, null, null);
  846. }
  847. }
  848. // Bonus Spirit shot
  849. mAtk *= bss ? 4 : sps ? 2 : 1;
  850. // MDAM Formula.
  851. double damage = ((91 * Math.sqrt(mAtk)) / mDef) * skill.getPower(attacker, target, isPvP, isPvE);
  852. // Failure calculation
  853. if (Config.ALT_GAME_MAGICFAILURES && !calcMagicSuccess(attacker, target, skill))
  854. {
  855. if (attacker.isPlayer())
  856. {
  857. if (calcMagicSuccess(attacker, target, skill) && ((target.getLevel() - attacker.getLevel()) <= 9))
  858. {
  859. if (skill.hasEffectType(L2EffectType.HP_DRAIN))
  860. {
  861. attacker.sendPacket(SystemMessageId.DRAIN_HALF_SUCCESFUL);
  862. }
  863. else
  864. {
  865. attacker.sendPacket(SystemMessageId.ATTACK_FAILED);
  866. }
  867. damage /= 2;
  868. }
  869. else
  870. {
  871. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
  872. sm.addCharName(target);
  873. sm.addSkillName(skill);
  874. attacker.sendPacket(sm);
  875. damage = 1;
  876. }
  877. }
  878. if (target.isPlayer())
  879. {
  880. final SystemMessage sm = (skill.hasEffectType(L2EffectType.HP_DRAIN)) ? SystemMessage.getSystemMessage(SystemMessageId.RESISTED_C1_DRAIN) : SystemMessage.getSystemMessage(SystemMessageId.RESISTED_C1_MAGIC);
  881. sm.addCharName(attacker);
  882. target.sendPacket(sm);
  883. }
  884. }
  885. else if (mcrit)
  886. {
  887. damage *= attacker.isPlayer() && target.isPlayer() ? 2.5 : 3;
  888. damage *= attacker.calcStat(Stats.MAGIC_CRIT_DMG, 1, null, null);
  889. }
  890. // Weapon random damage
  891. damage *= attacker.getRandomDamageMultiplier();
  892. // PvP bonuses for damage
  893. if (isPvP)
  894. {
  895. Stats stat = skill.isMagic() ? Stats.PVP_MAGICAL_DMG : Stats.PVP_PHYS_SKILL_DMG;
  896. damage *= attacker.calcStat(stat, 1, null, null);
  897. }
  898. damage *= calcAttributeBonus(attacker, target, skill);
  899. if (target.isAttackable())
  900. {
  901. damage *= attacker.calcStat(Stats.PVE_MAGICAL_DMG, 1, null, null);
  902. if (!target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 2))
  903. {
  904. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 1;
  905. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  906. {
  907. damage *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  908. }
  909. else
  910. {
  911. damage *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  912. }
  913. }
  914. }
  915. return damage;
  916. }
  917. public static final double calcMagicDam(L2CubicInstance attacker, L2Character target, Skill skill, boolean mcrit, byte shld)
  918. {
  919. int mDef = target.getMDef(attacker.getOwner(), skill);
  920. switch (shld)
  921. {
  922. case SHIELD_DEFENSE_SUCCEED:
  923. mDef += target.getShldDef(); // kamael
  924. break;
  925. case SHIELD_DEFENSE_PERFECT_BLOCK: // perfect block
  926. return 1;
  927. }
  928. int mAtk = attacker.getCubicPower();
  929. final boolean isPvP = target.isPlayable();
  930. final boolean isPvE = target.isAttackable();
  931. // Cubics MDAM Formula (similar to PDAM formula, but using 91 instead of 70, also resisted by mDef).
  932. double damage = 91 * ((mAtk + skill.getPower(isPvP, isPvE)) / mDef);
  933. // Failure calculation
  934. L2PcInstance owner = attacker.getOwner();
  935. if (Config.ALT_GAME_MAGICFAILURES && !calcMagicSuccess(owner, target, skill))
  936. {
  937. if (calcMagicSuccess(owner, target, skill) && ((target.getLevel() - skill.getMagicLevel()) <= 9))
  938. {
  939. if (skill.hasEffectType(L2EffectType.HP_DRAIN))
  940. {
  941. owner.sendPacket(SystemMessageId.DRAIN_HALF_SUCCESFUL);
  942. }
  943. else
  944. {
  945. owner.sendPacket(SystemMessageId.ATTACK_FAILED);
  946. }
  947. damage /= 2;
  948. }
  949. else
  950. {
  951. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
  952. sm.addCharName(target);
  953. sm.addSkillName(skill);
  954. owner.sendPacket(sm);
  955. damage = 1;
  956. }
  957. if (target.isPlayer())
  958. {
  959. if (skill.hasEffectType(L2EffectType.HP_DRAIN))
  960. {
  961. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.RESISTED_C1_DRAIN);
  962. sm.addCharName(owner);
  963. target.sendPacket(sm);
  964. }
  965. else
  966. {
  967. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.RESISTED_C1_MAGIC);
  968. sm.addCharName(owner);
  969. target.sendPacket(sm);
  970. }
  971. }
  972. }
  973. else if (mcrit)
  974. {
  975. damage *= 3;
  976. }
  977. damage *= calcAttributeBonus(owner, target, skill);
  978. if (target.isAttackable())
  979. {
  980. damage *= attacker.getOwner().calcStat(Stats.PVE_MAGICAL_DMG, 1, null, null);
  981. if (!target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getOwner() != null) && ((target.getLevel() - attacker.getOwner().getLevel()) >= 2))
  982. {
  983. int lvlDiff = target.getLevel() - attacker.getOwner().getLevel() - 1;
  984. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  985. {
  986. damage *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  987. }
  988. else
  989. {
  990. damage *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  991. }
  992. }
  993. }
  994. return damage;
  995. }
  996. /**
  997. * Returns true in case of critical hit
  998. * @param rate
  999. * @param skill
  1000. * @param target
  1001. * @return
  1002. */
  1003. public static final boolean calcCrit(double rate, boolean skill, L2Character target)
  1004. {
  1005. double finalRate = target.getStat().calcStat(Stats.DEFENCE_CRITICAL_RATE, rate, null, null) + target.getStat().calcStat(Stats.DEFENCE_CRITICAL_RATE_ADD, 0, null, null);
  1006. return finalRate > Rnd.get(1000);
  1007. }
  1008. public static final boolean calcMCrit(double mRate)
  1009. {
  1010. return mRate > Rnd.get(1000);
  1011. }
  1012. /**
  1013. * @param target
  1014. * @param dmg
  1015. * @return true in case when ATTACK is canceled due to hit
  1016. */
  1017. public static final boolean calcAtkBreak(L2Character target, double dmg)
  1018. {
  1019. if (target.isChanneling())
  1020. {
  1021. return false;
  1022. }
  1023. double init = 0;
  1024. if (Config.ALT_GAME_CANCEL_CAST && target.isCastingNow())
  1025. {
  1026. init = 15;
  1027. }
  1028. if (Config.ALT_GAME_CANCEL_BOW && target.isAttackingNow())
  1029. {
  1030. L2Weapon wpn = target.getActiveWeaponItem();
  1031. if ((wpn != null) && (wpn.getItemType() == WeaponType.BOW))
  1032. {
  1033. init = 15;
  1034. }
  1035. }
  1036. if (target.isRaid() || target.isInvul() || (init <= 0))
  1037. {
  1038. return false; // No attack break
  1039. }
  1040. // Chance of break is higher with higher dmg
  1041. init += Math.sqrt(13 * dmg);
  1042. // Chance is affected by target MEN
  1043. init -= ((BaseStats.MEN.calcBonus(target) * 100) - 100);
  1044. // Calculate all modifiers for ATTACK_CANCEL
  1045. double rate = target.calcStat(Stats.ATTACK_CANCEL, init, null, null);
  1046. // Adjust the rate to be between 1 and 99
  1047. rate = Math.max(Math.min(rate, 99), 1);
  1048. return Rnd.get(100) < rate;
  1049. }
  1050. /**
  1051. * Calculate delay (in milliseconds) before next ATTACK
  1052. * @param attacker
  1053. * @param target
  1054. * @param rate
  1055. * @return
  1056. */
  1057. public static final int calcPAtkSpd(L2Character attacker, L2Character target, double rate)
  1058. {
  1059. // measured Oct 2006 by Tank6585, formula by Sami
  1060. // attack speed 312 equals 1500 ms delay... (or 300 + 40 ms delay?)
  1061. if (rate < 2)
  1062. {
  1063. return 2700;
  1064. }
  1065. return (int) (470000 / rate);
  1066. }
  1067. /**
  1068. * Calculate delay (in milliseconds) for skills cast
  1069. * @param attacker
  1070. * @param skill
  1071. * @param skillTime
  1072. * @return
  1073. */
  1074. public static final int calcAtkSpd(L2Character attacker, Skill skill, double skillTime)
  1075. {
  1076. if (skill.isMagic())
  1077. {
  1078. return (int) ((skillTime / attacker.getMAtkSpd()) * 333);
  1079. }
  1080. return (int) ((skillTime / attacker.getPAtkSpd()) * 300);
  1081. }
  1082. /**
  1083. * Formula based on http://l2p.l2wh.com/nonskillattacks.html
  1084. * @param attacker
  1085. * @param target
  1086. * @return {@code true} if hit missed (target evaded), {@code false} otherwise.
  1087. */
  1088. public static boolean calcHitMiss(L2Character attacker, L2Character target)
  1089. {
  1090. int chance = (80 + (2 * (attacker.getAccuracy() - target.getEvasionRate(attacker)))) * 10;
  1091. // Get additional bonus from the conditions when you are attacking
  1092. chance *= HitConditionBonus.getInstance().getConditionBonus(attacker, target);
  1093. chance = Math.max(chance, 200);
  1094. chance = Math.min(chance, 980);
  1095. return chance < Rnd.get(1000);
  1096. }
  1097. /**
  1098. * Returns:<br>
  1099. * 0 = shield defense doesn't succeed<br>
  1100. * 1 = shield defense succeed<br>
  1101. * 2 = perfect block<br>
  1102. * @param attacker
  1103. * @param target
  1104. * @param skill
  1105. * @param sendSysMsg
  1106. * @return
  1107. */
  1108. public static byte calcShldUse(L2Character attacker, L2Character target, Skill skill, boolean sendSysMsg)
  1109. {
  1110. if ((skill != null) && skill.ignoreShield())
  1111. {
  1112. return 0;
  1113. }
  1114. L2Item item = target.getSecondaryWeaponItem();
  1115. if ((item == null) || !(item instanceof L2Armor) || (((L2Armor) item).getItemType() == ArmorType.SIGIL))
  1116. {
  1117. return 0;
  1118. }
  1119. double shldRate = target.calcStat(Stats.SHIELD_RATE, 0, attacker, null) * BaseStats.DEX.calcBonus(target);
  1120. if (shldRate <= 1e-6)
  1121. {
  1122. return 0;
  1123. }
  1124. int degreeside = (int) target.calcStat(Stats.SHIELD_DEFENCE_ANGLE, 0, null, null) + 120;
  1125. if ((degreeside < 360) && (!target.isFacing(attacker, degreeside)))
  1126. {
  1127. return 0;
  1128. }
  1129. byte shldSuccess = SHIELD_DEFENSE_FAILED;
  1130. // if attacker
  1131. // if attacker use bow and target wear shield, shield block rate is multiplied by 1.3 (30%)
  1132. L2Weapon at_weapon = attacker.getActiveWeaponItem();
  1133. if ((at_weapon != null) && (at_weapon.getItemType() == WeaponType.BOW))
  1134. {
  1135. shldRate *= 1.3;
  1136. }
  1137. if ((shldRate > 0) && ((100 - Config.ALT_PERFECT_SHLD_BLOCK) < Rnd.get(100)))
  1138. {
  1139. shldSuccess = SHIELD_DEFENSE_PERFECT_BLOCK;
  1140. }
  1141. else if (shldRate > Rnd.get(100))
  1142. {
  1143. shldSuccess = SHIELD_DEFENSE_SUCCEED;
  1144. }
  1145. if (sendSysMsg && target.isPlayer())
  1146. {
  1147. L2PcInstance enemy = target.getActingPlayer();
  1148. switch (shldSuccess)
  1149. {
  1150. case SHIELD_DEFENSE_SUCCEED:
  1151. enemy.sendPacket(SystemMessageId.SHIELD_DEFENCE_SUCCESSFULL);
  1152. break;
  1153. case SHIELD_DEFENSE_PERFECT_BLOCK:
  1154. enemy.sendPacket(SystemMessageId.YOUR_EXCELLENT_SHIELD_DEFENSE_WAS_A_SUCCESS);
  1155. break;
  1156. }
  1157. }
  1158. return shldSuccess;
  1159. }
  1160. public static byte calcShldUse(L2Character attacker, L2Character target, Skill skill)
  1161. {
  1162. return calcShldUse(attacker, target, skill, true);
  1163. }
  1164. public static byte calcShldUse(L2Character attacker, L2Character target)
  1165. {
  1166. return calcShldUse(attacker, target, null, true);
  1167. }
  1168. public static boolean calcMagicAffected(L2Character actor, L2Character target, Skill skill)
  1169. {
  1170. // TODO: CHECK/FIX THIS FORMULA UP!!
  1171. double defence = 0;
  1172. if (skill.isActive() && skill.isBad())
  1173. {
  1174. defence = target.getMDef(actor, skill);
  1175. }
  1176. double attack = 2 * actor.getMAtk(target, skill) * calcGeneralTraitBonus(actor, target, skill.getTraitType(), false);
  1177. double d = (attack - defence) / (attack + defence);
  1178. if (skill.isDebuff())
  1179. {
  1180. if (target.calcStat(Stats.DEBUFF_IMMUNITY, 0, null, skill) > 0)
  1181. {
  1182. return false;
  1183. }
  1184. }
  1185. d += 0.5 * Rnd.nextGaussian();
  1186. return d > 0;
  1187. }
  1188. public static double calcLvlBonusMod(L2Character attacker, L2Character target, Skill skill)
  1189. {
  1190. int attackerLvl = skill.getMagicLevel() > 0 ? skill.getMagicLevel() : attacker.getLevel();
  1191. double skillLvlBonusRateMod = 1 + (skill.getLvlBonusRate() / 100.);
  1192. double lvlMod = 1 + ((attackerLvl - target.getLevel()) / 100.);
  1193. return skillLvlBonusRateMod * lvlMod;
  1194. }
  1195. /**
  1196. * Calculates the effect landing success.<br>
  1197. * @param attacker the attacker
  1198. * @param target the target
  1199. * @param skill the skill
  1200. * @return {@code true} if the effect lands
  1201. */
  1202. public static boolean calcEffectSuccess(L2Character attacker, L2Character target, Skill skill)
  1203. {
  1204. // StaticObjects can not receive continuous effects.
  1205. if (target.isDoor() || (target instanceof L2SiegeFlagInstance) || (target instanceof L2StaticObjectInstance))
  1206. {
  1207. return false;
  1208. }
  1209. if (skill.isDebuff() && (target.calcStat(Stats.DEBUFF_IMMUNITY, 0, attacker, skill) > 0))
  1210. {
  1211. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
  1212. sm.addCharName(target);
  1213. sm.addSkillName(skill);
  1214. attacker.sendPacket(sm);
  1215. return false;
  1216. }
  1217. final int activateRate = skill.getActivateRate();
  1218. if ((activateRate == -1) || (skill.getBasicProperty() == BaseStats.NONE))
  1219. {
  1220. return true;
  1221. }
  1222. int magicLevel = skill.getMagicLevel();
  1223. if (magicLevel <= -1)
  1224. {
  1225. magicLevel = target.getLevel() + 3;
  1226. }
  1227. int targetBaseStat = 0;
  1228. switch (skill.getBasicProperty())
  1229. {
  1230. case STR:
  1231. targetBaseStat = target.getSTR();
  1232. break;
  1233. case DEX:
  1234. targetBaseStat = target.getDEX();
  1235. break;
  1236. case CON:
  1237. targetBaseStat = target.getCON();
  1238. break;
  1239. case INT:
  1240. targetBaseStat = target.getINT();
  1241. break;
  1242. case MEN:
  1243. targetBaseStat = target.getMEN();
  1244. break;
  1245. case WIT:
  1246. targetBaseStat = target.getWIT();
  1247. break;
  1248. }
  1249. final double baseMod = ((((((magicLevel - target.getLevel()) + 3) * skill.getLvlBonusRate()) + activateRate) + 30.0) - targetBaseStat);
  1250. final double elementMod = calcAttributeBonus(attacker, target, skill);
  1251. final double traitMod = calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false);
  1252. final double buffDebuffMod = 1 + (target.calcStat(skill.isDebuff() ? Stats.DEBUFF_VULN : Stats.BUFF_VULN, 1, null, null) / 100);
  1253. double mAtkMod = 1;
  1254. if (skill.isMagic())
  1255. {
  1256. double mAtk = attacker.getMAtk(null, null);
  1257. double val = 0;
  1258. if (attacker.isChargedShot(ShotType.BLESSED_SPIRITSHOTS))
  1259. {
  1260. val = mAtk * 3.0;// 3.0 is the blessed spiritshot multiplier
  1261. }
  1262. val += mAtk;
  1263. val = (Math.sqrt(val) / target.getMDef(null, null)) * 11.0;
  1264. mAtkMod = val;
  1265. }
  1266. final double rate = baseMod * elementMod * traitMod * mAtkMod * buffDebuffMod;
  1267. final double finalRate = traitMod > 0 ? Util.constrain(rate, skill.getMinChance(), skill.getMaxChance()) : 0;
  1268. if (attacker.isDebug())
  1269. {
  1270. final StatsSet set = new StatsSet();
  1271. set.set("baseMod", baseMod);
  1272. set.set("elementMod", elementMod);
  1273. set.set("traitMod", traitMod);
  1274. set.set("mAtkMod", mAtkMod);
  1275. set.set("buffDebuffMod", buffDebuffMod);
  1276. set.set("rate", rate);
  1277. set.set("finalRate", finalRate);
  1278. Debug.sendSkillDebug(attacker, target, skill, set);
  1279. }
  1280. if (finalRate <= Rnd.get(100))
  1281. {
  1282. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
  1283. sm.addCharName(target);
  1284. sm.addSkillName(skill);
  1285. attacker.sendPacket(sm);
  1286. return false;
  1287. }
  1288. return true;
  1289. }
  1290. public static boolean calcCubicSkillSuccess(L2CubicInstance attacker, L2Character target, Skill skill, byte shld)
  1291. {
  1292. if (skill.isDebuff())
  1293. {
  1294. if (skill.getPower() == -1)
  1295. {
  1296. return true;
  1297. }
  1298. else if (target.calcStat(Stats.DEBUFF_IMMUNITY, 0, null, skill) > 0)
  1299. {
  1300. return false;
  1301. }
  1302. }
  1303. // Perfect Shield Block.
  1304. if (shld == SHIELD_DEFENSE_PERFECT_BLOCK)
  1305. {
  1306. return false;
  1307. }
  1308. // if target reflect this skill then the effect will fail
  1309. if (calcBuffDebuffReflection(target, skill))
  1310. {
  1311. return false;
  1312. }
  1313. // Calculate BaseRate.
  1314. double baseRate = skill.getPower();
  1315. double statMod = skill.getBasicProperty().calcBonus(target);
  1316. double rate = (baseRate / statMod);
  1317. // Resist Modifier.
  1318. double resMod = calcGeneralTraitBonus(attacker.getOwner(), target, skill.getTraitType(), false);
  1319. rate *= resMod;
  1320. // Lvl Bonus Modifier.
  1321. double lvlBonusMod = calcLvlBonusMod(attacker.getOwner(), target, skill);
  1322. rate *= lvlBonusMod;
  1323. // Element Modifier.
  1324. double elementMod = calcAttributeBonus(attacker.getOwner(), target, skill);
  1325. rate *= elementMod;
  1326. // Add Matk/Mdef Bonus (TODO: Pending)
  1327. // Check the Rate Limits.
  1328. final double finalRate = Util.constrain(rate, skill.getMinChance(), skill.getMaxChance());
  1329. if (attacker.getOwner().isDebug())
  1330. {
  1331. final StatsSet set = new StatsSet();
  1332. set.set("baseMod", baseRate);
  1333. set.set("resMod", resMod);
  1334. set.set("statMod", statMod);
  1335. set.set("elementMod", elementMod);
  1336. set.set("lvlBonusMod", lvlBonusMod);
  1337. set.set("rate", rate);
  1338. set.set("finalRate", finalRate);
  1339. Debug.sendSkillDebug(attacker.getOwner(), target, skill, set);
  1340. }
  1341. return (Rnd.get(100) < finalRate);
  1342. }
  1343. public static boolean calcMagicSuccess(L2Character attacker, L2Character target, Skill skill)
  1344. {
  1345. if (skill.getPower() == -1)
  1346. {
  1347. return true;
  1348. }
  1349. // FIXME: Fix this LevelMod Formula.
  1350. int lvlDifference = (target.getLevel() - (skill.getMagicLevel() > 0 ? skill.getMagicLevel() : attacker.getLevel()));
  1351. double lvlModifier = Math.pow(1.3, lvlDifference);
  1352. float targetModifier = 1;
  1353. if (target.isAttackable() && !target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_MAGIC_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 3))
  1354. {
  1355. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 2;
  1356. if (lvlDiff >= Config.NPC_SKILL_CHANCE_PENALTY.size())
  1357. {
  1358. targetModifier = Config.NPC_SKILL_CHANCE_PENALTY.get(Config.NPC_SKILL_CHANCE_PENALTY.size() - 1);
  1359. }
  1360. else
  1361. {
  1362. targetModifier = Config.NPC_SKILL_CHANCE_PENALTY.get(lvlDiff);
  1363. }
  1364. }
  1365. // general magic resist
  1366. final double resModifier = target.calcStat(Stats.MAGIC_SUCCESS_RES, 1, null, skill);
  1367. int rate = 100 - Math.round((float) (lvlModifier * targetModifier * resModifier));
  1368. if (attacker.isDebug())
  1369. {
  1370. final StatsSet set = new StatsSet();
  1371. set.set("lvlDifference", lvlDifference);
  1372. set.set("lvlModifier", lvlModifier);
  1373. set.set("resModifier", resModifier);
  1374. set.set("targetModifier", targetModifier);
  1375. set.set("rate", rate);
  1376. Debug.sendSkillDebug(attacker, target, skill, set);
  1377. }
  1378. return (Rnd.get(100) < rate);
  1379. }
  1380. public static double calcManaDam(L2Character attacker, L2Character target, Skill skill, byte shld, boolean sps, boolean bss, boolean mcrit)
  1381. {
  1382. // Formula: (SQR(M.Atk)*Power*(Target Max MP/97))/M.Def
  1383. double mAtk = attacker.getMAtk(target, skill);
  1384. double mDef = target.getMDef(attacker, skill);
  1385. final boolean isPvP = attacker.isPlayable() && target.isPlayable();
  1386. final boolean isPvE = attacker.isPlayable() && target.isAttackable();
  1387. double mp = target.getMaxMp();
  1388. switch (shld)
  1389. {
  1390. case SHIELD_DEFENSE_SUCCEED:
  1391. mDef += target.getShldDef();
  1392. break;
  1393. case SHIELD_DEFENSE_PERFECT_BLOCK: // perfect block
  1394. return 1;
  1395. }
  1396. // Bonus Spiritshot
  1397. mAtk *= bss ? 4 : sps ? 2 : 1;
  1398. double damage = (Math.sqrt(mAtk) * skill.getPower(attacker, target, isPvP, isPvE) * (mp / 97)) / mDef;
  1399. damage *= calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false);
  1400. if (target.isAttackable())
  1401. {
  1402. damage *= attacker.calcStat(Stats.PVE_MAGICAL_DMG, 1, null, null);
  1403. if (!target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 2))
  1404. {
  1405. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 1;
  1406. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  1407. {
  1408. damage *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  1409. }
  1410. else
  1411. {
  1412. damage *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  1413. }
  1414. }
  1415. }
  1416. // Failure calculation
  1417. if (Config.ALT_GAME_MAGICFAILURES && !calcMagicSuccess(attacker, target, skill))
  1418. {
  1419. if (attacker.isPlayer())
  1420. {
  1421. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
  1422. sm.addCharName(target);
  1423. sm.addSkillName(skill);
  1424. attacker.sendPacket(sm);
  1425. damage /= 2;
  1426. }
  1427. if (target.isPlayer())
  1428. {
  1429. SystemMessage sm2 = SystemMessage.getSystemMessage(SystemMessageId.RESISTED_C1_MAGIC);
  1430. sm2.addCharName(attacker);
  1431. target.sendPacket(sm2);
  1432. }
  1433. }
  1434. if (mcrit)
  1435. {
  1436. damage *= 3;
  1437. attacker.sendPacket(SystemMessageId.CRITICAL_HIT_MAGIC);
  1438. }
  1439. return damage;
  1440. }
  1441. public static double calculateSkillResurrectRestorePercent(double baseRestorePercent, L2Character caster)
  1442. {
  1443. if ((baseRestorePercent == 0) || (baseRestorePercent == 100))
  1444. {
  1445. return baseRestorePercent;
  1446. }
  1447. double restorePercent = baseRestorePercent * BaseStats.WIT.calcBonus(caster);
  1448. if ((restorePercent - baseRestorePercent) > 20.0)
  1449. {
  1450. restorePercent += 20.0;
  1451. }
  1452. restorePercent = Math.max(restorePercent, baseRestorePercent);
  1453. restorePercent = Math.min(restorePercent, 90.0);
  1454. return restorePercent;
  1455. }
  1456. public static boolean calcPhysicalSkillEvasion(L2Character activeChar, L2Character target, Skill skill)
  1457. {
  1458. if (skill.isMagic() || skill.isDebuff())
  1459. {
  1460. return false;
  1461. }
  1462. if (Rnd.get(100) < target.calcStat(Stats.P_SKILL_EVASION, 0, null, skill))
  1463. {
  1464. if (activeChar.isPlayer())
  1465. {
  1466. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_DODGES_ATTACK);
  1467. sm.addString(target.getName());
  1468. activeChar.getActingPlayer().sendPacket(sm);
  1469. }
  1470. if (target.isPlayer())
  1471. {
  1472. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.AVOIDED_C1_ATTACK2);
  1473. sm.addString(activeChar.getName());
  1474. target.getActingPlayer().sendPacket(sm);
  1475. }
  1476. return true;
  1477. }
  1478. return false;
  1479. }
  1480. public static boolean calcSkillMastery(L2Character actor, Skill sk)
  1481. {
  1482. // Static Skills are not affected by Skill Mastery.
  1483. if (sk.isStatic())
  1484. {
  1485. return false;
  1486. }
  1487. double val = actor.getStat().calcStat(Stats.SKILL_MASTERY, 1, null, null);
  1488. if (actor.isPlayer())
  1489. {
  1490. val *= (actor.getActingPlayer().isMageClass() ? BaseStats.INT : BaseStats.STR).calcBonus(actor);
  1491. }
  1492. return Rnd.get(100) < val;
  1493. }
  1494. /**
  1495. * Calculates the Attribute Bonus
  1496. * @param attacker
  1497. * @param target
  1498. * @param skill Can be {@code null} if there is no skill used for the attack.
  1499. * @return The attribute bonus
  1500. */
  1501. public static double calcAttributeBonus(L2Character attacker, L2Character target, Skill skill)
  1502. {
  1503. int attack_attribute;
  1504. int defence_attribute;
  1505. if (skill != null)
  1506. {
  1507. if (skill.getElement() == -1)
  1508. {
  1509. attack_attribute = 0;
  1510. defence_attribute = target.getDefenseElementValue((byte) -1);
  1511. }
  1512. else
  1513. {
  1514. if (attacker.getAttackElement() == skill.getElement())
  1515. {
  1516. attack_attribute = attacker.getAttackElementValue(attacker.getAttackElement()) + skill.getElementPower();
  1517. defence_attribute = target.getDefenseElementValue(attacker.getAttackElement());
  1518. }
  1519. else
  1520. {
  1521. attack_attribute = skill.getElementPower();
  1522. defence_attribute = target.getDefenseElementValue(skill.getElement());
  1523. }
  1524. }
  1525. }
  1526. else
  1527. {
  1528. attack_attribute = attacker.getAttackElementValue(attacker.getAttackElement());
  1529. defence_attribute = target.getDefenseElementValue(attacker.getAttackElement());
  1530. }
  1531. double attack_attribute_mod = 0;
  1532. double defence_attribute_mod = 0;
  1533. if (attack_attribute >= 450)
  1534. {
  1535. if (defence_attribute >= 450)
  1536. {
  1537. attack_attribute_mod = 0.06909;
  1538. defence_attribute_mod = 0.078;
  1539. }
  1540. else if (attack_attribute >= 350)
  1541. {
  1542. attack_attribute_mod = 0.0887;
  1543. defence_attribute_mod = 0.1007;
  1544. }
  1545. else
  1546. {
  1547. attack_attribute_mod = 0.129;
  1548. defence_attribute_mod = 0.1473;
  1549. }
  1550. }
  1551. else if (attack_attribute >= 300)
  1552. {
  1553. if (defence_attribute >= 300)
  1554. {
  1555. attack_attribute_mod = 0.0887;
  1556. defence_attribute_mod = 0.1007;
  1557. }
  1558. else if (defence_attribute >= 150)
  1559. {
  1560. attack_attribute_mod = 0.129;
  1561. defence_attribute_mod = 0.1473;
  1562. }
  1563. else
  1564. {
  1565. attack_attribute_mod = 0.25;
  1566. defence_attribute_mod = 0.2894;
  1567. }
  1568. }
  1569. else if (attack_attribute >= 150)
  1570. {
  1571. if (defence_attribute >= 150)
  1572. {
  1573. attack_attribute_mod = 0.129;
  1574. defence_attribute_mod = 0.1473;
  1575. }
  1576. else if (defence_attribute >= 0)
  1577. {
  1578. attack_attribute_mod = 0.25;
  1579. defence_attribute_mod = 0.2894;
  1580. }
  1581. else
  1582. {
  1583. attack_attribute_mod = 0.4;
  1584. defence_attribute_mod = 0.55;
  1585. }
  1586. }
  1587. else if (attack_attribute >= -99)
  1588. {
  1589. if (defence_attribute >= 0)
  1590. {
  1591. attack_attribute_mod = 0.25;
  1592. defence_attribute_mod = 0.2894;
  1593. }
  1594. else
  1595. {
  1596. attack_attribute_mod = 0.4;
  1597. defence_attribute_mod = 0.55;
  1598. }
  1599. }
  1600. else
  1601. {
  1602. if (defence_attribute >= 450)
  1603. {
  1604. attack_attribute_mod = 0.06909;
  1605. defence_attribute_mod = 0.078;
  1606. }
  1607. else if (defence_attribute >= 350)
  1608. {
  1609. attack_attribute_mod = 0.0887;
  1610. defence_attribute_mod = 0.1007;
  1611. }
  1612. else
  1613. {
  1614. attack_attribute_mod = 0.129;
  1615. defence_attribute_mod = 0.1473;
  1616. }
  1617. }
  1618. int attribute_diff = attack_attribute - defence_attribute;
  1619. double min;
  1620. double max;
  1621. if (attribute_diff >= 300)
  1622. {
  1623. max = 100.0;
  1624. min = -50;
  1625. }
  1626. else if (attribute_diff >= 150)
  1627. {
  1628. max = 70.0;
  1629. min = -50;
  1630. }
  1631. else if (attribute_diff >= -150)
  1632. {
  1633. max = 40.0;
  1634. min = -50;
  1635. }
  1636. else if (attribute_diff >= -300)
  1637. {
  1638. max = 40.0;
  1639. min = -60;
  1640. }
  1641. else
  1642. {
  1643. max = 40.0;
  1644. min = -80;
  1645. }
  1646. attack_attribute += 100;
  1647. attack_attribute *= attack_attribute;
  1648. attack_attribute_mod = (attack_attribute / 144.0) * attack_attribute_mod;
  1649. defence_attribute += 100;
  1650. defence_attribute *= defence_attribute;
  1651. defence_attribute_mod = (defence_attribute / 169.0) * defence_attribute_mod;
  1652. double attribute_mod_diff = attack_attribute_mod - defence_attribute_mod;
  1653. attribute_mod_diff = Util.constrain(attribute_mod_diff, min, max);
  1654. double result = (attribute_mod_diff / 100.0) + 1;
  1655. if (attacker.isPlayer() && target.isPlayer() && (result < 1.0))
  1656. {
  1657. result = 1.0;
  1658. }
  1659. return result;
  1660. }
  1661. public static void calcDamageReflected(L2Character attacker, L2Character target, Skill skill, boolean crit)
  1662. {
  1663. // Only melee skills can be reflected
  1664. if (skill.isMagic() || (skill.getCastRange() > MELEE_ATTACK_RANGE))
  1665. {
  1666. return;
  1667. }
  1668. final double chance = target.calcStat(Stats.VENGEANCE_SKILL_PHYSICAL_DAMAGE, 0, target, skill);
  1669. if (Rnd.get(100) < chance)
  1670. {
  1671. if (target.isPlayer())
  1672. {
  1673. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.COUNTERED_C1_ATTACK);
  1674. sm.addCharName(attacker);
  1675. target.sendPacket(sm);
  1676. }
  1677. if (attacker.isPlayer())
  1678. {
  1679. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_PERFORMING_COUNTERATTACK);
  1680. sm.addCharName(target);
  1681. attacker.sendPacket(sm);
  1682. }
  1683. double counterdmg = (((target.getPAtk(attacker) * 10.0) * 70.0) / attacker.getPDef(target));
  1684. counterdmg *= calcWeaponTraitBonus(attacker, target);
  1685. counterdmg *= calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false);
  1686. counterdmg *= calcAttributeBonus(attacker, target, skill);
  1687. attacker.reduceCurrentHp(counterdmg, target, skill);
  1688. if (crit) // TODO: It counters multiple times depending on how much effects skill has not on critical, but gotta be verified first!
  1689. {
  1690. attacker.reduceCurrentHp(counterdmg, target, skill);
  1691. }
  1692. }
  1693. }
  1694. /**
  1695. * Calculate buff/debuff reflection.
  1696. * @param target
  1697. * @param skill
  1698. * @return {@code true} if reflect, {@code false} otherwise.
  1699. */
  1700. public static boolean calcBuffDebuffReflection(L2Character target, Skill skill)
  1701. {
  1702. if (!skill.isDebuff() || (skill.getActivateRate() == -1))
  1703. {
  1704. return false;
  1705. }
  1706. final double reflectChance = target.calcStat(skill.isMagic() ? Stats.REFLECT_SKILL_MAGIC : Stats.REFLECT_SKILL_PHYSIC, 0, null, skill);
  1707. return reflectChance > Rnd.get(100);
  1708. }
  1709. /**
  1710. * Calculate damage caused by falling
  1711. * @param cha
  1712. * @param fallHeight
  1713. * @return damage
  1714. */
  1715. public static double calcFallDam(L2Character cha, int fallHeight)
  1716. {
  1717. if (!Config.ENABLE_FALLING_DAMAGE || (fallHeight < 0))
  1718. {
  1719. return 0;
  1720. }
  1721. final double damage = cha.calcStat(Stats.FALL, (fallHeight * cha.getMaxHp()) / 1000.0, null, null);
  1722. return damage;
  1723. }
  1724. public static boolean calcBlowSuccess(L2Character activeChar, L2Character target, Skill skill)
  1725. {
  1726. double dexMod = BaseStats.DEX.calcBonus(activeChar);
  1727. // Apply DEX Mod.
  1728. double blowChance = skill.getBlowChance();
  1729. // Apply Position Bonus (TODO: values are unconfirmed, possibly custom, remove or update when confirmed).
  1730. double sideMod = (activeChar.isInFrontOfTarget()) ? 1 : (activeChar.isBehindTarget()) ? 2 : 1.5;
  1731. // Apply all mods.
  1732. double baseRate = blowChance * dexMod * sideMod;
  1733. // Apply blow rates
  1734. double rate = activeChar.calcStat(Stats.BLOW_RATE, baseRate, target, null);
  1735. // Debug
  1736. if (activeChar.isDebug())
  1737. {
  1738. final StatsSet set = new StatsSet();
  1739. set.set("dexMod", dexMod);
  1740. set.set("blowChance", blowChance);
  1741. set.set("sideMod", sideMod);
  1742. set.set("baseRate", baseRate);
  1743. set.set("rate", rate);
  1744. Debug.sendSkillDebug(activeChar, target, skill, set);
  1745. }
  1746. return Rnd.get(100) < rate;
  1747. }
  1748. public static List<BuffInfo> calcCancelStealEffects(L2Character activeChar, L2Character target, Skill skill, String slot, int rate, int max)
  1749. {
  1750. final List<BuffInfo> canceled = new ArrayList<>(max);
  1751. switch (slot)
  1752. {
  1753. case "buff":
  1754. {
  1755. // Resist Modifier.
  1756. int cancelMagicLvl = skill.getMagicLevel();
  1757. final double vuln = target.calcStat(Stats.CANCEL_VULN, 0, target, null);
  1758. final double prof = activeChar.calcStat(Stats.CANCEL_PROF, 0, target, null);
  1759. double resMod = 1 + (((vuln + prof) * -1) / 100);
  1760. double finalRate = rate / resMod;
  1761. if (activeChar.isDebug())
  1762. {
  1763. final StatsSet set = new StatsSet();
  1764. set.set("baseMod", rate);
  1765. set.set("magicLevel", cancelMagicLvl);
  1766. set.set("resMod", resMod);
  1767. set.set("rate", finalRate);
  1768. Debug.sendSkillDebug(activeChar, target, skill, set);
  1769. }
  1770. // Prevent initialization.
  1771. final List<BuffInfo> buffs = target.getEffectList().hasBuffs() ? new ArrayList<>(target.getEffectList().getBuffs().values()) : new ArrayList<>(1);
  1772. if (target.getEffectList().hasTriggered())
  1773. {
  1774. buffs.addAll(target.getEffectList().getTriggered().values());
  1775. }
  1776. if (target.getEffectList().hasDances())
  1777. {
  1778. buffs.addAll(target.getEffectList().getDances().values());
  1779. }
  1780. for (int i = buffs.size() - 1; i >= 0; i--) // reverse order
  1781. {
  1782. BuffInfo info = buffs.get(i);
  1783. if (!info.getSkill().canBeStolen() || (!calcCancelSuccess(info, cancelMagicLvl, (int) finalRate, skill)))
  1784. {
  1785. continue;
  1786. }
  1787. canceled.add(info);
  1788. if (canceled.size() >= max)
  1789. {
  1790. break;
  1791. }
  1792. }
  1793. break;
  1794. }
  1795. case "debuff":
  1796. {
  1797. final List<BuffInfo> debuffs = new ArrayList<>(target.getEffectList().getDebuffs().values());
  1798. for (int i = debuffs.size() - 1; i >= 0; i--)
  1799. {
  1800. BuffInfo info = debuffs.get(i);
  1801. if (info.getSkill().isDebuff() && info.getSkill().canBeDispeled() && (Rnd.get(100) <= rate))
  1802. {
  1803. canceled.add(info);
  1804. if (canceled.size() >= max)
  1805. {
  1806. break;
  1807. }
  1808. }
  1809. }
  1810. break;
  1811. }
  1812. }
  1813. return canceled;
  1814. }
  1815. public static boolean calcCancelSuccess(BuffInfo info, int cancelMagicLvl, int rate, Skill skill)
  1816. {
  1817. // Lvl Bonus Modifier.
  1818. rate *= info.getSkill().getMagicLevel() > 0 ? 1 + ((cancelMagicLvl - info.getSkill().getMagicLevel()) / 100.) : 1;
  1819. return Rnd.get(100) < Util.constrain(rate, skill.getMinChance(), skill.getMaxChance());
  1820. }
  1821. /**
  1822. * Calculates the abnormal time for an effect.<br>
  1823. * The abnormal time is taken from the skill definition, and it's global for all effects present in the skills.
  1824. * @param caster the caster
  1825. * @param target the target
  1826. * @param skill the skill
  1827. * @return the time that the effect will last
  1828. */
  1829. public static int calcEffectAbnormalTime(L2Character caster, L2Character target, Skill skill)
  1830. {
  1831. int time = skill.isPassive() || skill.isToggle() ? -1 : skill.getAbnormalTime();
  1832. // An herb buff will affect both master and servitor, but the buff duration will be half of the normal duration.
  1833. // If a servitor is not summoned, the master will receive the full buff duration.
  1834. if ((target != null) && target.isServitor() && skill.isAbnormalInstant())
  1835. {
  1836. time /= 2;
  1837. }
  1838. // If the skill is a mastery skill, the effect will last twice the default time.
  1839. if (Formulas.calcSkillMastery(caster, skill))
  1840. {
  1841. time *= 2;
  1842. }
  1843. // Debuffs Duration Affected by Resistances.
  1844. if ((caster != null) && (target != null) && skill.isDebuff())
  1845. {
  1846. double statMod = skill.getBasicProperty().calcBonus(target);
  1847. double resMod = calcGeneralTraitBonus(caster, target, skill.getTraitType(), false);
  1848. double lvlBonusMod = calcLvlBonusMod(caster, target, skill);
  1849. double elementMod = calcAttributeBonus(caster, target, skill);
  1850. time = (int) Math.ceil(Util.constrain(((time * resMod * lvlBonusMod * elementMod) / statMod), (time * 0.5), time));
  1851. }
  1852. return time;
  1853. }
  1854. /**
  1855. * Calculate Probability in following effects:<br>
  1856. * TargetCancel,<br>
  1857. * TargetMeProbability,<br>
  1858. * SkillTurning,<br>
  1859. * Betray,<br>
  1860. * Bluff,<br>
  1861. * DeleteHate,<br>
  1862. * RandomizeHate,<br>
  1863. * DeleteHateOfMe,<br>
  1864. * TransferHate,<br>
  1865. * Confuse<br>
  1866. * @param baseChance chance from effect parameter
  1867. * @param attacker
  1868. * @param target
  1869. * @param skill
  1870. * @return chance for effect to succeed
  1871. */
  1872. public static boolean calcProbability(double baseChance, L2Character attacker, L2Character target, Skill skill)
  1873. {
  1874. return Rnd.get(100) < ((((((skill.getMagicLevel() + baseChance) - target.getLevel()) + 30) - target.getINT()) * calcAttributeBonus(attacker, target, skill)) * calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false));
  1875. }
  1876. /**
  1877. * Calculates karma lost upon death.
  1878. * @param player
  1879. * @param exp
  1880. * @return the amount of karma player has loosed.
  1881. */
  1882. public static int calculateKarmaLost(L2PcInstance player, long exp)
  1883. {
  1884. double karmaLooseMul = KarmaData.getInstance().getMultiplier(player.getLevel());
  1885. if (exp > 0) // Received exp
  1886. {
  1887. exp /= Config.RATE_KARMA_LOST;
  1888. }
  1889. return (int) ((Math.abs(exp) / karmaLooseMul) / 30);
  1890. }
  1891. /**
  1892. * Calculates karma gain upon playable kill.</br> Updated to High Five on 10.09.2014 by Zealar tested in retail.
  1893. * @param pkCount
  1894. * @param isSummon
  1895. * @return karma points that will be added to the player.
  1896. */
  1897. public static int calculateKarmaGain(int pkCount, boolean isSummon)
  1898. {
  1899. int result = 43200;
  1900. if (isSummon)
  1901. {
  1902. result = (int) ((((pkCount * 0.375) + 1) * 60) * 4) - 150;
  1903. if (result > 10800)
  1904. {
  1905. return 10800;
  1906. }
  1907. }
  1908. if (pkCount < 99)
  1909. {
  1910. result = (int) ((((pkCount * 0.5) + 1) * 60) * 12);
  1911. }
  1912. else if (pkCount < 180)
  1913. {
  1914. result = (int) ((((pkCount * 0.125) + 37.75) * 60) * 12);
  1915. }
  1916. return result;
  1917. }
  1918. public static double calcGeneralTraitBonus(L2Character attacker, L2Character target, TraitType traitType, boolean ignoreResistance)
  1919. {
  1920. if (traitType == TraitType.NONE)
  1921. {
  1922. return 1.0;
  1923. }
  1924. if (target.getStat().isTraitInvul(traitType))
  1925. {
  1926. return 0;
  1927. }
  1928. switch (traitType.getType())
  1929. {
  1930. case 2:
  1931. {
  1932. if (!attacker.getStat().hasAttackTrait(traitType) || !target.getStat().hasDefenceTrait(traitType))
  1933. {
  1934. return 1.0;
  1935. }
  1936. break;
  1937. }
  1938. case 3:
  1939. {
  1940. if (ignoreResistance)
  1941. {
  1942. return 1.0;
  1943. }
  1944. break;
  1945. }
  1946. default:
  1947. {
  1948. return 1.0;
  1949. }
  1950. }
  1951. final double result = (attacker.getStat().getAttackTrait(traitType) - target.getStat().getDefenceTrait(traitType)) + 1.0;
  1952. return Util.constrain(result, 0.05, 2.0);
  1953. }
  1954. public static double calcWeaponTraitBonus(L2Character attacker, L2Character target)
  1955. {
  1956. final TraitType type = attacker.getAttackType().getTraitType();
  1957. double result = target.getStat().getDefenceTraits()[type.getId()] - 1.0;
  1958. return 1.0 - result;
  1959. }
  1960. public static double calcAttackTraitBonus(L2Character attacker, L2Character target)
  1961. {
  1962. final double weaponTraitBonus = calcWeaponTraitBonus(attacker, target);
  1963. if (weaponTraitBonus == 0)
  1964. {
  1965. return 0;
  1966. }
  1967. double weaknessBonus = 1.0;
  1968. for (TraitType traitType : TraitType.values())
  1969. {
  1970. if (traitType.getType() == 2)
  1971. {
  1972. weaknessBonus *= calcGeneralTraitBonus(attacker, target, traitType, true);
  1973. if (weaknessBonus == 0)
  1974. {
  1975. return 0;
  1976. }
  1977. }
  1978. }
  1979. return Util.constrain((weaponTraitBonus * weaknessBonus), 0.05, 2.0);
  1980. }
  1981. }