AirShipManager.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.instancemanager;
  16. import gnu.trove.map.hash.TIntObjectHashMap;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.sql.Statement;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.idfactory.IdFactory;
  26. import com.l2jserver.gameserver.model.StatsSet;
  27. import com.l2jserver.gameserver.model.VehiclePathPoint;
  28. import com.l2jserver.gameserver.model.actor.instance.L2AirShipInstance;
  29. import com.l2jserver.gameserver.model.actor.instance.L2ControllableAirShipInstance;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.actor.templates.L2CharTemplate;
  32. import com.l2jserver.gameserver.network.serverpackets.ExAirShipTeleportList;
  33. public class AirShipManager
  34. {
  35. private static final Logger _log = Logger.getLogger(AirShipManager.class.getName());
  36. private static final String LOAD_DB = "SELECT * FROM airships";
  37. private static final String ADD_DB = "INSERT INTO airships (owner_id,fuel) VALUES (?,?)";
  38. private static final String UPDATE_DB = "UPDATE airships SET fuel=? WHERE owner_id=?";
  39. private L2CharTemplate _airShipTemplate = null;
  40. private TIntObjectHashMap<StatsSet> _airShipsInfo = new TIntObjectHashMap<>();
  41. private TIntObjectHashMap<L2AirShipInstance> _airShips = new TIntObjectHashMap<>();
  42. private TIntObjectHashMap<AirShipTeleportList> _teleports = new TIntObjectHashMap<>();
  43. public static final AirShipManager getInstance()
  44. {
  45. return SingletonHolder._instance;
  46. }
  47. protected AirShipManager()
  48. {
  49. StatsSet npcDat = new StatsSet();
  50. npcDat.set("npcId", 9);
  51. npcDat.set("level", 0);
  52. npcDat.set("jClass", "boat");
  53. npcDat.set("baseSTR", 0);
  54. npcDat.set("baseCON", 0);
  55. npcDat.set("baseDEX", 0);
  56. npcDat.set("baseINT", 0);
  57. npcDat.set("baseWIT", 0);
  58. npcDat.set("baseMEN", 0);
  59. npcDat.set("baseShldDef", 0);
  60. npcDat.set("baseShldRate", 0);
  61. npcDat.set("baseAccCombat", 38);
  62. npcDat.set("baseEvasRate", 38);
  63. npcDat.set("baseCritRate", 38);
  64. npcDat.set("collision_radius", 0);
  65. npcDat.set("collision_height", 0);
  66. npcDat.set("sex", "male");
  67. npcDat.set("type", "");
  68. npcDat.set("baseAtkRange", 0);
  69. npcDat.set("baseMpMax", 0);
  70. npcDat.set("baseCpMax", 0);
  71. npcDat.set("rewardExp", 0);
  72. npcDat.set("rewardSp", 0);
  73. npcDat.set("basePAtk", 0);
  74. npcDat.set("baseMAtk", 0);
  75. npcDat.set("basePAtkSpd", 0);
  76. npcDat.set("aggroRange", 0);
  77. npcDat.set("baseMAtkSpd", 0);
  78. npcDat.set("rhand", 0);
  79. npcDat.set("lhand", 0);
  80. npcDat.set("armor", 0);
  81. npcDat.set("baseWalkSpd", 0);
  82. npcDat.set("baseRunSpd", 0);
  83. npcDat.set("name", "AirShip");
  84. npcDat.set("baseHpMax", 50000);
  85. npcDat.set("baseHpReg", 3.e-3f);
  86. npcDat.set("baseMpReg", 3.e-3f);
  87. npcDat.set("basePDef", 100);
  88. npcDat.set("baseMDef", 100);
  89. _airShipTemplate = new L2CharTemplate(npcDat);
  90. load();
  91. }
  92. public L2AirShipInstance getNewAirShip(int x, int y, int z, int heading)
  93. {
  94. final L2AirShipInstance airShip = new L2AirShipInstance(IdFactory.getInstance().getNextId(), _airShipTemplate);
  95. airShip.setHeading(heading);
  96. airShip.setXYZInvisible(x, y, z);
  97. airShip.spawnMe();
  98. airShip.getStat().setMoveSpeed(280);
  99. airShip.getStat().setRotationSpeed(2000);
  100. return airShip;
  101. }
  102. public L2AirShipInstance getNewAirShip(int x, int y, int z, int heading, int ownerId)
  103. {
  104. final StatsSet info = _airShipsInfo.get(ownerId);
  105. if (info == null)
  106. return null;
  107. final L2AirShipInstance airShip;
  108. if (_airShips.containsKey(ownerId))
  109. {
  110. airShip = _airShips.get(ownerId);
  111. airShip.refreshID();
  112. }
  113. else
  114. {
  115. airShip = new L2ControllableAirShipInstance(IdFactory.getInstance().getNextId(), _airShipTemplate, ownerId);
  116. _airShips.put(ownerId, airShip);
  117. airShip.setMaxFuel(600);
  118. airShip.setFuel(info.getInteger("fuel"));
  119. airShip.getStat().setMoveSpeed(280);
  120. airShip.getStat().setRotationSpeed(2000);
  121. }
  122. airShip.setHeading(heading);
  123. airShip.setXYZInvisible(x, y, z);
  124. airShip.spawnMe();
  125. return airShip;
  126. }
  127. public void removeAirShip(L2AirShipInstance ship)
  128. {
  129. if (ship.getOwnerId() != 0)
  130. {
  131. storeInDb(ship.getOwnerId());
  132. final StatsSet info = _airShipsInfo.get(ship.getOwnerId());
  133. if (info != null)
  134. info.set("fuel", ship.getFuel());
  135. }
  136. }
  137. public boolean hasAirShipLicense(int ownerId)
  138. {
  139. return _airShipsInfo.contains(ownerId);
  140. }
  141. public void registerLicense(int ownerId)
  142. {
  143. if (!_airShipsInfo.contains(ownerId))
  144. {
  145. final StatsSet info = new StatsSet();
  146. info.set("fuel", 600);
  147. _airShipsInfo.put(ownerId, info);
  148. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  149. PreparedStatement ps = con.prepareStatement(ADD_DB))
  150. {
  151. ps.setInt(1, ownerId);
  152. ps.setInt(2, info.getInteger("fuel"));
  153. ps.executeUpdate();
  154. }
  155. catch (SQLException e)
  156. {
  157. _log.log(Level.WARNING, getClass().getSimpleName()+": Could not add new airship license: " + e.getMessage(), e);
  158. }
  159. catch (Exception e)
  160. {
  161. _log.log(Level.WARNING, getClass().getSimpleName()+": Error while initializing: " + e.getMessage(), e);
  162. }
  163. }
  164. }
  165. public boolean hasAirShip(int ownerId)
  166. {
  167. final L2AirShipInstance ship = _airShips.get(ownerId);
  168. if (ship == null || !(ship.isVisible() || ship.isTeleporting()))
  169. return false;
  170. return true;
  171. }
  172. public void registerAirShipTeleportList(int dockId, int locationId, VehiclePathPoint[][] tp, int[] fuelConsumption)
  173. {
  174. if (tp.length != fuelConsumption.length)
  175. return;
  176. _teleports.put(dockId, new AirShipTeleportList(locationId, fuelConsumption, tp));
  177. }
  178. public void sendAirShipTeleportList(L2PcInstance player)
  179. {
  180. if (player == null || !player.isInAirShip())
  181. return;
  182. final L2AirShipInstance ship = player.getAirShip();
  183. if (!ship.isCaptain(player) || !ship.isInDock() || ship.isMoving())
  184. return;
  185. int dockId = ship.getDockId();
  186. if (!_teleports.contains(dockId))
  187. return;
  188. final AirShipTeleportList all = _teleports.get(dockId);
  189. player.sendPacket(new ExAirShipTeleportList(all.location, all.routes, all.fuel));
  190. }
  191. public VehiclePathPoint[] getTeleportDestination(int dockId, int index)
  192. {
  193. final AirShipTeleportList all = _teleports.get(dockId);
  194. if (all == null)
  195. return null;
  196. if (index < -1 || index >= all.routes.length)
  197. return null;
  198. return all.routes[index + 1];
  199. }
  200. public int getFuelConsumption(int dockId, int index)
  201. {
  202. final AirShipTeleportList all = _teleports.get(dockId);
  203. if (all == null)
  204. return 0;
  205. if (index < -1 || index >= all.fuel.length)
  206. return 0;
  207. return all.fuel[index + 1];
  208. }
  209. private void load()
  210. {
  211. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  212. Statement s = con.createStatement();
  213. ResultSet rs = s.executeQuery(LOAD_DB))
  214. {
  215. StatsSet info;
  216. while (rs.next())
  217. {
  218. info = new StatsSet();
  219. info.set("fuel", rs.getInt("fuel"));
  220. _airShipsInfo.put(rs.getInt("owner_id"), info);
  221. }
  222. }
  223. catch (SQLException e)
  224. {
  225. _log.log(Level.WARNING, getClass().getSimpleName()+": Could not load airships table: " + e.getMessage(), e);
  226. }
  227. catch (Exception e)
  228. {
  229. _log.log(Level.WARNING, getClass().getSimpleName()+": Error while initializing: " + e.getMessage(), e);
  230. }
  231. _log.info(getClass().getSimpleName()+": Loaded " + _airShipsInfo.size() + " private airships");
  232. }
  233. private void storeInDb(int ownerId)
  234. {
  235. StatsSet info = _airShipsInfo.get(ownerId);
  236. if (info == null)
  237. return;
  238. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  239. PreparedStatement ps = con.prepareStatement(UPDATE_DB))
  240. {
  241. ps.setInt(1, info.getInteger("fuel"));
  242. ps.setInt(2, ownerId);
  243. ps.executeUpdate();
  244. }
  245. catch (SQLException e)
  246. {
  247. _log.log(Level.WARNING, getClass().getSimpleName()+": Could not update airships table: " + e.getMessage(), e);
  248. }
  249. catch (Exception e)
  250. {
  251. _log.log(Level.WARNING, getClass().getSimpleName()+": Error while save: " + e.getMessage(), e);
  252. }
  253. }
  254. private static class AirShipTeleportList
  255. {
  256. public int location;
  257. public int[] fuel;
  258. public VehiclePathPoint[][] routes;
  259. public AirShipTeleportList(int loc, int[] f, VehiclePathPoint[][] r)
  260. {
  261. location = loc;
  262. fuel = f;
  263. routes = r;
  264. }
  265. }
  266. private static class SingletonHolder
  267. {
  268. protected static final AirShipManager _instance = new AirShipManager();
  269. }
  270. }