BoatManager.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2004-2015 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.instancemanager;
  20. import java.util.Map;
  21. import java.util.concurrent.ConcurrentHashMap;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.model.L2World;
  24. import com.l2jserver.gameserver.model.StatsSet;
  25. import com.l2jserver.gameserver.model.VehiclePathPoint;
  26. import com.l2jserver.gameserver.model.actor.instance.L2BoatInstance;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.actor.templates.L2CharTemplate;
  29. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  30. public class BoatManager
  31. {
  32. private final Map<Integer, L2BoatInstance> _boats = new ConcurrentHashMap<>();
  33. private final boolean[] _docksBusy = new boolean[3];
  34. public static final int TALKING_ISLAND = 1;
  35. public static final int GLUDIN_HARBOR = 2;
  36. public static final int RUNE_HARBOR = 3;
  37. public static final BoatManager getInstance()
  38. {
  39. return SingletonHolder._instance;
  40. }
  41. protected BoatManager()
  42. {
  43. for (int i = 0; i < _docksBusy.length; i++)
  44. {
  45. _docksBusy[i] = false;
  46. }
  47. }
  48. public L2BoatInstance getNewBoat(int boatId, int x, int y, int z, int heading)
  49. {
  50. if (!Config.ALLOW_BOAT)
  51. {
  52. return null;
  53. }
  54. StatsSet npcDat = new StatsSet();
  55. npcDat.set("npcId", boatId);
  56. npcDat.set("level", 0);
  57. npcDat.set("jClass", "boat");
  58. npcDat.set("baseSTR", 0);
  59. npcDat.set("baseCON", 0);
  60. npcDat.set("baseDEX", 0);
  61. npcDat.set("baseINT", 0);
  62. npcDat.set("baseWIT", 0);
  63. npcDat.set("baseMEN", 0);
  64. npcDat.set("baseShldDef", 0);
  65. npcDat.set("baseShldRate", 0);
  66. npcDat.set("baseAccCombat", 38);
  67. npcDat.set("baseEvasRate", 38);
  68. npcDat.set("baseCritRate", 38);
  69. // npcDat.set("name", "");
  70. npcDat.set("collision_radius", 0);
  71. npcDat.set("collision_height", 0);
  72. npcDat.set("sex", "male");
  73. npcDat.set("type", "");
  74. npcDat.set("baseAtkRange", 0);
  75. npcDat.set("baseMpMax", 0);
  76. npcDat.set("baseCpMax", 0);
  77. npcDat.set("rewardExp", 0);
  78. npcDat.set("rewardSp", 0);
  79. npcDat.set("basePAtk", 0);
  80. npcDat.set("baseMAtk", 0);
  81. npcDat.set("basePAtkSpd", 0);
  82. npcDat.set("aggroRange", 0);
  83. npcDat.set("baseMAtkSpd", 0);
  84. npcDat.set("rhand", 0);
  85. npcDat.set("lhand", 0);
  86. npcDat.set("armor", 0);
  87. npcDat.set("baseWalkSpd", 0);
  88. npcDat.set("baseRunSpd", 0);
  89. npcDat.set("baseHpMax", 50000);
  90. npcDat.set("baseHpReg", 3.e-3f);
  91. npcDat.set("baseMpReg", 3.e-3f);
  92. npcDat.set("basePDef", 100);
  93. npcDat.set("baseMDef", 100);
  94. final L2BoatInstance boat = new L2BoatInstance(new L2CharTemplate(npcDat));
  95. boat.setHeading(heading);
  96. boat.setXYZInvisible(x, y, z);
  97. boat.spawnMe();
  98. _boats.put(boat.getObjectId(), boat);
  99. return boat;
  100. }
  101. /**
  102. * @param boatId
  103. * @return
  104. */
  105. public L2BoatInstance getBoat(int boatId)
  106. {
  107. return _boats.get(boatId);
  108. }
  109. /**
  110. * Lock/unlock dock so only one ship can be docked
  111. * @param h Dock Id
  112. * @param value True if dock is locked
  113. */
  114. public void dockShip(int h, boolean value)
  115. {
  116. try
  117. {
  118. _docksBusy[h] = value;
  119. }
  120. catch (ArrayIndexOutOfBoundsException e)
  121. {
  122. }
  123. }
  124. /**
  125. * Check if dock is busy
  126. * @param h Dock Id
  127. * @return Trye if dock is locked
  128. */
  129. public boolean dockBusy(int h)
  130. {
  131. try
  132. {
  133. return _docksBusy[h];
  134. }
  135. catch (ArrayIndexOutOfBoundsException e)
  136. {
  137. return false;
  138. }
  139. }
  140. /**
  141. * Broadcast one packet in both path points
  142. * @param point1
  143. * @param point2
  144. * @param packet
  145. */
  146. public void broadcastPacket(VehiclePathPoint point1, VehiclePathPoint point2, L2GameServerPacket packet)
  147. {
  148. broadcastPacketsToPlayers(point1, point2, packet);
  149. }
  150. /**
  151. * Broadcast several packets in both path points
  152. * @param point1
  153. * @param point2
  154. * @param packets
  155. */
  156. public void broadcastPackets(VehiclePathPoint point1, VehiclePathPoint point2, L2GameServerPacket... packets)
  157. {
  158. broadcastPacketsToPlayers(point1, point2, packets);
  159. }
  160. private void broadcastPacketsToPlayers(VehiclePathPoint point1, VehiclePathPoint point2, L2GameServerPacket... packets)
  161. {
  162. for (L2PcInstance player : L2World.getInstance().getPlayers())
  163. {
  164. double dx = (double) player.getX() - point1.getX();
  165. double dy = (double) player.getY() - point1.getY();
  166. if (Math.sqrt((dx * dx) + (dy * dy)) < Config.BOAT_BROADCAST_RADIUS)
  167. {
  168. for (L2GameServerPacket p : packets)
  169. {
  170. player.sendPacket(p);
  171. }
  172. }
  173. else
  174. {
  175. dx = (double) player.getX() - point2.getX();
  176. dy = (double) player.getY() - point2.getY();
  177. if (Math.sqrt((dx * dx) + (dy * dy)) < Config.BOAT_BROADCAST_RADIUS)
  178. {
  179. for (L2GameServerPacket p : packets)
  180. {
  181. player.sendPacket(p);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. private static class SingletonHolder
  188. {
  189. protected static final BoatManager _instance = new BoatManager();
  190. }
  191. }