AirShipManager.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. Connection con = null;
  149. try
  150. {
  151. con = L2DatabaseFactory.getInstance().getConnection();
  152. try (PreparedStatement ps = con.prepareStatement(ADD_DB))
  153. {
  154. ps.setInt(1, ownerId);
  155. ps.setInt(2, info.getInteger("fuel"));
  156. ps.executeUpdate();
  157. }
  158. }
  159. catch (SQLException e)
  160. {
  161. _log.log(Level.WARNING, getClass().getSimpleName()+": Could not add new airship license: " + e.getMessage(), e);
  162. }
  163. catch (Exception e)
  164. {
  165. _log.log(Level.WARNING, getClass().getSimpleName()+": Error while initializing: " + e.getMessage(), e);
  166. }
  167. finally
  168. {
  169. L2DatabaseFactory.close(con);
  170. }
  171. }
  172. }
  173. public boolean hasAirShip(int ownerId)
  174. {
  175. final L2AirShipInstance ship = _airShips.get(ownerId);
  176. if (ship == null || !(ship.isVisible() || ship.isTeleporting()))
  177. return false;
  178. return true;
  179. }
  180. public void registerAirShipTeleportList(int dockId, int locationId, VehiclePathPoint[][] tp, int[] fuelConsumption)
  181. {
  182. if (tp.length != fuelConsumption.length)
  183. return;
  184. _teleports.put(dockId, new AirShipTeleportList(locationId, fuelConsumption, tp));
  185. }
  186. public void sendAirShipTeleportList(L2PcInstance player)
  187. {
  188. if (player == null || !player.isInAirShip())
  189. return;
  190. final L2AirShipInstance ship = player.getAirShip();
  191. if (!ship.isCaptain(player) || !ship.isInDock() || ship.isMoving())
  192. return;
  193. int dockId = ship.getDockId();
  194. if (!_teleports.contains(dockId))
  195. return;
  196. final AirShipTeleportList all = _teleports.get(dockId);
  197. player.sendPacket(new ExAirShipTeleportList(all.location, all.routes, all.fuel));
  198. }
  199. public VehiclePathPoint[] getTeleportDestination(int dockId, int index)
  200. {
  201. final AirShipTeleportList all = _teleports.get(dockId);
  202. if (all == null)
  203. return null;
  204. if (index < -1 || index >= all.routes.length)
  205. return null;
  206. return all.routes[index + 1];
  207. }
  208. public int getFuelConsumption(int dockId, int index)
  209. {
  210. final AirShipTeleportList all = _teleports.get(dockId);
  211. if (all == null)
  212. return 0;
  213. if (index < -1 || index >= all.fuel.length)
  214. return 0;
  215. return all.fuel[index + 1];
  216. }
  217. private void load()
  218. {
  219. Connection con = null;
  220. try
  221. {
  222. con = L2DatabaseFactory.getInstance().getConnection();
  223. try (Statement s = con.createStatement();
  224. ResultSet rs = s.executeQuery(LOAD_DB))
  225. {
  226. StatsSet info;
  227. while (rs.next())
  228. {
  229. info = new StatsSet();
  230. info.set("fuel", rs.getInt("fuel"));
  231. _airShipsInfo.put(rs.getInt("owner_id"), info);
  232. }
  233. }
  234. }
  235. catch (SQLException e)
  236. {
  237. _log.log(Level.WARNING, getClass().getSimpleName()+": Could not load airships table: " + e.getMessage(), e);
  238. }
  239. catch (Exception e)
  240. {
  241. _log.log(Level.WARNING, getClass().getSimpleName()+": Error while initializing: " + e.getMessage(), e);
  242. }
  243. finally
  244. {
  245. L2DatabaseFactory.close(con);
  246. }
  247. _log.info(getClass().getSimpleName()+": Loaded " + _airShipsInfo.size() + " private airships");
  248. }
  249. private void storeInDb(int ownerId)
  250. {
  251. StatsSet info = _airShipsInfo.get(ownerId);
  252. if (info == null)
  253. return;
  254. Connection con = null;
  255. try
  256. {
  257. con = L2DatabaseFactory.getInstance().getConnection();
  258. try (PreparedStatement ps = con.prepareStatement(UPDATE_DB))
  259. {
  260. ps.setInt(1, info.getInteger("fuel"));
  261. ps.setInt(2, ownerId);
  262. ps.executeUpdate();
  263. }
  264. }
  265. catch (SQLException e)
  266. {
  267. _log.log(Level.WARNING, getClass().getSimpleName()+": Could not update airships table: " + e.getMessage(), e);
  268. }
  269. catch (Exception e)
  270. {
  271. _log.log(Level.WARNING, getClass().getSimpleName()+": Error while save: " + e.getMessage(), e);
  272. }
  273. finally
  274. {
  275. L2DatabaseFactory.close(con);
  276. }
  277. }
  278. private static class AirShipTeleportList
  279. {
  280. public int location;
  281. public int[] fuel;
  282. public VehiclePathPoint[][] routes;
  283. public AirShipTeleportList(int loc, int[] f, VehiclePathPoint[][] r)
  284. {
  285. location = loc;
  286. fuel = f;
  287. routes = r;
  288. }
  289. }
  290. private static class SingletonHolder
  291. {
  292. protected static final AirShipManager _instance = new AirShipManager();
  293. }
  294. }