2
0

BoatManager.java 5.6 KB

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