armark1ng

Untitled

Sep 4th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. package com.rs.game.map.bossInstance;
  2.  
  3. import java.util.List;
  4. import java.util.concurrent.CopyOnWriteArrayList;
  5.  
  6. import com.rs.game.WorldTile;
  7. import com.rs.game.map.MapInstance;
  8. import com.rs.game.map.bossInstance.BossInstanceHandler.Boss;
  9. import com.rs.game.player.Player;
  10.  
  11. public abstract class BossInstance {
  12.  
  13. /*
  14. * spawn speed
  15. */
  16. public static final int STANDARD = 1, FAST = 2, FASTEST = 4;
  17. public static final int FFA = 0, PIN = 1, FRIENDS_ONLY = 2;
  18.  
  19. public static int STARTING = 0, RUNNING = 1, FINISHED = 2;
  20.  
  21. // null if real world
  22. private MapInstance map;
  23. private Player owner;
  24. private List<Player> players;
  25. private InstanceSettings settings;
  26. private int stage;
  27.  
  28. /*
  29. * creates instance
  30. */
  31. public BossInstance(Player owner, InstanceSettings settings) {
  32. this.owner = owner;
  33. this.settings = settings;
  34. players = new CopyOnWriteArrayList<Player>();
  35. if (!isPublic())
  36. owner.setLastBossInstanceSettings(settings);
  37. init();
  38. }
  39.  
  40. public InstanceSettings getSettings() {
  41. return settings;
  42. }
  43.  
  44. public int getPlayersCount() {
  45. return players.size();
  46. }
  47.  
  48. public void init() {
  49. if (owner != null) {
  50. owner.lock();
  51. int[] pos = getMapPos();
  52. int[] size = getMapSize();
  53. setMapInstance(pos[0], pos[1], size[0], size[1]);
  54. map.load(new Runnable() {
  55. @Override
  56. public void run() {
  57. loadMapInstance();
  58. stage = RUNNING;
  59. enterInstance(owner, false);
  60. }
  61. });
  62. } else {
  63. loadMapInstance();
  64. stage = RUNNING;
  65. }
  66. }
  67.  
  68. public boolean isInstanceReady() {
  69. return stage == RUNNING;
  70. }
  71.  
  72. public List<Player> getPlayers() {
  73. return players;
  74. }
  75.  
  76. public boolean isPlayerInside(Player player) {
  77. synchronized (BossInstanceHandler.LOCK) {
  78. return players.contains(player);
  79. }
  80. }
  81.  
  82. public abstract int[] getMapPos();
  83.  
  84. // return 1,1 by default
  85. public abstract int[] getMapSize();
  86.  
  87. public void setMapInstance(int chunkX, int chunkY, int ratioX, int ratioY) {
  88. map = new MapInstance(chunkX, chunkY, ratioX, ratioY);
  89. }
  90.  
  91. public abstract void loadMapInstance();
  92.  
  93. public void enterInstance(Player player, boolean login) {
  94. synchronized (BossInstanceHandler.LOCK) {
  95. if (!login)
  96. player.useStairs(-1, getTile(settings.getBoss().getInsideTile()), 0, 2);
  97. players.add(player);
  98. playMusic(player);
  99. if (!isPublic())
  100. player.getPackets().sendGameMessage(
  101. "Welcome to this session agaisnt <col=00FFFF>" + getInstanceName()
  102. + "</col>. This arena will expire in " + (settings.getTimeRemaining() / 60000)
  103. + " minutes.");
  104. if (!login)
  105. player.getControlerManager().startControler(settings.getBoss().getControllerName(), this);
  106. else {
  107. if (settings.getBoss() == Boss.Vorago)
  108. player.getControlerManager().startControler(settings.getBoss().getControllerName(), this);
  109. }
  110. player.setLastBossInstanceKey(owner == null ? null : owner.getUsername());
  111. }
  112. }
  113.  
  114. public void playMusic(Player player) {
  115. player.getMusicsManager().forcePlayMusic(settings.getBoss().getMusicId());
  116. }
  117.  
  118. public String getInstanceName() {
  119. return settings.getBoss().name().replace("_", " ");
  120. }
  121.  
  122. public static final int TELEPORTED = 0, LOGGED_OUT = 1, EXITED = 2, DIED = 3;
  123.  
  124. public void leaveInstance(Player player, int type) {
  125. synchronized (BossInstanceHandler.LOCK) {
  126. if (type == EXITED)
  127. player.useStairs(-1, settings.getBoss().getOutsideTile(), 0, 2);
  128. else if (type == LOGGED_OUT && !isPublic()) // if public no need to
  129. // move the player to
  130. // entrance :p(exept for
  131. // instance vorago but
  132. // thats done at vorago
  133. // instance extend)
  134. player.setLocation(settings.getBoss().getOutsideTile());
  135. player.getMusicsManager().reset();
  136. players.remove(player);
  137. if (players.isEmpty()) // public version
  138. finish();
  139. }
  140. }
  141.  
  142. public void finish() {
  143. if (!isPublic()) {
  144. stage = FINISHED;
  145. map.destroy(null);
  146. settings.getBoss().getCachedInstances().remove(getOwnerUsername());
  147. }
  148. }
  149.  
  150. public WorldTile getTile(WorldTile tile) {
  151. return getTile(tile.getX(), tile.getY(), tile.getPlane());
  152. }
  153.  
  154. // gets the instanced coords of the world tile(notice if the area instanced
  155. // differs from coords it will give wrong tile, the ratio part is to make
  156. // sure the coords are inside map
  157. public WorldTile getTile(int x, int y, int plane) {
  158. WorldTile tile;
  159. if (owner != null) {
  160. int[] originalPos = map.getOriginalPos();
  161. tile = map.getTile((x - originalPos[0] * 8) % (map.getRatioX() * 64),
  162. (y - originalPos[1] * 8) % (map.getRatioY() * 64));
  163. tile.moveLocation(0, 0, plane);
  164. } else
  165. tile = new WorldTile(x, y, plane);
  166. return tile;
  167. }
  168.  
  169. public String getOwnerUsername() {
  170. return owner == null ? "" : owner.getUsername();
  171. }
  172.  
  173. public boolean isPublic() {
  174. return owner == null;
  175. }
  176.  
  177. public Boss getBoss() {
  178. return settings.getBoss();
  179. }
  180.  
  181. public boolean isFinished() {
  182. return stage == FINISHED;
  183. }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment