MonsterRace.java 4.0 KB

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