AirShipManager.java 9.6 KB

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