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 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 Map<Integer, StatsSet> _airShipsInfo = new HashMap<>();
  42. private Map<Integer, L2AirShipInstance> _airShips = new HashMap<>();
  43. private 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. return null;
  104. final L2AirShipInstance airShip;
  105. if (_airShips.containsKey(ownerId))
  106. {
  107. airShip = _airShips.get(ownerId);
  108. airShip.refreshID();
  109. }
  110. else
  111. {
  112. airShip = new L2ControllableAirShipInstance(IdFactory.getInstance().getNextId(), _airShipTemplate, ownerId);
  113. _airShips.put(ownerId, airShip);
  114. airShip.setMaxFuel(600);
  115. airShip.setFuel(info.getInteger("fuel"));
  116. airShip.getStat().setMoveSpeed(280);
  117. airShip.getStat().setRotationSpeed(2000);
  118. }
  119. airShip.setHeading(heading);
  120. airShip.setXYZInvisible(x, y, z);
  121. airShip.spawnMe();
  122. return airShip;
  123. }
  124. public void removeAirShip(L2AirShipInstance ship)
  125. {
  126. if (ship.getOwnerId() != 0)
  127. {
  128. storeInDb(ship.getOwnerId());
  129. final StatsSet info = _airShipsInfo.get(ship.getOwnerId());
  130. if (info != null)
  131. info.set("fuel", ship.getFuel());
  132. }
  133. }
  134. public boolean hasAirShipLicense(int ownerId)
  135. {
  136. return _airShipsInfo.containsKey(ownerId);
  137. }
  138. public void registerLicense(int ownerId)
  139. {
  140. if (!_airShipsInfo.containsKey(ownerId))
  141. {
  142. final StatsSet info = new StatsSet();
  143. info.set("fuel", 600);
  144. _airShipsInfo.put(ownerId, info);
  145. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  146. PreparedStatement ps = con.prepareStatement(ADD_DB))
  147. {
  148. ps.setInt(1, ownerId);
  149. ps.setInt(2, info.getInteger("fuel"));
  150. ps.executeUpdate();
  151. }
  152. catch (SQLException e)
  153. {
  154. _log.log(Level.WARNING, getClass().getSimpleName()+": Could not add new airship license: " + e.getMessage(), e);
  155. }
  156. catch (Exception e)
  157. {
  158. _log.log(Level.WARNING, getClass().getSimpleName()+": Error while initializing: " + e.getMessage(), e);
  159. }
  160. }
  161. }
  162. public boolean hasAirShip(int ownerId)
  163. {
  164. final L2AirShipInstance ship = _airShips.get(ownerId);
  165. if (ship == null || !(ship.isVisible() || ship.isTeleporting()))
  166. return false;
  167. return true;
  168. }
  169. public void registerAirShipTeleportList(int dockId, int locationId, VehiclePathPoint[][] tp, int[] fuelConsumption)
  170. {
  171. if (tp.length != fuelConsumption.length)
  172. return;
  173. _teleports.put(dockId, new AirShipTeleportList(locationId, fuelConsumption, tp));
  174. }
  175. public void sendAirShipTeleportList(L2PcInstance player)
  176. {
  177. if (player == null || !player.isInAirShip())
  178. return;
  179. final L2AirShipInstance ship = player.getAirShip();
  180. if (!ship.isCaptain(player) || !ship.isInDock() || ship.isMoving())
  181. return;
  182. int dockId = ship.getDockId();
  183. if (!_teleports.containsKey(dockId))
  184. return;
  185. final AirShipTeleportList all = _teleports.get(dockId);
  186. player.sendPacket(new ExAirShipTeleportList(all.location, all.routes, all.fuel));
  187. }
  188. public VehiclePathPoint[] getTeleportDestination(int dockId, int index)
  189. {
  190. final AirShipTeleportList all = _teleports.get(dockId);
  191. if (all == null)
  192. return null;
  193. if (index < -1 || index >= all.routes.length)
  194. return null;
  195. return all.routes[index + 1];
  196. }
  197. public int getFuelConsumption(int dockId, int index)
  198. {
  199. final AirShipTeleportList all = _teleports.get(dockId);
  200. if (all == null)
  201. return 0;
  202. if (index < -1 || index >= all.fuel.length)
  203. return 0;
  204. return all.fuel[index + 1];
  205. }
  206. private void load()
  207. {
  208. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  209. Statement s = con.createStatement();
  210. ResultSet rs = s.executeQuery(LOAD_DB))
  211. {
  212. StatsSet info;
  213. while (rs.next())
  214. {
  215. info = new StatsSet();
  216. info.set("fuel", rs.getInt("fuel"));
  217. _airShipsInfo.put(rs.getInt("owner_id"), info);
  218. }
  219. }
  220. catch (SQLException e)
  221. {
  222. _log.log(Level.WARNING, getClass().getSimpleName()+": Could not load airships table: " + e.getMessage(), e);
  223. }
  224. catch (Exception e)
  225. {
  226. _log.log(Level.WARNING, getClass().getSimpleName()+": Error while initializing: " + e.getMessage(), e);
  227. }
  228. _log.info(getClass().getSimpleName()+": Loaded " + _airShipsInfo.size() + " private airships");
  229. }
  230. private void storeInDb(int ownerId)
  231. {
  232. StatsSet info = _airShipsInfo.get(ownerId);
  233. if (info == null)
  234. return;
  235. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  236. PreparedStatement ps = con.prepareStatement(UPDATE_DB))
  237. {
  238. ps.setInt(1, info.getInteger("fuel"));
  239. ps.setInt(2, ownerId);
  240. ps.executeUpdate();
  241. }
  242. catch (SQLException e)
  243. {
  244. _log.log(Level.WARNING, getClass().getSimpleName()+": Could not update airships table: " + e.getMessage(), e);
  245. }
  246. catch (Exception e)
  247. {
  248. _log.log(Level.WARNING, getClass().getSimpleName()+": Error while save: " + e.getMessage(), e);
  249. }
  250. }
  251. private static class AirShipTeleportList
  252. {
  253. public int location;
  254. public int[] fuel;
  255. public VehiclePathPoint[][] routes;
  256. public AirShipTeleportList(int loc, int[] f, VehiclePathPoint[][] r)
  257. {
  258. location = loc;
  259. fuel = f;
  260. routes = r;
  261. }
  262. }
  263. public static final AirShipManager getInstance()
  264. {
  265. return SingletonHolder._instance;
  266. }
  267. private static class SingletonHolder
  268. {
  269. protected static final AirShipManager _instance = new AirShipManager();
  270. }
  271. }