MonsterRace.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver;
  16. import java.lang.reflect.Constructor;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.gameserver.datatables.NpcTable;
  20. import com.l2jserver.gameserver.idfactory.IdFactory;
  21. import com.l2jserver.gameserver.model.actor.L2Npc;
  22. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  23. import com.l2jserver.util.Rnd;
  24. public class MonsterRace
  25. {
  26. protected static final Logger _log = Logger.getLogger(MonsterRace.class.getName());
  27. private L2Npc[] _monsters;
  28. private Constructor<?> _constructor;
  29. private int[][] _speeds;
  30. private int[] _first, _second;
  31. protected MonsterRace()
  32. {
  33. _monsters = new L2Npc[8];
  34. _speeds = new int[8][20];
  35. _first = new int[2];
  36. _second = new int[2];
  37. }
  38. public static MonsterRace getInstance()
  39. {
  40. return SingletonHolder._instance;
  41. }
  42. public void newRace()
  43. {
  44. int random = 0;
  45. for (int i = 0; i < 8; i++)
  46. {
  47. int id = 31003;
  48. random = Rnd.get(24);
  49. while (true)
  50. {
  51. for (int j = i - 1; j >= 0; j--)
  52. {
  53. if (_monsters[j].getTemplate().getNpcId() == (id + random))
  54. {
  55. random = Rnd.get(24);
  56. continue;
  57. }
  58. }
  59. break;
  60. }
  61. try
  62. {
  63. L2NpcTemplate template = NpcTable.getInstance().getTemplate(id + random);
  64. _constructor = Class.forName("com.l2jserver.gameserver.model.actor.instance." + template.getType() + "Instance").getConstructors()[0];
  65. int objectId = IdFactory.getInstance().getNextId();
  66. _monsters[i] = (L2Npc) _constructor.newInstance(objectId, template);
  67. }
  68. catch (Exception e)
  69. {
  70. _log.log(Level.WARNING, "", e);
  71. }
  72. //_log.info("Monster "+i+" is id: "+(id+random));
  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. _speeds[i][j] = 100;
  89. else
  90. _speeds[i][j] = Rnd.get(60) + 65;
  91. total += _speeds[i][j];
  92. }
  93. if (total >= _first[1])
  94. {
  95. _second[0] = _first[0];
  96. _second[1] = _first[1];
  97. _first[0] = 8 - i;
  98. _first[1] = total;
  99. }
  100. else if (total >= _second[1])
  101. {
  102. _second[0] = 8 - i;
  103. _second[1] = total;
  104. }
  105. }
  106. }
  107. /**
  108. * @return Returns the monsters.
  109. */
  110. public L2Npc[] getMonsters()
  111. {
  112. return _monsters;
  113. }
  114. /**
  115. * @return Returns the speeds.
  116. */
  117. public int[][] getSpeeds()
  118. {
  119. return _speeds;
  120. }
  121. public int getFirstPlace()
  122. {
  123. return _first[0];
  124. }
  125. public int getSecondPlace()
  126. {
  127. return _second[0];
  128. }
  129. private static class SingletonHolder
  130. {
  131. protected static final MonsterRace _instance = new MonsterRace();
  132. }
  133. }