Formulas.java 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  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.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.data.xml.impl.HitConditionBonusData;
  27. import com.l2jserver.gameserver.data.xml.impl.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. * Calculates the CP regeneration rate (base + modifiers).
  425. * @param player the player
  426. * @return the CP regeneration rate
  427. */
  428. public static final double calcCpRegen(L2PcInstance player)
  429. {
  430. // With CON bonus
  431. final double init = player.getActingPlayer().getTemplate().getBaseCpRegen(player.getLevel()) * player.getLevelMod() * BaseStats.CON.calcBonus(player);
  432. double cpRegenMultiplier = Config.CP_REGEN_MULTIPLIER;
  433. if (player.isSitting())
  434. {
  435. cpRegenMultiplier *= 1.5; // Sitting
  436. }
  437. else if (!player.isMoving())
  438. {
  439. cpRegenMultiplier *= 1.1; // Staying
  440. }
  441. else if (player.isRunning())
  442. {
  443. cpRegenMultiplier *= 0.7; // Running
  444. }
  445. return player.calcStat(Stats.REGENERATE_CP_RATE, Math.max(1, init), null, null) * cpRegenMultiplier;
  446. }
  447. public static final double calcFestivalRegenModifier(L2PcInstance activeChar)
  448. {
  449. final int[] festivalInfo = SevenSignsFestival.getInstance().getFestivalForPlayer(activeChar);
  450. final int oracle = festivalInfo[0];
  451. final int festivalId = festivalInfo[1];
  452. int[] festivalCenter;
  453. // If the player isn't found in the festival, leave the regen rate as it is.
  454. if (festivalId < 0)
  455. {
  456. return 0;
  457. }
  458. // Retrieve the X and Y coords for the center of the festival arena the player is in.
  459. if (oracle == SevenSigns.CABAL_DAWN)
  460. {
  461. festivalCenter = SevenSignsFestival.FESTIVAL_DAWN_PLAYER_SPAWNS[festivalId];
  462. }
  463. else
  464. {
  465. festivalCenter = SevenSignsFestival.FESTIVAL_DUSK_PLAYER_SPAWNS[festivalId];
  466. }
  467. // Check the distance between the player and the player spawn point, in the center of the arena.
  468. double distToCenter = activeChar.calculateDistance(festivalCenter[0], festivalCenter[1], 0, false, false);
  469. if (Config.DEBUG)
  470. {
  471. _log.info("Distance: " + distToCenter + ", RegenMulti: " + ((distToCenter * 2.5) / 50));
  472. }
  473. return 1.0 - (distToCenter * 0.0005); // Maximum Decreased Regen of ~ -65%;
  474. }
  475. public static final double calcSiegeRegenModifier(L2PcInstance activeChar)
  476. {
  477. if ((activeChar == null) || (activeChar.getClan() == null))
  478. {
  479. return 0;
  480. }
  481. Siege siege = SiegeManager.getInstance().getSiege(activeChar.getX(), activeChar.getY(), activeChar.getZ());
  482. if ((siege == null) || !siege.isInProgress())
  483. {
  484. return 0;
  485. }
  486. L2SiegeClan siegeClan = siege.getAttackerClan(activeChar.getClan().getId());
  487. if ((siegeClan == null) || siegeClan.getFlag().isEmpty() || !Util.checkIfInRange(200, activeChar, siegeClan.getFlag().get(0), true))
  488. {
  489. return 0;
  490. }
  491. return 1.5; // If all is true, then modifier will be 50% more
  492. }
  493. public static double calcBlowDamage(L2Character attacker, L2Character target, Skill skill, byte shld, boolean ss)
  494. {
  495. double defence = target.getPDef(attacker);
  496. switch (shld)
  497. {
  498. case Formulas.SHIELD_DEFENSE_SUCCEED:
  499. {
  500. defence += target.getShldDef();
  501. break;
  502. }
  503. case Formulas.SHIELD_DEFENSE_PERFECT_BLOCK: // perfect block
  504. {
  505. return 1;
  506. }
  507. }
  508. final boolean isPvP = attacker.isPlayable() && target.isPlayable();
  509. final boolean isPvE = attacker.isPlayable() && target.isAttackable();
  510. double power = skill.getPower(isPvP, isPvE);
  511. double damage = 0;
  512. 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);
  513. double ssboost = ss ? 2 : 1;
  514. double pvpBonus = 1;
  515. if (isPvP)
  516. {
  517. // Damage bonuses in PvP fight
  518. pvpBonus = attacker.calcStat(Stats.PVP_PHYS_SKILL_DMG, 1, null, null);
  519. // Defense bonuses in PvP fight
  520. defence *= target.calcStat(Stats.PVP_PHYS_SKILL_DEF, 1, null, null);
  521. }
  522. // Initial damage
  523. double baseMod = ((77 * (power + (attacker.getPAtk(target) * ssboost))) / defence);
  524. // Critical
  525. double criticalMod = (attacker.calcStat(Stats.CRITICAL_DAMAGE, 1, target, skill));
  526. double criticalVulnMod = (target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE, 1, target, skill));
  527. double criticalAddMod = ((attacker.getStat().calcStat(Stats.CRITICAL_DAMAGE_ADD, 0) * 6.1 * 77) / defence);
  528. double criticalAddVuln = target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE_ADD, 0, target, skill);
  529. // Trait, elements
  530. double weaponTraitMod = calcWeaponTraitBonus(attacker, target);
  531. double generalTraitMod = calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false);
  532. double attributeMod = calcAttributeBonus(attacker, target, skill);
  533. double weaponMod = attacker.getRandomDamageMultiplier();
  534. double penaltyMod = 1;
  535. 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))
  536. {
  537. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 1;
  538. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  539. {
  540. penaltyMod *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  541. }
  542. else
  543. {
  544. penaltyMod *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  545. }
  546. }
  547. damage = (baseMod * criticalMod * criticalVulnMod * proximityBonus * pvpBonus) + criticalAddMod + criticalAddVuln;
  548. damage *= weaponTraitMod;
  549. damage *= generalTraitMod;
  550. damage *= attributeMod;
  551. damage *= weaponMod;
  552. damage *= penaltyMod;
  553. if (attacker.isDebug())
  554. {
  555. final StatsSet set = new StatsSet();
  556. set.set("skillPower", skill.getPower(isPvP, isPvE));
  557. set.set("ssboost", ssboost);
  558. set.set("proximityBonus", proximityBonus);
  559. set.set("pvpBonus", pvpBonus);
  560. set.set("baseMod", baseMod);
  561. set.set("criticalMod", criticalMod);
  562. set.set("criticalVulnMod", criticalVulnMod);
  563. set.set("criticalAddMod", criticalAddMod);
  564. set.set("criticalAddVuln", criticalAddVuln);
  565. set.set("weaponTraitMod", weaponTraitMod);
  566. set.set("generalTraitMod", generalTraitMod);
  567. set.set("attributeMod", attributeMod);
  568. set.set("weaponMod", weaponMod);
  569. set.set("penaltyMod", penaltyMod);
  570. set.set("damage", (int) damage);
  571. Debug.sendSkillDebug(attacker, target, skill, set);
  572. }
  573. return Math.max(damage, 1);
  574. }
  575. public static double calcBackstabDamage(L2Character attacker, L2Character target, Skill skill, byte shld, boolean ss)
  576. {
  577. double defence = target.getPDef(attacker);
  578. switch (shld)
  579. {
  580. case Formulas.SHIELD_DEFENSE_SUCCEED:
  581. {
  582. defence += target.getShldDef();
  583. break;
  584. }
  585. case Formulas.SHIELD_DEFENSE_PERFECT_BLOCK: // perfect block
  586. {
  587. return 1;
  588. }
  589. }
  590. boolean isPvP = attacker.isPlayable() && target.isPlayer();
  591. boolean isPvE = attacker.isPlayable() && target.isAttackable();
  592. double damage = 0;
  593. 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)
  594. double ssboost = ss ? 2 : 1;
  595. double pvpBonus = 1;
  596. if (isPvP)
  597. {
  598. // Damage bonuses in PvP fight
  599. pvpBonus = attacker.calcStat(Stats.PVP_PHYS_SKILL_DMG, 1, null, null);
  600. // Defense bonuses in PvP fight
  601. defence *= target.calcStat(Stats.PVP_PHYS_SKILL_DEF, 1, null, null);
  602. }
  603. // Initial damage
  604. double baseMod = ((77 * (skill.getPower(isPvP, isPvE) + attacker.getPAtk(target))) / defence) * ssboost;
  605. // Critical
  606. double criticalMod = (attacker.calcStat(Stats.CRITICAL_DAMAGE, 1, target, skill));
  607. double criticalVulnMod = (target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE, 1, target, skill));
  608. double criticalAddMod = ((attacker.calcStat(Stats.CRITICAL_DAMAGE_ADD, 0, target, skill) * 6.1 * 77) / defence);
  609. double criticalAddVuln = target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE_ADD, 0, target, skill);
  610. // Trait, elements
  611. double generalTraitMod = calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false);
  612. double attributeMod = calcAttributeBonus(attacker, target, skill);
  613. double weaponMod = attacker.getRandomDamageMultiplier();
  614. double penaltyMod = 1;
  615. if (target.isAttackable() && !target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 2))
  616. {
  617. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 1;
  618. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  619. {
  620. penaltyMod *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  621. }
  622. else
  623. {
  624. penaltyMod *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  625. }
  626. }
  627. damage = (baseMod * criticalMod * criticalVulnMod * proximityBonus * pvpBonus) + criticalAddMod + criticalAddVuln;
  628. damage *= generalTraitMod;
  629. damage *= attributeMod;
  630. damage *= weaponMod;
  631. damage *= penaltyMod;
  632. if (attacker.isDebug())
  633. {
  634. final StatsSet set = new StatsSet();
  635. set.set("skillPower", skill.getPower(isPvP, isPvE));
  636. set.set("ssboost", ssboost);
  637. set.set("proximityBonus", proximityBonus);
  638. set.set("pvpBonus", pvpBonus);
  639. set.set("baseMod", baseMod);
  640. set.set("criticalMod", criticalMod);
  641. set.set("criticalVulnMod", criticalVulnMod);
  642. set.set("criticalAddMod", criticalAddMod);
  643. set.set("criticalAddVuln", criticalAddVuln);
  644. set.set("generalTraitMod", generalTraitMod);
  645. set.set("attributeMod", attributeMod);
  646. set.set("weaponMod", weaponMod);
  647. set.set("penaltyMod", penaltyMod);
  648. set.set("damage", (int) damage);
  649. Debug.sendSkillDebug(attacker, target, skill, set);
  650. }
  651. return Math.max(damage, 1);
  652. }
  653. /**
  654. * Calculated damage caused by ATTACK of attacker on target.
  655. * @param attacker player or NPC that makes ATTACK
  656. * @param target player or NPC, target of ATTACK
  657. * @param skill
  658. * @param shld
  659. * @param crit if the ATTACK have critical success
  660. * @param ss if weapon item was charged by soulshot
  661. * @return
  662. */
  663. public static final double calcPhysDam(L2Character attacker, L2Character target, Skill skill, byte shld, boolean crit, boolean ss)
  664. {
  665. final boolean isPvP = attacker.isPlayable() && target.isPlayable();
  666. final boolean isPvE = attacker.isPlayable() && target.isAttackable();
  667. double damage = attacker.getPAtk(target);
  668. double defence = target.getPDef(attacker);
  669. // Defense bonuses in PvP fight
  670. if (isPvP)
  671. {
  672. defence *= (skill == null) ? target.calcStat(Stats.PVP_PHYSICAL_DEF, 1, null, null) : target.calcStat(Stats.PVP_PHYS_SKILL_DEF, 1, null, null);
  673. }
  674. switch (shld)
  675. {
  676. case SHIELD_DEFENSE_SUCCEED:
  677. {
  678. if (!Config.ALT_GAME_SHIELD_BLOCKS)
  679. {
  680. defence += target.getShldDef();
  681. }
  682. break;
  683. }
  684. case SHIELD_DEFENSE_PERFECT_BLOCK: // perfect block
  685. {
  686. return 1.;
  687. }
  688. }
  689. // Add soulshot boost.
  690. int ssBoost = ss ? 2 : 1;
  691. damage = (skill != null) ? ((damage * ssBoost) + skill.getPower(attacker, target, isPvP, isPvE)) : (damage * ssBoost);
  692. if (crit)
  693. {
  694. // Finally retail like formula
  695. damage = 2 * attacker.calcStat(Stats.CRITICAL_DAMAGE, 1, target, skill) * target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE, 1, target, null) * ((70 * damage) / defence);
  696. // Crit dmg add is almost useless in normal hits...
  697. damage += ((attacker.calcStat(Stats.CRITICAL_DAMAGE_ADD, 0, target, skill) * 70) / defence);
  698. damage += target.calcStat(Stats.DEFENCE_CRITICAL_DAMAGE_ADD, 0, target, skill);
  699. }
  700. else
  701. {
  702. damage = (70 * damage) / defence;
  703. }
  704. damage *= calcAttackTraitBonus(attacker, target);
  705. // Weapon random damage
  706. damage *= attacker.getRandomDamageMultiplier();
  707. if ((shld > 0) && Config.ALT_GAME_SHIELD_BLOCKS)
  708. {
  709. damage -= target.getShldDef();
  710. if (damage < 0)
  711. {
  712. damage = 0;
  713. }
  714. }
  715. if ((damage > 0) && (damage < 1))
  716. {
  717. damage = 1;
  718. }
  719. else if (damage < 0)
  720. {
  721. damage = 0;
  722. }
  723. // Dmg bonuses in PvP fight
  724. if (isPvP)
  725. {
  726. if (skill == null)
  727. {
  728. damage *= attacker.calcStat(Stats.PVP_PHYSICAL_DMG, 1, null, null);
  729. }
  730. else
  731. {
  732. damage *= attacker.calcStat(Stats.PVP_PHYS_SKILL_DMG, 1, null, null);
  733. }
  734. }
  735. // Physical skill dmg boost
  736. if (skill != null)
  737. {
  738. damage = attacker.calcStat(Stats.PHYSICAL_SKILL_POWER, damage, null, null);
  739. }
  740. damage *= calcAttributeBonus(attacker, target, skill);
  741. if (target.isAttackable())
  742. {
  743. final L2Weapon weapon = attacker.getActiveWeaponItem();
  744. if ((weapon != null) && ((weapon.getItemType() == WeaponType.BOW) || (weapon.getItemType() == WeaponType.CROSSBOW)))
  745. {
  746. if (skill != null)
  747. {
  748. damage *= attacker.calcStat(Stats.PVE_BOW_SKILL_DMG, 1, null, null);
  749. }
  750. else
  751. {
  752. damage *= attacker.calcStat(Stats.PVE_BOW_DMG, 1, null, null);
  753. }
  754. }
  755. else
  756. {
  757. damage *= attacker.calcStat(Stats.PVE_PHYSICAL_DMG, 1, null, null);
  758. }
  759. if (!target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 2))
  760. {
  761. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 1;
  762. if (skill != null)
  763. {
  764. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  765. {
  766. damage *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  767. }
  768. else
  769. {
  770. damage *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  771. }
  772. }
  773. else if (crit)
  774. {
  775. if (lvlDiff >= Config.NPC_CRIT_DMG_PENALTY.size())
  776. {
  777. damage *= Config.NPC_CRIT_DMG_PENALTY.get(Config.NPC_CRIT_DMG_PENALTY.size() - 1);
  778. }
  779. else
  780. {
  781. damage *= Config.NPC_CRIT_DMG_PENALTY.get(lvlDiff);
  782. }
  783. }
  784. else
  785. {
  786. if (lvlDiff >= Config.NPC_DMG_PENALTY.size())
  787. {
  788. damage *= Config.NPC_DMG_PENALTY.get(Config.NPC_DMG_PENALTY.size() - 1);
  789. }
  790. else
  791. {
  792. damage *= Config.NPC_DMG_PENALTY.get(lvlDiff);
  793. }
  794. }
  795. }
  796. }
  797. return damage;
  798. }
  799. public static final double calcMagicDam(L2Character attacker, L2Character target, Skill skill, byte shld, boolean sps, boolean bss, boolean mcrit)
  800. {
  801. int mDef = target.getMDef(attacker, skill);
  802. switch (shld)
  803. {
  804. case SHIELD_DEFENSE_SUCCEED:
  805. {
  806. mDef += target.getShldDef();
  807. break;
  808. }
  809. case SHIELD_DEFENSE_PERFECT_BLOCK:
  810. {
  811. return 1;
  812. }
  813. }
  814. int mAtk = attacker.getMAtk(target, skill);
  815. final boolean isPvP = attacker.isPlayable() && target.isPlayable();
  816. final boolean isPvE = attacker.isPlayable() && target.isAttackable();
  817. // PvP bonuses for defense
  818. if (isPvP)
  819. {
  820. if (skill.isMagic())
  821. {
  822. mDef *= target.calcStat(Stats.PVP_MAGICAL_DEF, 1, null, null);
  823. }
  824. else
  825. {
  826. mDef *= target.calcStat(Stats.PVP_PHYS_SKILL_DEF, 1, null, null);
  827. }
  828. }
  829. // Bonus Spirit shot
  830. mAtk *= bss ? 4 : sps ? 2 : 1;
  831. // MDAM Formula.
  832. double damage = ((91 * Math.sqrt(mAtk)) / mDef) * skill.getPower(attacker, target, isPvP, isPvE);
  833. // Failure calculation
  834. if (Config.ALT_GAME_MAGICFAILURES && !calcMagicSuccess(attacker, target, skill))
  835. {
  836. if (attacker.isPlayer())
  837. {
  838. if (calcMagicSuccess(attacker, target, skill) && ((target.getLevel() - attacker.getLevel()) <= 9))
  839. {
  840. if (skill.hasEffectType(L2EffectType.HP_DRAIN))
  841. {
  842. attacker.sendPacket(SystemMessageId.DRAIN_HALF_SUCCESFUL);
  843. }
  844. else
  845. {
  846. attacker.sendPacket(SystemMessageId.ATTACK_FAILED);
  847. }
  848. damage /= 2;
  849. }
  850. else
  851. {
  852. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
  853. sm.addCharName(target);
  854. sm.addSkillName(skill);
  855. attacker.sendPacket(sm);
  856. damage = 1;
  857. }
  858. }
  859. if (target.isPlayer())
  860. {
  861. final SystemMessage sm = (skill.hasEffectType(L2EffectType.HP_DRAIN)) ? SystemMessage.getSystemMessage(SystemMessageId.RESISTED_C1_DRAIN) : SystemMessage.getSystemMessage(SystemMessageId.RESISTED_C1_MAGIC);
  862. sm.addCharName(attacker);
  863. target.sendPacket(sm);
  864. }
  865. }
  866. else if (mcrit)
  867. {
  868. damage *= attacker.isPlayer() && target.isPlayer() ? 2.5 : 3;
  869. damage *= attacker.calcStat(Stats.MAGIC_CRIT_DMG, 1, null, null);
  870. }
  871. // Weapon random damage
  872. damage *= attacker.getRandomDamageMultiplier();
  873. // PvP bonuses for damage
  874. if (isPvP)
  875. {
  876. Stats stat = skill.isMagic() ? Stats.PVP_MAGICAL_DMG : Stats.PVP_PHYS_SKILL_DMG;
  877. damage *= attacker.calcStat(stat, 1, null, null);
  878. }
  879. damage *= calcAttributeBonus(attacker, target, skill);
  880. if (target.isAttackable())
  881. {
  882. damage *= attacker.calcStat(Stats.PVE_MAGICAL_DMG, 1, null, null);
  883. if (!target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 2))
  884. {
  885. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 1;
  886. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  887. {
  888. damage *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  889. }
  890. else
  891. {
  892. damage *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  893. }
  894. }
  895. }
  896. return damage;
  897. }
  898. public static final double calcMagicDam(L2CubicInstance attacker, L2Character target, Skill skill, boolean mcrit, byte shld)
  899. {
  900. int mDef = target.getMDef(attacker.getOwner(), skill);
  901. switch (shld)
  902. {
  903. case SHIELD_DEFENSE_SUCCEED:
  904. mDef += target.getShldDef(); // kamael
  905. break;
  906. case SHIELD_DEFENSE_PERFECT_BLOCK: // perfect block
  907. return 1;
  908. }
  909. int mAtk = attacker.getCubicPower();
  910. final boolean isPvP = target.isPlayable();
  911. final boolean isPvE = target.isAttackable();
  912. // Cubics MDAM Formula (similar to PDAM formula, but using 91 instead of 70, also resisted by mDef).
  913. double damage = 91 * ((mAtk + skill.getPower(isPvP, isPvE)) / mDef);
  914. // Failure calculation
  915. L2PcInstance owner = attacker.getOwner();
  916. if (Config.ALT_GAME_MAGICFAILURES && !calcMagicSuccess(owner, target, skill))
  917. {
  918. if (calcMagicSuccess(owner, target, skill) && ((target.getLevel() - skill.getMagicLevel()) <= 9))
  919. {
  920. if (skill.hasEffectType(L2EffectType.HP_DRAIN))
  921. {
  922. owner.sendPacket(SystemMessageId.DRAIN_HALF_SUCCESFUL);
  923. }
  924. else
  925. {
  926. owner.sendPacket(SystemMessageId.ATTACK_FAILED);
  927. }
  928. damage /= 2;
  929. }
  930. else
  931. {
  932. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
  933. sm.addCharName(target);
  934. sm.addSkillName(skill);
  935. owner.sendPacket(sm);
  936. damage = 1;
  937. }
  938. if (target.isPlayer())
  939. {
  940. if (skill.hasEffectType(L2EffectType.HP_DRAIN))
  941. {
  942. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.RESISTED_C1_DRAIN);
  943. sm.addCharName(owner);
  944. target.sendPacket(sm);
  945. }
  946. else
  947. {
  948. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.RESISTED_C1_MAGIC);
  949. sm.addCharName(owner);
  950. target.sendPacket(sm);
  951. }
  952. }
  953. }
  954. else if (mcrit)
  955. {
  956. damage *= 3;
  957. }
  958. damage *= calcAttributeBonus(owner, target, skill);
  959. if (target.isAttackable())
  960. {
  961. damage *= attacker.getOwner().calcStat(Stats.PVE_MAGICAL_DMG, 1, null, null);
  962. if (!target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getOwner() != null) && ((target.getLevel() - attacker.getOwner().getLevel()) >= 2))
  963. {
  964. int lvlDiff = target.getLevel() - attacker.getOwner().getLevel() - 1;
  965. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  966. {
  967. damage *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  968. }
  969. else
  970. {
  971. damage *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  972. }
  973. }
  974. }
  975. return damage;
  976. }
  977. /**
  978. * Returns true in case of critical hit
  979. * @param rate
  980. * @param skill
  981. * @param target
  982. * @return
  983. */
  984. public static final boolean calcCrit(double rate, boolean skill, L2Character target)
  985. {
  986. double finalRate = target.getStat().calcStat(Stats.DEFENCE_CRITICAL_RATE, rate, null, null) + target.getStat().calcStat(Stats.DEFENCE_CRITICAL_RATE_ADD, 0, null, null);
  987. return finalRate > Rnd.get(1000);
  988. }
  989. public static final boolean calcMCrit(double mRate)
  990. {
  991. return mRate > Rnd.get(1000);
  992. }
  993. /**
  994. * @param target
  995. * @param dmg
  996. * @return true in case when ATTACK is canceled due to hit
  997. */
  998. public static final boolean calcAtkBreak(L2Character target, double dmg)
  999. {
  1000. if (target.isChanneling())
  1001. {
  1002. return false;
  1003. }
  1004. double init = 0;
  1005. if (Config.ALT_GAME_CANCEL_CAST && target.isCastingNow())
  1006. {
  1007. init = 15;
  1008. }
  1009. if (Config.ALT_GAME_CANCEL_BOW && target.isAttackingNow())
  1010. {
  1011. L2Weapon wpn = target.getActiveWeaponItem();
  1012. if ((wpn != null) && (wpn.getItemType() == WeaponType.BOW))
  1013. {
  1014. init = 15;
  1015. }
  1016. }
  1017. if (target.isRaid() || target.isInvul() || (init <= 0))
  1018. {
  1019. return false; // No attack break
  1020. }
  1021. // Chance of break is higher with higher dmg
  1022. init += Math.sqrt(13 * dmg);
  1023. // Chance is affected by target MEN
  1024. init -= ((BaseStats.MEN.calcBonus(target) * 100) - 100);
  1025. // Calculate all modifiers for ATTACK_CANCEL
  1026. double rate = target.calcStat(Stats.ATTACK_CANCEL, init, null, null);
  1027. // Adjust the rate to be between 1 and 99
  1028. rate = Math.max(Math.min(rate, 99), 1);
  1029. return Rnd.get(100) < rate;
  1030. }
  1031. /**
  1032. * Calculate delay (in milliseconds) before next ATTACK
  1033. * @param attacker
  1034. * @param target
  1035. * @param rate
  1036. * @return
  1037. */
  1038. public static final int calcPAtkSpd(L2Character attacker, L2Character target, double rate)
  1039. {
  1040. // measured Oct 2006 by Tank6585, formula by Sami
  1041. // attack speed 312 equals 1500 ms delay... (or 300 + 40 ms delay?)
  1042. if (rate < 2)
  1043. {
  1044. return 2700;
  1045. }
  1046. return (int) (470000 / rate);
  1047. }
  1048. /**
  1049. * Calculate delay (in milliseconds) for skills cast
  1050. * @param attacker
  1051. * @param skill
  1052. * @param skillTime
  1053. * @return
  1054. */
  1055. public static final int calcAtkSpd(L2Character attacker, Skill skill, double skillTime)
  1056. {
  1057. if (skill.isMagic())
  1058. {
  1059. return (int) ((skillTime / attacker.getMAtkSpd()) * 333);
  1060. }
  1061. return (int) ((skillTime / attacker.getPAtkSpd()) * 300);
  1062. }
  1063. /**
  1064. * Formula based on http://l2p.l2wh.com/nonskillattacks.html
  1065. * @param attacker
  1066. * @param target
  1067. * @return {@code true} if hit missed (target evaded), {@code false} otherwise.
  1068. */
  1069. public static boolean calcHitMiss(L2Character attacker, L2Character target)
  1070. {
  1071. int chance = (80 + (2 * (attacker.getAccuracy() - target.getEvasionRate(attacker)))) * 10;
  1072. // Get additional bonus from the conditions when you are attacking
  1073. chance *= HitConditionBonusData.getInstance().getConditionBonus(attacker, target);
  1074. chance = Math.max(chance, 200);
  1075. chance = Math.min(chance, 980);
  1076. return chance < Rnd.get(1000);
  1077. }
  1078. /**
  1079. * Returns:<br>
  1080. * 0 = shield defense doesn't succeed<br>
  1081. * 1 = shield defense succeed<br>
  1082. * 2 = perfect block<br>
  1083. * @param attacker
  1084. * @param target
  1085. * @param skill
  1086. * @param sendSysMsg
  1087. * @return
  1088. */
  1089. public static byte calcShldUse(L2Character attacker, L2Character target, Skill skill, boolean sendSysMsg)
  1090. {
  1091. if ((skill != null) && skill.ignoreShield())
  1092. {
  1093. return 0;
  1094. }
  1095. L2Item item = target.getSecondaryWeaponItem();
  1096. if ((item == null) || !(item instanceof L2Armor) || (((L2Armor) item).getItemType() == ArmorType.SIGIL))
  1097. {
  1098. return 0;
  1099. }
  1100. double shldRate = target.calcStat(Stats.SHIELD_RATE, 0, attacker, null) * BaseStats.DEX.calcBonus(target);
  1101. if (shldRate <= 1e-6)
  1102. {
  1103. return 0;
  1104. }
  1105. int degreeside = (int) target.calcStat(Stats.SHIELD_DEFENCE_ANGLE, 0, null, null) + 120;
  1106. if ((degreeside < 360) && (!target.isFacing(attacker, degreeside)))
  1107. {
  1108. return 0;
  1109. }
  1110. byte shldSuccess = SHIELD_DEFENSE_FAILED;
  1111. // if attacker
  1112. // if attacker use bow and target wear shield, shield block rate is multiplied by 1.3 (30%)
  1113. L2Weapon at_weapon = attacker.getActiveWeaponItem();
  1114. if ((at_weapon != null) && (at_weapon.getItemType() == WeaponType.BOW))
  1115. {
  1116. shldRate *= 1.3;
  1117. }
  1118. if ((shldRate > 0) && ((100 - Config.ALT_PERFECT_SHLD_BLOCK) < Rnd.get(100)))
  1119. {
  1120. shldSuccess = SHIELD_DEFENSE_PERFECT_BLOCK;
  1121. }
  1122. else if (shldRate > Rnd.get(100))
  1123. {
  1124. shldSuccess = SHIELD_DEFENSE_SUCCEED;
  1125. }
  1126. if (sendSysMsg && target.isPlayer())
  1127. {
  1128. L2PcInstance enemy = target.getActingPlayer();
  1129. switch (shldSuccess)
  1130. {
  1131. case SHIELD_DEFENSE_SUCCEED:
  1132. enemy.sendPacket(SystemMessageId.SHIELD_DEFENCE_SUCCESSFULL);
  1133. break;
  1134. case SHIELD_DEFENSE_PERFECT_BLOCK:
  1135. enemy.sendPacket(SystemMessageId.YOUR_EXCELLENT_SHIELD_DEFENSE_WAS_A_SUCCESS);
  1136. break;
  1137. }
  1138. }
  1139. return shldSuccess;
  1140. }
  1141. public static byte calcShldUse(L2Character attacker, L2Character target, Skill skill)
  1142. {
  1143. return calcShldUse(attacker, target, skill, true);
  1144. }
  1145. public static byte calcShldUse(L2Character attacker, L2Character target)
  1146. {
  1147. return calcShldUse(attacker, target, null, true);
  1148. }
  1149. public static boolean calcMagicAffected(L2Character actor, L2Character target, Skill skill)
  1150. {
  1151. // TODO: CHECK/FIX THIS FORMULA UP!!
  1152. double defence = 0;
  1153. if (skill.isActive() && skill.isBad())
  1154. {
  1155. defence = target.getMDef(actor, skill);
  1156. }
  1157. double attack = 2 * actor.getMAtk(target, skill) * calcGeneralTraitBonus(actor, target, skill.getTraitType(), false);
  1158. double d = (attack - defence) / (attack + defence);
  1159. if (skill.isDebuff())
  1160. {
  1161. if (target.calcStat(Stats.DEBUFF_IMMUNITY, 0, null, skill) > 0)
  1162. {
  1163. return false;
  1164. }
  1165. }
  1166. d += 0.5 * Rnd.nextGaussian();
  1167. return d > 0;
  1168. }
  1169. public static double calcLvlBonusMod(L2Character attacker, L2Character target, Skill skill)
  1170. {
  1171. int attackerLvl = skill.getMagicLevel() > 0 ? skill.getMagicLevel() : attacker.getLevel();
  1172. double skillLvlBonusRateMod = 1 + (skill.getLvlBonusRate() / 100.);
  1173. double lvlMod = 1 + ((attackerLvl - target.getLevel()) / 100.);
  1174. return skillLvlBonusRateMod * lvlMod;
  1175. }
  1176. /**
  1177. * Calculates the effect landing success.<br>
  1178. * @param attacker the attacker
  1179. * @param target the target
  1180. * @param skill the skill
  1181. * @return {@code true} if the effect lands
  1182. */
  1183. public static boolean calcEffectSuccess(L2Character attacker, L2Character target, Skill skill)
  1184. {
  1185. // StaticObjects can not receive continuous effects.
  1186. if (target.isDoor() || (target instanceof L2SiegeFlagInstance) || (target instanceof L2StaticObjectInstance))
  1187. {
  1188. return false;
  1189. }
  1190. if (skill.isDebuff() && (target.calcStat(Stats.DEBUFF_IMMUNITY, 0, attacker, skill) > 0))
  1191. {
  1192. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
  1193. sm.addCharName(target);
  1194. sm.addSkillName(skill);
  1195. attacker.sendPacket(sm);
  1196. return false;
  1197. }
  1198. final int activateRate = skill.getActivateRate();
  1199. if ((activateRate == -1) || (skill.getBasicProperty() == BaseStats.NONE))
  1200. {
  1201. return true;
  1202. }
  1203. int magicLevel = skill.getMagicLevel();
  1204. if (magicLevel <= -1)
  1205. {
  1206. magicLevel = target.getLevel() + 3;
  1207. }
  1208. int targetBaseStat = 0;
  1209. switch (skill.getBasicProperty())
  1210. {
  1211. case STR:
  1212. targetBaseStat = target.getSTR();
  1213. break;
  1214. case DEX:
  1215. targetBaseStat = target.getDEX();
  1216. break;
  1217. case CON:
  1218. targetBaseStat = target.getCON();
  1219. break;
  1220. case INT:
  1221. targetBaseStat = target.getINT();
  1222. break;
  1223. case MEN:
  1224. targetBaseStat = target.getMEN();
  1225. break;
  1226. case WIT:
  1227. targetBaseStat = target.getWIT();
  1228. break;
  1229. }
  1230. final double baseMod = ((((((magicLevel - target.getLevel()) + 3) * skill.getLvlBonusRate()) + activateRate) + 30.0) - targetBaseStat);
  1231. final double elementMod = calcAttributeBonus(attacker, target, skill);
  1232. final double traitMod = calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false);
  1233. final double buffDebuffMod = 1 + (target.calcStat(skill.isDebuff() ? Stats.DEBUFF_VULN : Stats.BUFF_VULN, 1, null, null) / 100);
  1234. double mAtkMod = 1;
  1235. if (skill.isMagic())
  1236. {
  1237. double mAtk = attacker.getMAtk(null, null);
  1238. double val = 0;
  1239. if (attacker.isChargedShot(ShotType.BLESSED_SPIRITSHOTS))
  1240. {
  1241. val = mAtk * 3.0;// 3.0 is the blessed spiritshot multiplier
  1242. }
  1243. val += mAtk;
  1244. val = (Math.sqrt(val) / target.getMDef(null, null)) * 11.0;
  1245. mAtkMod = val;
  1246. }
  1247. final double rate = baseMod * elementMod * traitMod * mAtkMod * buffDebuffMod;
  1248. final double finalRate = traitMod > 0 ? Util.constrain(rate, skill.getMinChance(), skill.getMaxChance()) : 0;
  1249. if (attacker.isDebug())
  1250. {
  1251. final StatsSet set = new StatsSet();
  1252. set.set("baseMod", baseMod);
  1253. set.set("elementMod", elementMod);
  1254. set.set("traitMod", traitMod);
  1255. set.set("mAtkMod", mAtkMod);
  1256. set.set("buffDebuffMod", buffDebuffMod);
  1257. set.set("rate", rate);
  1258. set.set("finalRate", finalRate);
  1259. Debug.sendSkillDebug(attacker, target, skill, set);
  1260. }
  1261. if (finalRate <= Rnd.get(100))
  1262. {
  1263. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_RESISTED_YOUR_S2);
  1264. sm.addCharName(target);
  1265. sm.addSkillName(skill);
  1266. attacker.sendPacket(sm);
  1267. return false;
  1268. }
  1269. return true;
  1270. }
  1271. public static boolean calcCubicSkillSuccess(L2CubicInstance attacker, L2Character target, Skill skill, byte shld)
  1272. {
  1273. if (skill.isDebuff())
  1274. {
  1275. if (skill.getPower() == -1)
  1276. {
  1277. return true;
  1278. }
  1279. else if (target.calcStat(Stats.DEBUFF_IMMUNITY, 0, null, skill) > 0)
  1280. {
  1281. return false;
  1282. }
  1283. }
  1284. // Perfect Shield Block.
  1285. if (shld == SHIELD_DEFENSE_PERFECT_BLOCK)
  1286. {
  1287. return false;
  1288. }
  1289. // if target reflect this skill then the effect will fail
  1290. if (calcBuffDebuffReflection(target, skill))
  1291. {
  1292. return false;
  1293. }
  1294. // Calculate BaseRate.
  1295. double baseRate = skill.getPower();
  1296. double statMod = skill.getBasicProperty().calcBonus(target);
  1297. double rate = (baseRate / statMod);
  1298. // Resist Modifier.
  1299. double resMod = calcGeneralTraitBonus(attacker.getOwner(), target, skill.getTraitType(), false);
  1300. rate *= resMod;
  1301. // Lvl Bonus Modifier.
  1302. double lvlBonusMod = calcLvlBonusMod(attacker.getOwner(), target, skill);
  1303. rate *= lvlBonusMod;
  1304. // Element Modifier.
  1305. double elementMod = calcAttributeBonus(attacker.getOwner(), target, skill);
  1306. rate *= elementMod;
  1307. // Add Matk/Mdef Bonus (TODO: Pending)
  1308. // Check the Rate Limits.
  1309. final double finalRate = Util.constrain(rate, skill.getMinChance(), skill.getMaxChance());
  1310. if (attacker.getOwner().isDebug())
  1311. {
  1312. final StatsSet set = new StatsSet();
  1313. set.set("baseMod", baseRate);
  1314. set.set("resMod", resMod);
  1315. set.set("statMod", statMod);
  1316. set.set("elementMod", elementMod);
  1317. set.set("lvlBonusMod", lvlBonusMod);
  1318. set.set("rate", rate);
  1319. set.set("finalRate", finalRate);
  1320. Debug.sendSkillDebug(attacker.getOwner(), target, skill, set);
  1321. }
  1322. return (Rnd.get(100) < finalRate);
  1323. }
  1324. public static boolean calcMagicSuccess(L2Character attacker, L2Character target, Skill skill)
  1325. {
  1326. if (skill.getPower() == -1)
  1327. {
  1328. return true;
  1329. }
  1330. // FIXME: Fix this LevelMod Formula.
  1331. int lvlDifference = (target.getLevel() - (skill.getMagicLevel() > 0 ? skill.getMagicLevel() : attacker.getLevel()));
  1332. double lvlModifier = Math.pow(1.3, lvlDifference);
  1333. float targetModifier = 1;
  1334. if (target.isAttackable() && !target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_MAGIC_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 3))
  1335. {
  1336. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 2;
  1337. if (lvlDiff >= Config.NPC_SKILL_CHANCE_PENALTY.size())
  1338. {
  1339. targetModifier = Config.NPC_SKILL_CHANCE_PENALTY.get(Config.NPC_SKILL_CHANCE_PENALTY.size() - 1);
  1340. }
  1341. else
  1342. {
  1343. targetModifier = Config.NPC_SKILL_CHANCE_PENALTY.get(lvlDiff);
  1344. }
  1345. }
  1346. // general magic resist
  1347. final double resModifier = target.calcStat(Stats.MAGIC_SUCCESS_RES, 1, null, skill);
  1348. int rate = 100 - Math.round((float) (lvlModifier * targetModifier * resModifier));
  1349. if (attacker.isDebug())
  1350. {
  1351. final StatsSet set = new StatsSet();
  1352. set.set("lvlDifference", lvlDifference);
  1353. set.set("lvlModifier", lvlModifier);
  1354. set.set("resModifier", resModifier);
  1355. set.set("targetModifier", targetModifier);
  1356. set.set("rate", rate);
  1357. Debug.sendSkillDebug(attacker, target, skill, set);
  1358. }
  1359. return (Rnd.get(100) < rate);
  1360. }
  1361. public static double calcManaDam(L2Character attacker, L2Character target, Skill skill, byte shld, boolean sps, boolean bss, boolean mcrit)
  1362. {
  1363. // Formula: (SQR(M.Atk)*Power*(Target Max MP/97))/M.Def
  1364. double mAtk = attacker.getMAtk(target, skill);
  1365. double mDef = target.getMDef(attacker, skill);
  1366. final boolean isPvP = attacker.isPlayable() && target.isPlayable();
  1367. final boolean isPvE = attacker.isPlayable() && target.isAttackable();
  1368. double mp = target.getMaxMp();
  1369. switch (shld)
  1370. {
  1371. case SHIELD_DEFENSE_SUCCEED:
  1372. mDef += target.getShldDef();
  1373. break;
  1374. case SHIELD_DEFENSE_PERFECT_BLOCK: // perfect block
  1375. return 1;
  1376. }
  1377. // Bonus Spiritshot
  1378. mAtk *= bss ? 4 : sps ? 2 : 1;
  1379. double damage = (Math.sqrt(mAtk) * skill.getPower(attacker, target, isPvP, isPvE) * (mp / 97)) / mDef;
  1380. damage *= calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false);
  1381. if (target.isAttackable())
  1382. {
  1383. damage *= attacker.calcStat(Stats.PVE_MAGICAL_DMG, 1, null, null);
  1384. if (!target.isRaid() && !target.isRaidMinion() && (target.getLevel() >= Config.MIN_NPC_LVL_DMG_PENALTY) && (attacker.getActingPlayer() != null) && ((target.getLevel() - attacker.getActingPlayer().getLevel()) >= 2))
  1385. {
  1386. int lvlDiff = target.getLevel() - attacker.getActingPlayer().getLevel() - 1;
  1387. if (lvlDiff >= Config.NPC_SKILL_DMG_PENALTY.size())
  1388. {
  1389. damage *= Config.NPC_SKILL_DMG_PENALTY.get(Config.NPC_SKILL_DMG_PENALTY.size() - 1);
  1390. }
  1391. else
  1392. {
  1393. damage *= Config.NPC_SKILL_DMG_PENALTY.get(lvlDiff);
  1394. }
  1395. }
  1396. }
  1397. // Failure calculation
  1398. if (Config.ALT_GAME_MAGICFAILURES && !calcMagicSuccess(attacker, target, skill))
  1399. {
  1400. if (attacker.isPlayer())
  1401. {
  1402. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.DAMAGE_DECREASED_BECAUSE_C1_RESISTED_C2_MAGIC);
  1403. sm.addCharName(target);
  1404. sm.addCharName(attacker);
  1405. attacker.sendPacket(sm);
  1406. damage /= 2;
  1407. }
  1408. if (target.isPlayer())
  1409. {
  1410. SystemMessage sm2 = SystemMessage.getSystemMessage(SystemMessageId.C1_WEAKLY_RESISTED_C2_MAGIC);
  1411. sm2.addCharName(target);
  1412. sm2.addCharName(attacker);
  1413. target.sendPacket(sm2);
  1414. }
  1415. }
  1416. if (mcrit)
  1417. {
  1418. damage *= 3;
  1419. attacker.sendPacket(SystemMessageId.CRITICAL_HIT_MAGIC);
  1420. }
  1421. return damage;
  1422. }
  1423. public static double calculateSkillResurrectRestorePercent(double baseRestorePercent, L2Character caster)
  1424. {
  1425. if ((baseRestorePercent == 0) || (baseRestorePercent == 100))
  1426. {
  1427. return baseRestorePercent;
  1428. }
  1429. double restorePercent = baseRestorePercent * BaseStats.WIT.calcBonus(caster);
  1430. if ((restorePercent - baseRestorePercent) > 20.0)
  1431. {
  1432. restorePercent += 20.0;
  1433. }
  1434. restorePercent = Math.max(restorePercent, baseRestorePercent);
  1435. restorePercent = Math.min(restorePercent, 90.0);
  1436. return restorePercent;
  1437. }
  1438. public static boolean calcPhysicalSkillEvasion(L2Character activeChar, L2Character target, Skill skill)
  1439. {
  1440. if (skill.isMagic() || skill.isDebuff())
  1441. {
  1442. return false;
  1443. }
  1444. if (Rnd.get(100) < target.calcStat(Stats.P_SKILL_EVASION, 0, null, skill))
  1445. {
  1446. if (activeChar.isPlayer())
  1447. {
  1448. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_DODGES_ATTACK);
  1449. sm.addString(target.getName());
  1450. activeChar.getActingPlayer().sendPacket(sm);
  1451. }
  1452. if (target.isPlayer())
  1453. {
  1454. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.AVOIDED_C1_ATTACK2);
  1455. sm.addString(activeChar.getName());
  1456. target.getActingPlayer().sendPacket(sm);
  1457. }
  1458. return true;
  1459. }
  1460. return false;
  1461. }
  1462. public static boolean calcSkillMastery(L2Character actor, Skill sk)
  1463. {
  1464. // Static Skills are not affected by Skill Mastery.
  1465. if (sk.isStatic())
  1466. {
  1467. return false;
  1468. }
  1469. final int val = (int) actor.getStat().calcStat(Stats.SKILL_CRITICAL, 0, null, null);
  1470. if (val == 0)
  1471. {
  1472. return false;
  1473. }
  1474. if (actor.isPlayer())
  1475. {
  1476. double initVal = 0;
  1477. switch (val)
  1478. {
  1479. case 1:
  1480. {
  1481. initVal = (BaseStats.STR).calcBonus(actor);
  1482. break;
  1483. }
  1484. case 4:
  1485. {
  1486. initVal = (BaseStats.INT).calcBonus(actor);
  1487. break;
  1488. }
  1489. }
  1490. initVal *= actor.getStat().calcStat(Stats.SKILL_CRITICAL_PROBABILITY, 1, null, null);
  1491. return (Rnd.get(100) < initVal);
  1492. }
  1493. return false;
  1494. }
  1495. /**
  1496. * Calculates the Attribute Bonus
  1497. * @param attacker
  1498. * @param target
  1499. * @param skill Can be {@code null} if there is no skill used for the attack.
  1500. * @return The attribute bonus
  1501. */
  1502. public static double calcAttributeBonus(L2Character attacker, L2Character target, Skill skill)
  1503. {
  1504. int attack_attribute;
  1505. int defence_attribute;
  1506. if (skill != null)
  1507. {
  1508. if (skill.getElement() == -1)
  1509. {
  1510. attack_attribute = 0;
  1511. defence_attribute = target.getDefenseElementValue((byte) -1);
  1512. }
  1513. else
  1514. {
  1515. if (attacker.getAttackElement() == skill.getElement())
  1516. {
  1517. attack_attribute = attacker.getAttackElementValue(attacker.getAttackElement()) + skill.getElementPower();
  1518. defence_attribute = target.getDefenseElementValue(attacker.getAttackElement());
  1519. }
  1520. else
  1521. {
  1522. attack_attribute = skill.getElementPower();
  1523. defence_attribute = target.getDefenseElementValue(skill.getElement());
  1524. }
  1525. }
  1526. }
  1527. else
  1528. {
  1529. attack_attribute = attacker.getAttackElementValue(attacker.getAttackElement());
  1530. defence_attribute = target.getDefenseElementValue(attacker.getAttackElement());
  1531. }
  1532. double attack_attribute_mod = 0;
  1533. double defence_attribute_mod = 0;
  1534. if (attack_attribute >= 450)
  1535. {
  1536. if (defence_attribute >= 450)
  1537. {
  1538. attack_attribute_mod = 0.06909;
  1539. defence_attribute_mod = 0.078;
  1540. }
  1541. // On retail else if (attack_attribute >= 350), can be considered a typo
  1542. else if (defence_attribute >= 350)
  1543. {
  1544. attack_attribute_mod = 0.0887;
  1545. defence_attribute_mod = 0.1007;
  1546. }
  1547. else
  1548. {
  1549. attack_attribute_mod = 0.129;
  1550. defence_attribute_mod = 0.1473;
  1551. }
  1552. }
  1553. else if (attack_attribute >= 300)
  1554. {
  1555. if (defence_attribute >= 300)
  1556. {
  1557. attack_attribute_mod = 0.0887;
  1558. defence_attribute_mod = 0.1007;
  1559. }
  1560. else if (defence_attribute >= 150)
  1561. {
  1562. attack_attribute_mod = 0.129;
  1563. defence_attribute_mod = 0.1473;
  1564. }
  1565. else
  1566. {
  1567. attack_attribute_mod = 0.25;
  1568. defence_attribute_mod = 0.2894;
  1569. }
  1570. }
  1571. else if (attack_attribute >= 150)
  1572. {
  1573. if (defence_attribute >= 150)
  1574. {
  1575. attack_attribute_mod = 0.129;
  1576. defence_attribute_mod = 0.1473;
  1577. }
  1578. else if (defence_attribute >= 0)
  1579. {
  1580. attack_attribute_mod = 0.25;
  1581. defence_attribute_mod = 0.2894;
  1582. }
  1583. else
  1584. {
  1585. attack_attribute_mod = 0.4;
  1586. defence_attribute_mod = 0.55;
  1587. }
  1588. }
  1589. else if (attack_attribute >= -99)
  1590. {
  1591. if (defence_attribute >= 0)
  1592. {
  1593. attack_attribute_mod = 0.25;
  1594. defence_attribute_mod = 0.2894;
  1595. }
  1596. else
  1597. {
  1598. attack_attribute_mod = 0.4;
  1599. defence_attribute_mod = 0.55;
  1600. }
  1601. }
  1602. else
  1603. {
  1604. if (defence_attribute >= 450)
  1605. {
  1606. attack_attribute_mod = 0.06909;
  1607. defence_attribute_mod = 0.078;
  1608. }
  1609. else if (defence_attribute >= 350)
  1610. {
  1611. attack_attribute_mod = 0.0887;
  1612. defence_attribute_mod = 0.1007;
  1613. }
  1614. else
  1615. {
  1616. attack_attribute_mod = 0.129;
  1617. defence_attribute_mod = 0.1473;
  1618. }
  1619. }
  1620. int attribute_diff = attack_attribute - defence_attribute;
  1621. double min;
  1622. double max;
  1623. if (attribute_diff >= 300)
  1624. {
  1625. max = 100.0;
  1626. min = -50;
  1627. }
  1628. else if (attribute_diff >= 150)
  1629. {
  1630. max = 70.0;
  1631. min = -50;
  1632. }
  1633. else if (attribute_diff >= -150)
  1634. {
  1635. max = 40.0;
  1636. min = -50;
  1637. }
  1638. else if (attribute_diff >= -300)
  1639. {
  1640. max = 40.0;
  1641. min = -60;
  1642. }
  1643. else
  1644. {
  1645. max = 40.0;
  1646. min = -80;
  1647. }
  1648. attack_attribute += 100;
  1649. attack_attribute *= attack_attribute;
  1650. attack_attribute_mod = (attack_attribute / 144.0) * attack_attribute_mod;
  1651. defence_attribute += 100;
  1652. defence_attribute *= defence_attribute;
  1653. defence_attribute_mod = (defence_attribute / 169.0) * defence_attribute_mod;
  1654. double attribute_mod_diff = attack_attribute_mod - defence_attribute_mod;
  1655. attribute_mod_diff = Util.constrain(attribute_mod_diff, min, max);
  1656. double result = (attribute_mod_diff / 100.0) + 1;
  1657. if (attacker.isPlayer() && target.isPlayer() && (result < 1.0))
  1658. {
  1659. result = 1.0;
  1660. }
  1661. return result;
  1662. }
  1663. public static void calcDamageReflected(L2Character attacker, L2Character target, Skill skill, boolean crit)
  1664. {
  1665. // Only melee skills can be reflected
  1666. if (skill.isMagic() || (skill.getCastRange() > MELEE_ATTACK_RANGE))
  1667. {
  1668. return;
  1669. }
  1670. final double chance = target.calcStat(Stats.VENGEANCE_SKILL_PHYSICAL_DAMAGE, 0, target, skill);
  1671. if (Rnd.get(100) < chance)
  1672. {
  1673. if (target.isPlayer())
  1674. {
  1675. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.COUNTERED_C1_ATTACK);
  1676. sm.addCharName(attacker);
  1677. target.sendPacket(sm);
  1678. }
  1679. if (attacker.isPlayer())
  1680. {
  1681. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_PERFORMING_COUNTERATTACK);
  1682. sm.addCharName(target);
  1683. attacker.sendPacket(sm);
  1684. }
  1685. double counterdmg = (((target.getPAtk(attacker) * 10.0) * 70.0) / attacker.getPDef(target));
  1686. counterdmg *= calcWeaponTraitBonus(attacker, target);
  1687. counterdmg *= calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false);
  1688. counterdmg *= calcAttributeBonus(attacker, target, skill);
  1689. attacker.reduceCurrentHp(counterdmg, target, skill);
  1690. if (crit) // TODO: It counters multiple times depending on how much effects skill has not on critical, but gotta be verified first!
  1691. {
  1692. attacker.reduceCurrentHp(counterdmg, target, skill);
  1693. }
  1694. }
  1695. }
  1696. /**
  1697. * Calculate buff/debuff reflection.
  1698. * @param target
  1699. * @param skill
  1700. * @return {@code true} if reflect, {@code false} otherwise.
  1701. */
  1702. public static boolean calcBuffDebuffReflection(L2Character target, Skill skill)
  1703. {
  1704. if (!skill.isDebuff() || (skill.getActivateRate() == -1))
  1705. {
  1706. return false;
  1707. }
  1708. final double reflectChance = target.calcStat(skill.isMagic() ? Stats.REFLECT_SKILL_MAGIC : Stats.REFLECT_SKILL_PHYSIC, 0, null, skill);
  1709. return reflectChance > Rnd.get(100);
  1710. }
  1711. /**
  1712. * Calculate damage caused by falling
  1713. * @param cha
  1714. * @param fallHeight
  1715. * @return damage
  1716. */
  1717. public static double calcFallDam(L2Character cha, int fallHeight)
  1718. {
  1719. if (!Config.ENABLE_FALLING_DAMAGE || (fallHeight < 0))
  1720. {
  1721. return 0;
  1722. }
  1723. final double damage = cha.calcStat(Stats.FALL, (fallHeight * cha.getMaxHp()) / 1000.0, null, null);
  1724. return damage;
  1725. }
  1726. public static boolean calcBlowSuccess(L2Character activeChar, L2Character target, Skill skill)
  1727. {
  1728. double dexMod = BaseStats.DEX.calcBonus(activeChar);
  1729. // Apply DEX Mod.
  1730. double blowChance = skill.getBlowChance();
  1731. // Apply Position Bonus (TODO: values are unconfirmed, possibly custom, remove or update when confirmed).
  1732. double sideMod = (activeChar.isInFrontOfTarget()) ? 1 : (activeChar.isBehindTarget()) ? 2 : 1.5;
  1733. // Apply all mods.
  1734. double baseRate = blowChance * dexMod * sideMod;
  1735. // Apply blow rates
  1736. double rate = activeChar.calcStat(Stats.BLOW_RATE, baseRate, target, null);
  1737. // Debug
  1738. if (activeChar.isDebug())
  1739. {
  1740. final StatsSet set = new StatsSet();
  1741. set.set("dexMod", dexMod);
  1742. set.set("blowChance", blowChance);
  1743. set.set("sideMod", sideMod);
  1744. set.set("baseRate", baseRate);
  1745. set.set("rate", rate);
  1746. Debug.sendSkillDebug(activeChar, target, skill, set);
  1747. }
  1748. return Rnd.get(100) < rate;
  1749. }
  1750. public static List<BuffInfo> calcCancelStealEffects(L2Character activeChar, L2Character target, Skill skill, String slot, int rate, int max)
  1751. {
  1752. final List<BuffInfo> canceled = new ArrayList<>(max);
  1753. switch (slot)
  1754. {
  1755. case "buff":
  1756. {
  1757. // Resist Modifier.
  1758. int cancelMagicLvl = skill.getMagicLevel();
  1759. final double vuln = target.calcStat(Stats.CANCEL_VULN, 0, target, null);
  1760. final double prof = activeChar.calcStat(Stats.CANCEL_PROF, 0, target, null);
  1761. double resMod = 1 + (((vuln + prof) * -1) / 100);
  1762. double finalRate = rate / resMod;
  1763. if (activeChar.isDebug())
  1764. {
  1765. final StatsSet set = new StatsSet();
  1766. set.set("baseMod", rate);
  1767. set.set("magicLevel", cancelMagicLvl);
  1768. set.set("resMod", resMod);
  1769. set.set("rate", finalRate);
  1770. Debug.sendSkillDebug(activeChar, target, skill, set);
  1771. }
  1772. // Prevent initialization.
  1773. final List<BuffInfo> buffs = target.getEffectList().hasBuffs() ? new ArrayList<>(target.getEffectList().getBuffs()) : new ArrayList<>(1);
  1774. if (target.getEffectList().hasTriggered())
  1775. {
  1776. buffs.addAll(target.getEffectList().getTriggered());
  1777. }
  1778. if (target.getEffectList().hasDances())
  1779. {
  1780. buffs.addAll(target.getEffectList().getDances());
  1781. }
  1782. for (int i = buffs.size() - 1; i >= 0; i--) // reverse order
  1783. {
  1784. BuffInfo info = buffs.get(i);
  1785. if (!info.getSkill().canBeStolen() || (!calcCancelSuccess(info, cancelMagicLvl, (int) finalRate, skill)))
  1786. {
  1787. continue;
  1788. }
  1789. canceled.add(info);
  1790. if (canceled.size() >= max)
  1791. {
  1792. break;
  1793. }
  1794. }
  1795. break;
  1796. }
  1797. case "debuff":
  1798. {
  1799. final List<BuffInfo> debuffs = new ArrayList<>(target.getEffectList().getDebuffs());
  1800. for (int i = debuffs.size() - 1; i >= 0; i--)
  1801. {
  1802. BuffInfo info = debuffs.get(i);
  1803. if (info.getSkill().isDebuff() && info.getSkill().canBeDispeled() && (Rnd.get(100) <= rate))
  1804. {
  1805. canceled.add(info);
  1806. if (canceled.size() >= max)
  1807. {
  1808. break;
  1809. }
  1810. }
  1811. }
  1812. break;
  1813. }
  1814. }
  1815. return canceled;
  1816. }
  1817. public static boolean calcCancelSuccess(BuffInfo info, int cancelMagicLvl, int rate, Skill skill)
  1818. {
  1819. // Lvl Bonus Modifier.
  1820. rate *= info.getSkill().getMagicLevel() > 0 ? 1 + ((cancelMagicLvl - info.getSkill().getMagicLevel()) / 100.) : 1;
  1821. return Rnd.get(100) < Util.constrain(rate, skill.getMinChance(), skill.getMaxChance());
  1822. }
  1823. /**
  1824. * Calculates the abnormal time for an effect.<br>
  1825. * The abnormal time is taken from the skill definition, and it's global for all effects present in the skills.
  1826. * @param caster the caster
  1827. * @param target the target
  1828. * @param skill the skill
  1829. * @return the time that the effect will last
  1830. */
  1831. public static int calcEffectAbnormalTime(L2Character caster, L2Character target, Skill skill)
  1832. {
  1833. int time = skill.isPassive() || skill.isToggle() ? -1 : skill.getAbnormalTime();
  1834. // An herb buff will affect both master and servitor, but the buff duration will be half of the normal duration.
  1835. // If a servitor is not summoned, the master will receive the full buff duration.
  1836. if ((target != null) && target.isServitor() && skill.isAbnormalInstant())
  1837. {
  1838. time /= 2;
  1839. }
  1840. // If the skill is a mastery skill, the effect will last twice the default time.
  1841. if (Formulas.calcSkillMastery(caster, skill))
  1842. {
  1843. time *= 2;
  1844. }
  1845. // Debuffs Duration Affected by Resistances.
  1846. if ((caster != null) && (target != null) && skill.isDebuff())
  1847. {
  1848. double statMod = skill.getBasicProperty().calcBonus(target);
  1849. double resMod = calcGeneralTraitBonus(caster, target, skill.getTraitType(), false);
  1850. double lvlBonusMod = calcLvlBonusMod(caster, target, skill);
  1851. double elementMod = calcAttributeBonus(caster, target, skill);
  1852. time = (int) Math.ceil(Util.constrain(((time * resMod * lvlBonusMod * elementMod) / statMod), (time * 0.5), time));
  1853. }
  1854. return time;
  1855. }
  1856. /**
  1857. * Calculate Probability in following effects:<br>
  1858. * TargetCancel,<br>
  1859. * TargetMeProbability,<br>
  1860. * SkillTurning,<br>
  1861. * Betray,<br>
  1862. * Bluff,<br>
  1863. * DeleteHate,<br>
  1864. * RandomizeHate,<br>
  1865. * DeleteHateOfMe,<br>
  1866. * TransferHate,<br>
  1867. * Confuse<br>
  1868. * @param baseChance chance from effect parameter
  1869. * @param attacker
  1870. * @param target
  1871. * @param skill
  1872. * @return chance for effect to succeed
  1873. */
  1874. public static boolean calcProbability(double baseChance, L2Character attacker, L2Character target, Skill skill)
  1875. {
  1876. return Rnd.get(100) < ((((((skill.getMagicLevel() + baseChance) - target.getLevel()) + 30) - target.getINT()) * calcAttributeBonus(attacker, target, skill)) * calcGeneralTraitBonus(attacker, target, skill.getTraitType(), false));
  1877. }
  1878. /**
  1879. * Calculates karma lost upon death.
  1880. * @param player
  1881. * @param exp
  1882. * @return the amount of karma player has loosed.
  1883. */
  1884. public static int calculateKarmaLost(L2PcInstance player, long exp)
  1885. {
  1886. double karmaLooseMul = KarmaData.getInstance().getMultiplier(player.getLevel());
  1887. if (exp > 0) // Received exp
  1888. {
  1889. exp /= Config.RATE_KARMA_LOST;
  1890. }
  1891. return (int) ((Math.abs(exp) / karmaLooseMul) / 30);
  1892. }
  1893. /**
  1894. * Calculates karma gain upon playable kill.</br> Updated to High Five on 10.09.2014 by Zealar tested in retail.
  1895. * @param pkCount
  1896. * @param isSummon
  1897. * @return karma points that will be added to the player.
  1898. */
  1899. public static int calculateKarmaGain(int pkCount, boolean isSummon)
  1900. {
  1901. int result = 43200;
  1902. if (isSummon)
  1903. {
  1904. result = (int) ((((pkCount * 0.375) + 1) * 60) * 4) - 150;
  1905. if (result > 10800)
  1906. {
  1907. return 10800;
  1908. }
  1909. }
  1910. if (pkCount < 99)
  1911. {
  1912. result = (int) ((((pkCount * 0.5) + 1) * 60) * 12);
  1913. }
  1914. else if (pkCount < 180)
  1915. {
  1916. result = (int) ((((pkCount * 0.125) + 37.75) * 60) * 12);
  1917. }
  1918. return result;
  1919. }
  1920. public static double calcGeneralTraitBonus(L2Character attacker, L2Character target, TraitType traitType, boolean ignoreResistance)
  1921. {
  1922. if (traitType == TraitType.NONE)
  1923. {
  1924. return 1.0;
  1925. }
  1926. if (target.getStat().isTraitInvul(traitType))
  1927. {
  1928. return 0;
  1929. }
  1930. switch (traitType.getType())
  1931. {
  1932. case 2:
  1933. {
  1934. if (!attacker.getStat().hasAttackTrait(traitType) || !target.getStat().hasDefenceTrait(traitType))
  1935. {
  1936. return 1.0;
  1937. }
  1938. break;
  1939. }
  1940. case 3:
  1941. {
  1942. if (ignoreResistance)
  1943. {
  1944. return 1.0;
  1945. }
  1946. break;
  1947. }
  1948. default:
  1949. {
  1950. return 1.0;
  1951. }
  1952. }
  1953. final double result = (attacker.getStat().getAttackTrait(traitType) - target.getStat().getDefenceTrait(traitType)) + 1.0;
  1954. return Util.constrain(result, 0.05, 2.0);
  1955. }
  1956. public static double calcWeaponTraitBonus(L2Character attacker, L2Character target)
  1957. {
  1958. final TraitType type = attacker.getAttackType().getTraitType();
  1959. double result = target.getStat().getDefenceTraits()[type.getId()] - 1.0;
  1960. return 1.0 - result;
  1961. }
  1962. public static double calcAttackTraitBonus(L2Character attacker, L2Character target)
  1963. {
  1964. final double weaponTraitBonus = calcWeaponTraitBonus(attacker, target);
  1965. if (weaponTraitBonus == 0)
  1966. {
  1967. return 0;
  1968. }
  1969. double weaknessBonus = 1.0;
  1970. for (TraitType traitType : TraitType.values())
  1971. {
  1972. if (traitType.getType() == 2)
  1973. {
  1974. weaknessBonus *= calcGeneralTraitBonus(attacker, target, traitType, true);
  1975. if (weaknessBonus == 0)
  1976. {
  1977. return 0;
  1978. }
  1979. }
  1980. }
  1981. return Util.constrain((weaponTraitBonus * weaknessBonus), 0.05, 2.0);
  1982. }
  1983. }