AirShipManager.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  30. import com.l2jserver.gameserver.model.AirShipTeleportList;
  31. import com.l2jserver.gameserver.model.StatsSet;
  32. import com.l2jserver.gameserver.model.VehiclePathPoint;
  33. import com.l2jserver.gameserver.model.actor.instance.L2AirShipInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2ControllableAirShipInstance;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. import com.l2jserver.gameserver.model.actor.templates.L2CharTemplate;
  37. import com.l2jserver.gameserver.network.serverpackets.ExAirShipTeleportList;
  38. public class AirShipManager
  39. {
  40. private static final Logger _log = Logger.getLogger(AirShipManager.class.getName());
  41. private static final String LOAD_DB = "SELECT * FROM airships";
  42. private static final String ADD_DB = "INSERT INTO airships (owner_id,fuel) VALUES (?,?)";
  43. private static final String UPDATE_DB = "UPDATE airships SET fuel=? WHERE owner_id=?";
  44. private L2CharTemplate _airShipTemplate = null;
  45. private final Map<Integer, StatsSet> _airShipsInfo = new HashMap<>();
  46. private final Map<Integer, L2AirShipInstance> _airShips = new HashMap<>();
  47. private final Map<Integer, AirShipTeleportList> _teleports = new HashMap<>();
  48. protected AirShipManager()
  49. {
  50. StatsSet npcDat = new StatsSet();
  51. npcDat.set("npcId", 9);
  52. npcDat.set("level", 0);
  53. npcDat.set("jClass", "boat");
  54. npcDat.set("baseSTR", 0);
  55. npcDat.set("baseCON", 0);
  56. npcDat.set("baseDEX", 0);
  57. npcDat.set("baseINT", 0);
  58. npcDat.set("baseWIT", 0);
  59. npcDat.set("baseMEN", 0);
  60. npcDat.set("baseShldDef", 0);
  61. npcDat.set("baseShldRate", 0);
  62. npcDat.set("baseAccCombat", 38);
  63. npcDat.set("baseEvasRate", 38);
  64. npcDat.set("baseCritRate", 38);
  65. npcDat.set("collision_radius", 0);
  66. npcDat.set("collision_height", 0);
  67. npcDat.set("sex", "male");
  68. npcDat.set("type", "");
  69. npcDat.set("baseAtkRange", 0);
  70. npcDat.set("baseMpMax", 0);
  71. npcDat.set("baseCpMax", 0);
  72. npcDat.set("rewardExp", 0);
  73. npcDat.set("rewardSp", 0);
  74. npcDat.set("basePAtk", 0);
  75. npcDat.set("baseMAtk", 0);
  76. npcDat.set("basePAtkSpd", 0);
  77. npcDat.set("aggroRange", 0);
  78. npcDat.set("baseMAtkSpd", 0);
  79. npcDat.set("rhand", 0);
  80. npcDat.set("lhand", 0);
  81. npcDat.set("armor", 0);
  82. npcDat.set("baseWalkSpd", 0);
  83. npcDat.set("baseRunSpd", 0);
  84. npcDat.set("name", "AirShip");
  85. npcDat.set("baseHpMax", 50000);
  86. npcDat.set("baseHpReg", 3.e-3f);
  87. npcDat.set("baseMpReg", 3.e-3f);
  88. npcDat.set("basePDef", 100);
  89. npcDat.set("baseMDef", 100);
  90. _airShipTemplate = new L2CharTemplate(npcDat);
  91. load();
  92. }
  93. public L2AirShipInstance getNewAirShip(int x, int y, int z, int heading)
  94. {
  95. final L2AirShipInstance airShip = new L2AirShipInstance(_airShipTemplate);
  96. airShip.setHeading(heading);
  97. airShip.setXYZInvisible(x, y, z);
  98. airShip.spawnMe();
  99. airShip.getStat().setMoveSpeed(280);
  100. airShip.getStat().setRotationSpeed(2000);
  101. return airShip;
  102. }
  103. public L2AirShipInstance getNewAirShip(int x, int y, int z, int heading, int ownerId)
  104. {
  105. final StatsSet info = _airShipsInfo.get(ownerId);
  106. if (info == null)
  107. {
  108. return null;
  109. }
  110. final L2AirShipInstance airShip;
  111. if (_airShips.containsKey(ownerId))
  112. {
  113. airShip = _airShips.get(ownerId);
  114. airShip.refreshID();
  115. }
  116. else
  117. {
  118. airShip = new L2ControllableAirShipInstance(_airShipTemplate, ownerId);
  119. _airShips.put(ownerId, airShip);
  120. airShip.setMaxFuel(600);
  121. airShip.setFuel(info.getInt("fuel"));
  122. airShip.getStat().setMoveSpeed(280);
  123. airShip.getStat().setRotationSpeed(2000);
  124. }
  125. airShip.setHeading(heading);
  126. airShip.setXYZInvisible(x, y, z);
  127. airShip.spawnMe();
  128. return airShip;
  129. }
  130. public void removeAirShip(L2AirShipInstance ship)
  131. {
  132. if (ship.getOwnerId() != 0)
  133. {
  134. storeInDb(ship.getOwnerId());
  135. final StatsSet info = _airShipsInfo.get(ship.getOwnerId());
  136. if (info != null)
  137. {
  138. info.set("fuel", ship.getFuel());
  139. }
  140. }
  141. }
  142. public boolean hasAirShipLicense(int ownerId)
  143. {
  144. return _airShipsInfo.containsKey(ownerId);
  145. }
  146. public void registerLicense(int ownerId)
  147. {
  148. if (!_airShipsInfo.containsKey(ownerId))
  149. {
  150. final StatsSet info = new StatsSet();
  151. info.set("fuel", 600);
  152. _airShipsInfo.put(ownerId, info);
  153. try (Connection con = ConnectionFactory.getInstance().getConnection();
  154. PreparedStatement ps = con.prepareStatement(ADD_DB))
  155. {
  156. ps.setInt(1, ownerId);
  157. ps.setInt(2, info.getInt("fuel"));
  158. ps.executeUpdate();
  159. }
  160. catch (SQLException e)
  161. {
  162. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not add new airship license: " + e.getMessage(), e);
  163. }
  164. catch (Exception e)
  165. {
  166. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error while initializing: " + e.getMessage(), e);
  167. }
  168. }
  169. }
  170. public boolean hasAirShip(int ownerId)
  171. {
  172. final L2AirShipInstance ship = _airShips.get(ownerId);
  173. if ((ship == null) || !(ship.isVisible() || ship.isTeleporting()))
  174. {
  175. return false;
  176. }
  177. return true;
  178. }
  179. public void registerAirShipTeleportList(int dockId, int locationId, VehiclePathPoint[][] tp, int[] fuelConsumption)
  180. {
  181. if (tp.length != fuelConsumption.length)
  182. {
  183. return;
  184. }
  185. _teleports.put(dockId, new AirShipTeleportList(locationId, fuelConsumption, tp));
  186. }
  187. public void sendAirShipTeleportList(L2PcInstance player)
  188. {
  189. if ((player == null) || !player.isInAirShip())
  190. {
  191. return;
  192. }
  193. final L2AirShipInstance ship = player.getAirShip();
  194. if (!ship.isCaptain(player) || !ship.isInDock() || ship.isMoving())
  195. {
  196. return;
  197. }
  198. int dockId = ship.getDockId();
  199. if (!_teleports.containsKey(dockId))
  200. {
  201. return;
  202. }
  203. final AirShipTeleportList all = _teleports.get(dockId);
  204. player.sendPacket(new ExAirShipTeleportList(all.getLocation(), all.getRoute(), all.getFuel()));
  205. }
  206. public VehiclePathPoint[] getTeleportDestination(int dockId, int index)
  207. {
  208. final AirShipTeleportList all = _teleports.get(dockId);
  209. if (all == null)
  210. {
  211. return null;
  212. }
  213. if ((index < -1) || (index >= all.getRoute().length))
  214. {
  215. return null;
  216. }
  217. return all.getRoute()[index + 1];
  218. }
  219. public int getFuelConsumption(int dockId, int index)
  220. {
  221. final AirShipTeleportList all = _teleports.get(dockId);
  222. if (all == null)
  223. {
  224. return 0;
  225. }
  226. if ((index < -1) || (index >= all.getFuel().length))
  227. {
  228. return 0;
  229. }
  230. return all.getFuel()[index + 1];
  231. }
  232. private void load()
  233. {
  234. try (Connection con = ConnectionFactory.getInstance().getConnection();
  235. Statement s = con.createStatement();
  236. ResultSet rs = s.executeQuery(LOAD_DB))
  237. {
  238. StatsSet info;
  239. while (rs.next())
  240. {
  241. info = new StatsSet();
  242. info.set("fuel", rs.getInt("fuel"));
  243. _airShipsInfo.put(rs.getInt("owner_id"), info);
  244. }
  245. }
  246. catch (SQLException e)
  247. {
  248. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not load airships table: " + e.getMessage(), e);
  249. }
  250. catch (Exception e)
  251. {
  252. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error while initializing: " + e.getMessage(), e);
  253. }
  254. _log.info(getClass().getSimpleName() + ": Loaded " + _airShipsInfo.size() + " private airships");
  255. }
  256. private void storeInDb(int ownerId)
  257. {
  258. StatsSet info = _airShipsInfo.get(ownerId);
  259. if (info == null)
  260. {
  261. return;
  262. }
  263. try (Connection con = ConnectionFactory.getInstance().getConnection();
  264. PreparedStatement ps = con.prepareStatement(UPDATE_DB))
  265. {
  266. ps.setInt(1, info.getInt("fuel"));
  267. ps.setInt(2, ownerId);
  268. ps.executeUpdate();
  269. }
  270. catch (SQLException e)
  271. {
  272. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not update airships table: " + e.getMessage(), e);
  273. }
  274. catch (Exception e)
  275. {
  276. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error while save: " + e.getMessage(), e);
  277. }
  278. }
  279. public static final AirShipManager getInstance()
  280. {
  281. return SingletonHolder._instance;
  282. }
  283. private static class SingletonHolder
  284. {
  285. protected static final AirShipManager _instance = new AirShipManager();
  286. }
  287. }