AirShipManager.java 9.3 KB

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