MonsterRace.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver;
  20. import java.lang.reflect.Constructor;
  21. import net.sf.l2j.gameserver.datatables.NpcTable;
  22. import net.sf.l2j.gameserver.idfactory.IdFactory;
  23. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  24. import net.sf.l2j.gameserver.templates.L2NpcTemplate;
  25. import net.sf.l2j.util.Rnd;
  26. public class MonsterRace
  27. {
  28. private L2NpcInstance[] _monsters;
  29. private static MonsterRace _instance;
  30. private Constructor<?> _constructor;
  31. private int[][] _speeds;
  32. private int[] _first, _second;
  33. private MonsterRace()
  34. {
  35. _monsters = new L2NpcInstance[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. if (_instance == null)
  43. {
  44. _instance = new MonsterRace();
  45. }
  46. return _instance;
  47. }
  48. public void newRace()
  49. {
  50. int random = 0;
  51. for (int i=0; i<8; i++)
  52. {
  53. int id = 31003;
  54. random = Rnd.get(24);
  55. while(true)
  56. {
  57. for (int j=i-1; j>=0; j--)
  58. {
  59. if (_monsters[j].getTemplate().npcId == (id + random))
  60. {
  61. random = Rnd.get(24);
  62. continue;
  63. }
  64. }
  65. break;
  66. }
  67. try
  68. {
  69. L2NpcTemplate template = NpcTable.getInstance().getTemplate(id+random);
  70. _constructor = Class.forName("net.sf.l2j.gameserver.model.actor.instance." + template.type + "Instance").getConstructors()[0];
  71. int objectId = IdFactory.getInstance().getNextId();
  72. _monsters[i] = (L2NpcInstance)_constructor.newInstance(objectId, template);
  73. }
  74. catch (Exception e)
  75. {
  76. e.printStackTrace();
  77. }
  78. //System.out.println("Monster "+i+" is id: "+(id+random));
  79. }
  80. newSpeeds();
  81. }
  82. public void newSpeeds()
  83. {
  84. _speeds = new int[8][20];
  85. int total = 0;
  86. _first[1]=0;_second[1]=0;
  87. for (int i=0; i<8; i++)
  88. {
  89. total = 0;
  90. for (int j=0; j<20 ;j++)
  91. {
  92. if (j == 19)
  93. _speeds[i][j] = 100;
  94. else
  95. _speeds[i][j] = Rnd.get(60) + 65;
  96. total += _speeds[i][j];
  97. }
  98. if (total >= _first[1])
  99. {
  100. _second[0] = _first[0];
  101. _second[1] = _first[1];
  102. _first[0] = 8 - i;
  103. _first[1] = total;
  104. }
  105. else if (total >= _second[1])
  106. {
  107. _second[0] = 8 - i;
  108. _second[1] = total;
  109. }
  110. }
  111. }
  112. /**
  113. * @return Returns the monsters.
  114. */
  115. public L2NpcInstance[] getMonsters()
  116. {
  117. return _monsters;
  118. }
  119. /**
  120. * @return Returns the speeds.
  121. */
  122. public int[][] getSpeeds()
  123. {
  124. return _speeds;
  125. }
  126. public int getFirstPlace()
  127. {
  128. return _first[0];
  129. }
  130. public int getSecondPlace()
  131. {
  132. return _second[0];
  133. }
  134. }