ValidatePosition.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 net.sf.l2j.gameserver.clientpackets;
  16. import java.util.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.GeoData;
  19. import net.sf.l2j.gameserver.TaskPriority;
  20. import net.sf.l2j.gameserver.Universe;
  21. import net.sf.l2j.gameserver.geoeditorcon.GeoEditorListener;
  22. import net.sf.l2j.gameserver.model.L2Character;
  23. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  24. import net.sf.l2j.gameserver.serverpackets.PartyMemberPosition;
  25. import net.sf.l2j.gameserver.serverpackets.ValidateLocation;
  26. import net.sf.l2j.gameserver.serverpackets.ValidateLocationInVehicle;
  27. /**
  28. * This class ...
  29. *
  30. * @version $Revision: 1.13.4.7 $ $Date: 2005/03/27 15:29:30 $
  31. */
  32. public class ValidatePosition extends L2GameClientPacket
  33. {
  34. private static Logger _log = Logger.getLogger(ValidatePosition.class.getName());
  35. private static final String _C__48_VALIDATEPOSITION = "[C] 48 ValidatePosition";
  36. /** urgent messages, execute immediatly */
  37. public TaskPriority getPriority() { return TaskPriority.PR_HIGH; }
  38. private int _x;
  39. private int _y;
  40. private int _z;
  41. private int _heading;
  42. @SuppressWarnings("unused")
  43. private int _data;
  44. @Override
  45. protected void readImpl()
  46. {
  47. _x = readD();
  48. _y = readD();
  49. _z = readD();
  50. _heading = readD();
  51. _data = readD();
  52. }
  53. @Override
  54. protected void runImpl()
  55. {
  56. L2PcInstance activeChar = getClient().getActiveChar();
  57. if (activeChar == null || activeChar.isTeleporting()) return;
  58. if (Config.GEODATA > 0
  59. && (activeChar.isInOlympiadMode() || activeChar.isInsideZone(L2Character.ZONE_SIEGE))
  60. && !activeChar.isFlying()
  61. && GeoData.getInstance().hasGeo(_x, _y))
  62. {
  63. // check Z coordinate sent by client
  64. short geoHeight = GeoData.getInstance().getSpawnHeight(_x, _y, activeChar.getZ()-30, activeChar.getZ()+30, activeChar.getObjectId());
  65. if (Math.abs(geoHeight - _z) > 15)
  66. {
  67. // causes mild flashing in the middle of a drop from a castle wall for example
  68. _z = geoHeight;
  69. // System.out.println("Spawnheight validation diff="+Math.abs(geoHeight - _z));
  70. }
  71. }
  72. if (Config.COORD_SYNCHRONIZE > 0)
  73. {
  74. activeChar.setClientX(_x);
  75. activeChar.setClientY(_y);
  76. activeChar.setClientZ(_z);
  77. activeChar.setClientHeading(_heading);
  78. int realX = activeChar.getX();
  79. int realY = activeChar.getY();
  80. // int realZ = activeChar.getZ();
  81. double dx = _x - realX;
  82. double dy = _y - realY;
  83. double diffSq = (dx*dx + dy*dy);
  84. /*
  85. if (Config.DEVELOPER && false)
  86. {
  87. int dxs = (_x - activeChar._lastClientPosition.x);
  88. int dys = (_y - activeChar._lastClientPosition.y);
  89. int dist = (int)Math.sqrt(dxs*dxs + dys*dys);
  90. int heading = dist > 0 ? (int)(Math.atan2(-dys/dist, -dxs/dist) * 10430.378350470452724949566316381) + 32768 : 0;
  91. _log.info("Client X:" + _x + ", Y:" + _y + ", Z:" + _z + ", H:" + _heading + ", Dist:" + activeChar.getLastClientDistance(_x, _y, _z));
  92. _log.info("Server X:" + realX + ", Y:" + realY + ", Z:" + realZ + ", H:" + activeChar.getHeading() + ", Dist:" + activeChar.getLastServerDistance(realX, realY, realZ));
  93. }
  94. */
  95. if (diffSq > 0 && diffSq < 250000) // if too large, messes observation
  96. {
  97. if ((Config.COORD_SYNCHRONIZE & 1) == 1
  98. && (!activeChar.isMoving() // character is not moving, take coordinates from client
  99. || !activeChar.validateMovementHeading(_heading))) // Heading changed on client = possible obstacle
  100. {
  101. if (Config.DEVELOPER)
  102. _log.info(activeChar.getName() + ": Synchronizing position Client --> Server" + (activeChar.isMoving()?" (collision)":" (stay sync)"));
  103. if (diffSq < 2500) // 50*50 - attack won't work fluently if even small differences are corrected
  104. activeChar.setXYZ(realX, realY, _z);
  105. else
  106. activeChar.setXYZ(_x, _y, _z);
  107. activeChar.setHeading(_heading);
  108. }
  109. else if ((Config.COORD_SYNCHRONIZE & 2) == 2
  110. && diffSq > 10000) // more than can be considered to be result of latency
  111. {
  112. if (Config.DEVELOPER)
  113. _log.info(activeChar.getName() + ": Synchronizing position Server --> Client");
  114. if (activeChar.isInBoat())
  115. {
  116. sendPacket(new ValidateLocationInVehicle(activeChar));
  117. }
  118. else
  119. {
  120. activeChar.sendPacket(new ValidateLocation(activeChar));
  121. }
  122. }
  123. }
  124. activeChar.setLastClientPosition(_x, _y, _z);
  125. activeChar.setLastServerPosition(activeChar.getX(), activeChar.getY(), activeChar.getZ());
  126. }
  127. else if (Config.COORD_SYNCHRONIZE == -1)
  128. {
  129. activeChar.setClientX(_x);
  130. activeChar.setClientY(_y);
  131. activeChar.setClientZ(_z);
  132. activeChar.setClientHeading(_heading); // No real need to validate heading.
  133. int realX = activeChar.getX();
  134. int realY = activeChar.getY();
  135. int realZ = activeChar.getZ();
  136. double dx = _x - realX;
  137. double dy = _y - realY;
  138. double diffSq = (dx*dx + dy*dy);
  139. if (diffSq < 250000)
  140. activeChar.setXYZ(realX,realY,_z);
  141. //TODO: do we need to validate?
  142. /*double dx = (_x - realX);
  143. double dy = (_y - realY);
  144. double dist = Math.sqrt(dx*dx + dy*dy);
  145. if ((dist < 500)&&(dist > 2)) //check it wasnt teleportation, and char isn't there yet
  146. activeChar.sendPacket(new CharMoveToLocation(activeChar));*/
  147. if (Config.DEBUG) {
  148. _log.fine("client pos: "+ _x + " "+ _y + " "+ _z +" head "+ _heading);
  149. _log.fine("server pos: "+ realX + " "+realY+ " "+realZ +" head "+activeChar.getHeading());
  150. }
  151. if (Config.ACTIVATE_POSITION_RECORDER && !activeChar.isFlying() && Universe.getInstance().shouldLog(activeChar.getObjectId()))
  152. Universe.getInstance().registerHeight(realX, realY, _z);
  153. if (Config.DEVELOPER)
  154. {
  155. if (diffSq > 1000000) {
  156. if (Config.DEBUG) _log.fine("client/server dist diff "+ (int)Math.sqrt(diffSq));
  157. if (activeChar.isInBoat())
  158. {
  159. sendPacket(new ValidateLocationInVehicle(activeChar));
  160. }
  161. else
  162. {
  163. activeChar.sendPacket(new ValidateLocation(activeChar));
  164. }
  165. }
  166. }
  167. }
  168. if(activeChar.getParty() != null)
  169. activeChar.getParty().broadcastToPartyMembers(activeChar,new PartyMemberPosition(activeChar));
  170. if (Config.ACCEPT_GEOEDITOR_CONN)
  171. if (GeoEditorListener.getInstance().getThread() != null && GeoEditorListener.getInstance().getThread().isWorking() && GeoEditorListener.getInstance().getThread().isSend(activeChar))
  172. GeoEditorListener.getInstance().getThread().sendGmPosition(_x,_y,(short)_z);
  173. }
  174. /* (non-Javadoc)
  175. * @see net.sf.l2j.gameserver.clientpackets.ClientBasePacket#getType()
  176. */
  177. @Override
  178. public String getType()
  179. {
  180. return _C__48_VALIDATEPOSITION;
  181. }
  182. @Deprecated
  183. public boolean equal(ValidatePosition pos)
  184. {
  185. return _x == pos._x && _y == pos._y && _z == pos._z && _heading == pos._heading;
  186. }
  187. }