MonsterRace.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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;
  20. import java.lang.reflect.Constructor;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import com.l2jserver.gameserver.data.xml.impl.NpcData;
  24. import com.l2jserver.gameserver.model.actor.L2Npc;
  25. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  26. import com.l2jserver.util.Rnd;
  27. public class MonsterRace
  28. {
  29. protected static final Logger _log = LoggerFactory.getLogger(MonsterRace.class);
  30. private final L2Npc[] _monsters;
  31. private int[][] _speeds;
  32. private final int[] _first, _second;
  33. protected MonsterRace()
  34. {
  35. _monsters = new L2Npc[8];
  36. _speeds = new int[8][20];
  37. _first = new int[2];
  38. _second = new int[2];
  39. }
  40. public static MonsterRace getInstance()
  41. {
  42. return SingletonHolder._instance;
  43. }
  44. public void newRace()
  45. {
  46. int random = 0;
  47. for (int i = 0; i < 8; i++)
  48. {
  49. int id = 31003;
  50. random = Rnd.get(24);
  51. while (true)
  52. {
  53. for (int j = i - 1; j >= 0; j--)
  54. {
  55. if (_monsters[j].getTemplate().getId() == (id + random))
  56. {
  57. random = Rnd.get(24);
  58. continue;
  59. }
  60. }
  61. break;
  62. }
  63. try
  64. {
  65. L2NpcTemplate template = NpcData.getInstance().getTemplate(id + random);
  66. Constructor<?> constructor = Class.forName("com.l2jserver.gameserver.model.actor.instance." + template.getType() + "Instance").getConstructors()[0];
  67. _monsters[i] = (L2Npc) constructor.newInstance(template);
  68. }
  69. catch (Exception e)
  70. {
  71. _log.warn("Unable to create monster!", e);
  72. }
  73. }
  74. newSpeeds();
  75. }
  76. public void newSpeeds()
  77. {
  78. _speeds = new int[8][20];
  79. int total = 0;
  80. _first[1] = 0;
  81. _second[1] = 0;
  82. for (int i = 0; i < 8; i++)
  83. {
  84. total = 0;
  85. for (int j = 0; j < 20; j++)
  86. {
  87. if (j == 19)
  88. {
  89. _speeds[i][j] = 100;
  90. }
  91. else
  92. {
  93. _speeds[i][j] = Rnd.get(60) + 65;
  94. }
  95. total += _speeds[i][j];
  96. }
  97. if (total >= _first[1])
  98. {
  99. _second[0] = _first[0];
  100. _second[1] = _first[1];
  101. _first[0] = 8 - i;
  102. _first[1] = total;
  103. }
  104. else if (total >= _second[1])
  105. {
  106. _second[0] = 8 - i;
  107. _second[1] = total;
  108. }
  109. }
  110. }
  111. /**
  112. * @return Returns the monsters.
  113. */
  114. public L2Npc[] getMonsters()
  115. {
  116. return _monsters;
  117. }
  118. /**
  119. * @return Returns the speeds.
  120. */
  121. public int[][] getSpeeds()
  122. {
  123. return _speeds;
  124. }
  125. public int getFirstPlace()
  126. {
  127. return _first[0];
  128. }
  129. public int getSecondPlace()
  130. {
  131. return _second[0];
  132. }
  133. private static class SingletonHolder
  134. {
  135. protected static final MonsterRace _instance = new MonsterRace();
  136. }
  137. }