MonsterRace.java 3.3 KB

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