Advertisement
Nik

Fences core 5685

Nik
Oct 24th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 16.71 KB | None | 0 0
  1. Index: java/com/l2jserver/gameserver/GameServer.java
  2. ===================================================================
  3. --- java/com/l2jserver/gameserver/GameServer.java   (revision 5685)
  4. +++ java/com/l2jserver/gameserver/GameServer.java   (working copy)
  5. @@ -49,6 +49,7 @@
  6.  import com.l2jserver.gameserver.datatables.EnchantOptionsData;
  7.  import com.l2jserver.gameserver.datatables.EventDroplist;
  8.  import com.l2jserver.gameserver.datatables.ExperienceTable;
  9. +import com.l2jserver.gameserver.datatables.FencesTable;
  10.  import com.l2jserver.gameserver.datatables.FishData;
  11.  import com.l2jserver.gameserver.datatables.FishingMonstersData;
  12.  import com.l2jserver.gameserver.datatables.FishingRodsData;
  13. @@ -256,7 +257,7 @@
  14.             PathFinding.getInstance();
  15.         }
  16.        
  17. -       printSection("NPCs");
  18. +       printSection("WorldObjects");
  19.         HerbDropTable.getInstance();
  20.         NpcTable.getInstance();
  21.         NpcWalkerRoutesData.getInstance();
  22. @@ -269,6 +270,7 @@
  23.         FortManager.getInstance().loadInstances();
  24.         NpcBufferTable.getInstance();
  25.         SpawnTable.getInstance();
  26. +       FencesTable.getInstance();
  27.         HellboundManager.getInstance();
  28.         RaidBossSpawnManager.getInstance();
  29.         DayNightSpawnManager.getInstance().trim().notifyChangeMode();
  30. Index: java/com/l2jserver/gameserver/datatables/FencesTable.java
  31. ===================================================================
  32. --- java/com/l2jserver/gameserver/datatables/FencesTable.java   (revision 0)
  33. +++ java/com/l2jserver/gameserver/datatables/FencesTable.java   (revision 0)
  34. @@ -0,0 +1,159 @@
  35. +/*
  36. + * This program is free software: you can redistribute it and/or modify it under
  37. + * the terms of the GNU General Public License as published by the Free Software
  38. + * Foundation, either version 3 of the License, or (at your option) any later
  39. + * version.
  40. + *
  41. + * This program is distributed in the hope that it will be useful, but WITHOUT
  42. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  43. + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  44. + * details.
  45. + *
  46. + * You should have received a copy of the GNU General Public License along with
  47. + * this program. If not, see <http://www.gnu.org/licenses/>.
  48. + */
  49. +package com.l2jserver.gameserver.datatables;
  50. +
  51. +import java.sql.Connection;
  52. +import java.sql.PreparedStatement;
  53. +import java.sql.ResultSet;
  54. +import java.sql.SQLException;
  55. +import java.util.HashMap;
  56. +import java.util.Map;
  57. +import java.util.logging.Level;
  58. +import java.util.logging.Logger;
  59. +
  60. +import com.l2jserver.L2DatabaseFactory;
  61. +import com.l2jserver.gameserver.model.actor.instance.L2FenceInstance;
  62. +import com.l2jserver.util.StringUtil;
  63. +
  64. +/**
  65. + * @author Nik based on Christian's work.
  66. + */
  67. +public class FencesTable
  68. +{
  69. +   protected static final Logger _log = Logger.getLogger(FencesTable.class.getName());
  70. +  
  71. +   private static final class SingletonHolder
  72. +   {
  73. +       protected static final FencesTable _instance = new FencesTable();
  74. +   }
  75. +  
  76. +   public static FencesTable getInstance()
  77. +   {
  78. +       return SingletonHolder._instance;
  79. +   }
  80. +  
  81. +   private final Map<Integer, L2FenceInstance> _fences = new HashMap<>();
  82. +  
  83. +   protected FencesTable()
  84. +   {
  85. +       load();
  86. +       _log.log(Level.INFO, StringUtil.concat("Loaded ", String.valueOf(_fences.size()), " stored fences."));
  87. +   }
  88. +  
  89. +   private void load()
  90. +   {
  91. +       try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  92. +           PreparedStatement stmt = con.prepareStatement("SELECT * FROM fences"))
  93. +       {
  94. +           ResultSet res = stmt.executeQuery();
  95. +           while (res.next())
  96. +           {
  97. +               L2FenceInstance fence = new L2FenceInstance(res.getInt("object_id"), res.getInt("state"), res.getInt("x"), res.getInt("y"), res.getInt("z"), res.getInt("width"), res.getInt("length"), res.getInt("height"));
  98. +               fence.setStoredToDB(true); // They are loaded from DB, therefore they are stored there.
  99. +               fence.spawnMe();
  100. +           }
  101. +       }
  102. +       catch (SQLException sqle)
  103. +       {
  104. +           _log.log(Level.WARNING, "Exception while loading fences: ", sqle);
  105. +       }
  106. +   }
  107. +  
  108. +   public void reload()
  109. +   {
  110. +       for (L2FenceInstance fence : _fences.values())
  111. +       {
  112. +           // Do not delete temp fences, they aren't stored in DB therefore they won't be reloaded, but deleted.
  113. +           if (!fence.isStoredToDB())
  114. +           {
  115. +               fence.decayMe();
  116. +           }
  117. +       }
  118. +      
  119. +       load();
  120. +   }
  121. +  
  122. +   public void addFence(L2FenceInstance fence)
  123. +   {
  124. +       _fences.put(fence.getObjectId(), fence);
  125. +   }
  126. +  
  127. +   public void getFence(int objectId)
  128. +   {
  129. +       _fences.get(objectId);
  130. +   }
  131. +  
  132. +   public L2FenceInstance removeFence(int objectId)
  133. +   {
  134. +       return _fences.remove(objectId);
  135. +   }
  136. +  
  137. +   public void deleteFence(int objectId, boolean deleteFromDB)
  138. +   {
  139. +       L2FenceInstance fence = removeFence(objectId);
  140. +       if (fence != null)
  141. +       {
  142. +           fence.decayMe();
  143. +       }
  144. +      
  145. +       if (deleteFromDB)
  146. +       {
  147. +           deleteStoredFence(objectId);
  148. +       }
  149. +   }
  150. +  
  151. +   /**
  152. +    * @param fence - Stores the fence to the database.
  153. +    */
  154. +   public void storeFence(L2FenceInstance fence)
  155. +   {
  156. +       try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  157. +           PreparedStatement stmt = con.prepareStatement("INSERT INTO fences (object_id,state,x,y,z,width,length,height) VALUES(?,?,?,?,?,?,?,?)"))
  158. +       {
  159. +           stmt.setInt(1, fence.getObjectId());
  160. +           stmt.setInt(2, fence.getX());
  161. +           stmt.setInt(3, fence.getY());
  162. +           stmt.setInt(4, fence.getZ());
  163. +           stmt.setInt(5, fence.getWidth());
  164. +           stmt.setInt(6, fence.getLength());
  165. +           stmt.setInt(6, fence.getHeight());
  166. +           stmt.executeUpdate();
  167. +       }
  168. +       catch (SQLException sqle)
  169. +       {
  170. +           _log.log(Level.WARNING, "Error while storing fence:", sqle);
  171. +       }
  172. +   }
  173. +  
  174. +   public boolean deleteStoredFence(int objectId)
  175. +   {
  176. +       try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  177. +           PreparedStatement stmt = con.prepareStatement("DELETE FROM fences WHERE object_id=?");)
  178. +       {
  179. +          
  180. +           stmt.setInt(1, objectId);
  181. +           if (stmt.executeUpdate() > 0)
  182. +           {
  183. +               return true;
  184. +           }
  185. +       }
  186. +       catch (SQLException sqle)
  187. +       {
  188. +           _log.log(Level.WARNING, "Error while deleting stored fence:", sqle);
  189. +       }
  190. +      
  191. +       return false;
  192. +   }
  193. +}
  194. Index: java/com/l2jserver/gameserver/model/L2Object.java
  195. ===================================================================
  196. --- java/com/l2jserver/gameserver/model/L2Object.java   (revision 5685)
  197. +++ java/com/l2jserver/gameserver/model/L2Object.java   (working copy)
  198. @@ -64,6 +64,7 @@
  199.         L2Object(null),
  200.         L2ItemInstance(L2Object),
  201.         L2Character(L2Object),
  202. +       L2FenceInstance(L2Object),
  203.         L2Npc(L2Character),
  204.         L2Playable(L2Character),
  205.         L2Summon(L2Playable),
  206. Index: java/com/l2jserver/gameserver/model/actor/instance/L2FenceInstance.java
  207. ===================================================================
  208. --- java/com/l2jserver/gameserver/model/actor/instance/L2FenceInstance.java (revision 0)
  209. +++ java/com/l2jserver/gameserver/model/actor/instance/L2FenceInstance.java (revision 0)
  210. @@ -0,0 +1,232 @@
  211. +/*
  212. + * This program is free software: you can redistribute it and/or modify it under
  213. + * the terms of the GNU General Public License as published by the Free Software
  214. + * Foundation, either version 3 of the License, or (at your option) any later
  215. + * version.
  216. + *
  217. + * This program is distributed in the hope that it will be useful, but WITHOUT
  218. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  219. + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  220. + * details.
  221. + *
  222. + * You should have received a copy of the GNU General Public License along with
  223. + * this program. If not, see <http://www.gnu.org/licenses/>.
  224. + */
  225. +
  226. +package com.l2jserver.gameserver.model.actor.instance;
  227. +
  228. +import java.awt.Rectangle;
  229. +
  230. +import com.l2jserver.gameserver.datatables.FencesTable;
  231. +import com.l2jserver.gameserver.idfactory.IdFactory;
  232. +import com.l2jserver.gameserver.model.L2Object;
  233. +import com.l2jserver.gameserver.model.Location;
  234. +import com.l2jserver.gameserver.model.actor.L2Character;
  235. +import com.l2jserver.gameserver.network.serverpackets.ExColosseumFenceInfo;
  236. +
  237. +/**
  238. + * @author Nik
  239. + */
  240. +public final class L2FenceInstance extends L2Object
  241. +{
  242. +   public static final int HIDDEN = 0;
  243. +   public static final int UNCLOSED = 1;
  244. +   public static final int CLOSED = 2;
  245. +  
  246. +   private int _state;
  247. +   private final int _width;
  248. +   private final int _length;
  249. +   private int _height;
  250. +   private Rectangle _shape;
  251. +   private boolean _storedToDB = false;
  252. +  
  253. +   public L2FenceInstance(int objectId, int state, int x, int y, int z, int width, int length, int height)
  254. +   {
  255. +       super(objectId);
  256. +       setInstanceType(InstanceType.L2FenceInstance);
  257. +       initPosition();
  258. +       setXYZ(x, y, z);
  259. +      
  260. +       if ((state < 0) || (state > 2))
  261. +       {
  262. +           state = 0;
  263. +       }
  264. +       _state = state;
  265. +       _width = width;
  266. +       _length = length;
  267. +       _height = height;
  268. +       _shape = new Rectangle(getX(), getY(), _width, _length);
  269. +   }
  270. +  
  271. +   public L2FenceInstance(int state, int x, int y, int z, int width, int length)
  272. +   {
  273. +       super(IdFactory.getInstance().getNextId());
  274. +       setInstanceType(InstanceType.L2FenceInstance);
  275. +       initPosition();
  276. +       setXYZ(x, y, z);
  277. +      
  278. +       if ((state < 0) || (state > 2))
  279. +       {
  280. +           state = 0;
  281. +       }
  282. +       _state = state;
  283. +       _width = width;
  284. +       _length = length;
  285. +       _height = 50;
  286. +       _shape = new Rectangle(getX(), getY(), _width, _length);
  287. +   }
  288. +  
  289. +   public L2FenceInstance(int x, int y, int z, int width, int length)
  290. +   {
  291. +       super(IdFactory.getInstance().getNextId());
  292. +       setInstanceType(InstanceType.L2FenceInstance);
  293. +       initPosition();
  294. +       setXYZ(x, y, z);
  295. +       _state = CLOSED; // Its a better default than HIDDEN.
  296. +       _width = width;
  297. +       _length = length;
  298. +       _height = 50;
  299. +       _shape = new Rectangle(getX(), getY(), _width, _length);
  300. +   }
  301. +  
  302. +   public boolean isLocInside(Location loc)
  303. +   {
  304. +       boolean isInsideZ = (loc.getZ() >= getZ()) && (loc.getZ() <= (getZ() + _height));
  305. +       return _shape.contains(loc.getX(), loc.getY()) && isInsideZ && (getInstanceId() == loc.getInstanceId());
  306. +   }
  307. +  
  308. +   public boolean isLocOutside(Location loc)
  309. +   {
  310. +       return !isLocInside(loc);
  311. +   }
  312. +  
  313. +   /**
  314. +    * @param x1 - obj1's X loc
  315. +    * @param y1 - obj1's Y loc
  316. +    * @param x2 - obj2's X loc
  317. +    * @param y2 - obj2's Y loc
  318. +    * @return can obj1 see obj2 through this fence (their LOS do not intersect the fence's bounds). <B>No Z</B> checks are done. <B>No Instance</B> checks are done.
  319. +    */
  320. +   public boolean canSee(int x1, int y1, int x2, int y2)
  321. +   {
  322. +       return !_shape.intersectsLine(x1, y1, x2, y2);
  323. +   }
  324. +  
  325. +   /**
  326. +    * @param loc1 - obj1's xyz & instanceId location
  327. +    * @param loc2 - obj2's xyz & instanceId location
  328. +    * @return can obj1 see obj2 through this fence (their LOS do not intersect the fence's bounds). <B>Z</B> checks are done. <B>Instance</B> checks are done.
  329. +    */
  330. +   public boolean canSee(Location loc1, Location loc2)
  331. +   {
  332. +       if ((getState() == CLOSED) && (loc1.getInstanceId() == loc2.getInstanceId()))
  333. +       {
  334. +           // Those Z checks are probably not 100% accurate, rework them if it must.
  335. +           int higherZ = loc1.getZ() >= loc2.getZ() ? loc1.getZ() : loc2.getZ();
  336. +           if ((higherZ >= getZ()) && (higherZ <= (getZ() + _height)))
  337. +           {
  338. +               return canSee(loc1.getX(), loc1.getY(), loc2.getX(), loc2.getY());
  339. +           }
  340. +       }
  341. +      
  342. +       return true;
  343. +   }
  344. +  
  345. +   /**
  346. +    * @param obj1
  347. +    * @param obj2
  348. +    * @return can obj1 see obj2 through this fence (their LOS do not intersect the fence's bounds). <B>Z</B> checks are done. <B>Instance</B> checks are done.
  349. +    */
  350. +   public boolean canSee(L2Object obj1, L2Object obj2)
  351. +   {
  352. +       if ((getState() == CLOSED) && (obj1.getInstanceId() == obj2.getInstanceId()))
  353. +       {
  354. +           // Those Z checks are probably not 100% accurate, rework them if it must.
  355. +           int higherZ = obj1.getZ() >= obj2.getZ() ? obj1.getZ() : obj2.getZ();
  356. +           if ((higherZ >= getZ()) && (higherZ <= (getZ() + _height)))
  357. +           {
  358. +               return canSee(obj1.getX(), obj1.getY(), obj2.getX(), obj2.getY());
  359. +           }
  360. +       }
  361. +      
  362. +       return true;
  363. +   }
  364. +  
  365. +   @Override
  366. +   public void sendInfo(L2PcInstance activeChar)
  367. +   {
  368. +       activeChar.sendPacket(new ExColosseumFenceInfo(this));
  369. +   }
  370. +  
  371. +   @Override
  372. +   public boolean isAutoAttackable(L2Character attacker)
  373. +   {
  374. +       return false;
  375. +   }
  376. +  
  377. +   /**
  378. +    * Also removes the fence from FencesTable, but its not removed from DB
  379. +    */
  380. +   @Override
  381. +   public void decayMe()
  382. +   {
  383. +       FencesTable.getInstance().removeFence(getObjectId());
  384. +   }
  385. +  
  386. +   @Override
  387. +   public void onSpawn()
  388. +   {
  389. +       // Maybe someone for some reason decided to respawn this fence on different XYZ. RESHAPE!!!
  390. +       _shape = new Rectangle(getX(), getY(), _width, _length);
  391. +       FencesTable.getInstance().addFence(this);
  392. +   }
  393. +  
  394. +   /**
  395. +    * @param height - how high the fence is :D :D Although the client height of the fence is ~50, you can set it to as much as you want. <BR>
  396. +    *            Setting it to lower might result in some strange LOS checks. Setting it too high might result in weird LOS checks in all the floors of TOI :P
  397. +    */
  398. +   public void setHeight(int height)
  399. +   {
  400. +       _height = height;
  401. +   }
  402. +  
  403. +   public void setState(int state)
  404. +   {
  405. +       if ((state < 0) || (state > 2))
  406. +       {
  407. +           state = 0;
  408. +       }
  409. +      
  410. +       _state = state;
  411. +   }
  412. +  
  413. +   public int getState()
  414. +   {
  415. +       return _state;
  416. +   }
  417. +  
  418. +   public int getWidth()
  419. +   {
  420. +       return _width;
  421. +   }
  422. +  
  423. +   public int getLength()
  424. +   {
  425. +       return _length;
  426. +   }
  427. +  
  428. +   public int getHeight()
  429. +   {
  430. +       return _height;
  431. +   }
  432. +  
  433. +   public void setStoredToDB(boolean storedToDB)
  434. +   {
  435. +       _storedToDB = storedToDB;
  436. +   }
  437. +  
  438. +   public boolean isStoredToDB()
  439. +   {
  440. +       return _storedToDB;
  441. +   }
  442. +}
  443. \ No newline at end of file
  444. Index: java/com/l2jserver/gameserver/idfactory/IdFactory.java
  445. ===================================================================
  446. --- java/com/l2jserver/gameserver/idfactory/IdFactory.java  (revision 5685)
  447. +++ java/com/l2jserver/gameserver/idfactory/IdFactory.java  (working copy)
  448. @@ -102,7 +102,8 @@
  449.         "SELECT ally_id     FROM clan_data             WHERE ally_id >= ?     AND ally_id < ?",
  450.         "SELECT leader_id   FROM clan_data             WHERE leader_id >= ?   AND leader_id < ?",
  451.         "SELECT item_obj_id FROM pets                  WHERE item_obj_id >= ? AND item_obj_id < ?",
  452. -       "SELECT object_id   FROM itemsonground        WHERE object_id >= ?   AND object_id < ?"
  453. +       "SELECT object_id   FROM itemsonground        WHERE object_id >= ?   AND object_id < ?",
  454. +       "SELECT object_id   FROM fences        WHERE object_id >= ?   AND object_id < ?"
  455.     };
  456.    
  457.     private static final String[] TIMESTAMPS_CLEAN =
  458. @@ -400,6 +401,20 @@
  459.                     temp.add(rs.getInt(1));
  460.                 }
  461.             }
  462. +          
  463. +           try (ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM fences"))
  464. +           {
  465. +               rs.next();
  466. +               temp.ensureCapacity(temp.size() + rs.getInt(1));
  467. +           }
  468. +          
  469. +           try (ResultSet rs = s.executeQuery("SELECT object_id FROM fences"))
  470. +           {
  471. +               while (rs.next())
  472. +               {
  473. +                   temp.add(rs.getInt(1));
  474. +               }
  475. +           }
  476.             temp.sort();
  477.             return temp.toArray();
  478.         }
  479. Index: java/com/l2jserver/gameserver/network/serverpackets/ExColosseumFenceInfo.java
  480. ===================================================================
  481. --- java/com/l2jserver/gameserver/network/serverpackets/ExColosseumFenceInfo.java   (revision 0)
  482. +++ java/com/l2jserver/gameserver/network/serverpackets/ExColosseumFenceInfo.java   (revision 0)
  483. @@ -0,0 +1,57 @@
  484. +/*
  485. + * This program is free software: you can redistribute it and/or modify it under
  486. + * the terms of the GNU General Public License as published by the Free Software
  487. + * Foundation, either version 3 of the License, or (at your option) any later
  488. + * version.
  489. + *
  490. + * This program is distributed in the hope that it will be useful, but WITHOUT
  491. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  492. + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  493. + * details.
  494. + *
  495. + * You should have received a copy of the GNU General Public License along with
  496. + * this program. If not, see <http://www.gnu.org/licenses/>.
  497. + */
  498. +package com.l2jserver.gameserver.network.serverpackets;
  499. +
  500. +import com.l2jserver.gameserver.model.actor.instance.L2FenceInstance;
  501. +
  502. +/**
  503. + * OP: 0xFE<br>
  504. + * OP2: 0x0003<br>
  505. + * Format: ddddddd<br>
  506. + * - d: object id<br>
  507. + * - d: state(0=hidden, 1=unconnected corners, 2=connected corners)<br>
  508. + * - d: x<br>
  509. + * - d: y<br>
  510. + * - d: z<br>
  511. + * - d: a side length<br>
  512. + * - d: b side length<br>
  513. + */
  514. +public class ExColosseumFenceInfo extends L2GameServerPacket
  515. +{
  516. +   private final L2FenceInstance _fence;
  517. +  
  518. +   public ExColosseumFenceInfo(L2FenceInstance fence)
  519. +   {
  520. +       _fence = fence;
  521. +   }
  522. +  
  523. +   /**
  524. +    * @see com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket#writeImpl()
  525. +    */
  526. +   @Override
  527. +   protected void writeImpl()
  528. +   {
  529. +       writeC(0xfe);
  530. +       writeH(0x0003);
  531. +      
  532. +       writeD(_fence.getObjectId());
  533. +       writeD(_fence.getState());
  534. +       writeD(_fence.getX());
  535. +       writeD(_fence.getY());
  536. +       writeD(_fence.getZ());
  537. +       writeD(_fence.getWidth());
  538. +       writeD(_fence.getLength());
  539. +   }
  540. +}
  541. \ No newline at end of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement