AirShipManager.java 9.2 KB

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