MonsterRace.java 3.5 KB

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