ValidatePosition.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.network.clientpackets;
  16. import java.util.logging.Logger;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.TaskPriority;
  19. import com.l2jserver.gameserver.geoeditorcon.GeoEditorListener;
  20. import com.l2jserver.gameserver.model.actor.L2Character;
  21. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  22. import com.l2jserver.gameserver.network.serverpackets.GetOnVehicle;
  23. import com.l2jserver.gameserver.network.serverpackets.ValidateLocation;
  24. /**
  25. * This class ...
  26. *
  27. * @version $Revision: 1.13.4.7 $ $Date: 2005/03/27 15:29:30 $
  28. */
  29. public class ValidatePosition extends L2GameClientPacket
  30. {
  31. private static Logger _log = Logger.getLogger(ValidatePosition.class.getName());
  32. private static final String _C__48_VALIDATEPOSITION = "[C] 48 ValidatePosition";
  33. /** urgent messages, execute immediately */
  34. public TaskPriority getPriority() { return TaskPriority.PR_HIGH; }
  35. private int _x;
  36. private int _y;
  37. private int _z;
  38. private int _heading;
  39. private int _data; // vehicle id
  40. @Override
  41. protected void readImpl()
  42. {
  43. _x = readD();
  44. _y = readD();
  45. _z = readD();
  46. _heading = readD();
  47. _data = readD();
  48. }
  49. @Override
  50. protected void runImpl()
  51. {
  52. final L2PcInstance activeChar = getClient().getActiveChar();
  53. if (activeChar == null
  54. || activeChar.isTeleporting()
  55. || activeChar.inObserverMode())
  56. return;
  57. final int realX = activeChar.getX();
  58. final int realY = activeChar.getY();
  59. int realZ = activeChar.getZ();
  60. if (Config.DEVELOPER)
  61. {
  62. _log.fine("client pos: "+ _x + " "+ _y + " "+ _z +" head "+ _heading);
  63. _log.fine("server pos: "+ realX + " "+realY+ " "+realZ +" head "+activeChar.getHeading());
  64. }
  65. if (_x == 0 && _y == 0)
  66. {
  67. if (realX != 0) // in this case this seems like a client error
  68. return;
  69. }
  70. int dx, dy, dz;
  71. double diffSq;
  72. if (activeChar.isInBoat())
  73. {
  74. if (Config.COORD_SYNCHRONIZE == 2)
  75. {
  76. dx = _x - activeChar.getInVehiclePosition().getX();
  77. dy = _y - activeChar.getInVehiclePosition().getY();
  78. dz = _z - activeChar.getInVehiclePosition().getZ();
  79. diffSq = (dx*dx + dy*dy);
  80. if (diffSq > 250000)
  81. sendPacket(new GetOnVehicle(activeChar.getObjectId(), _data, activeChar.getInVehiclePosition()));
  82. }
  83. return;
  84. }
  85. if (activeChar.isInAirShip())
  86. {
  87. /*if (Config.COORD_SYNCHRONIZE == 2)
  88. {
  89. dx = _x - activeChar.getInVehiclePosition().getX();
  90. dy = _y - activeChar.getInVehiclePosition().getY();
  91. dz = _z - activeChar.getInVehiclePosition().getZ();
  92. diffSq = (dx*dx + dy*dy);
  93. if (diffSq > 250000)
  94. sendPacket(new GetOnVehicle(activeChar.getObjectId(), _data, activeChar.getInBoatPosition()));
  95. }*/
  96. return;
  97. }
  98. if (activeChar.isFalling(_z))
  99. return; // disable validations during fall to avoid "jumping"
  100. dx = _x - realX;
  101. dy = _y - realY;
  102. dz = _z - realZ;
  103. diffSq = (dx*dx + dy*dy);
  104. /*L2Party party = activeChar.getParty();
  105. if(party != null && activeChar.getLastPartyPositionDistance(_x, _y, _z) > 150)
  106. {
  107. activeChar.setLastPartyPosition(_x, _y, _z);
  108. party.broadcastToPartyMembers(activeChar,new PartyMemberPosition(activeChar));
  109. }*/
  110. if (Config.ACCEPT_GEOEDITOR_CONN)
  111. if (GeoEditorListener.getInstance().getThread() != null
  112. && GeoEditorListener.getInstance().getThread().isWorking()
  113. && GeoEditorListener.getInstance().getThread().isSend(activeChar))
  114. GeoEditorListener.getInstance().getThread().sendGmPosition(_x,_y,(short)_z);
  115. if (activeChar.isFlying() || activeChar.isInsideZone(L2Character.ZONE_WATER))
  116. {
  117. activeChar.setXYZ(realX, realY, _z);
  118. if (diffSq > 90000) // validate packet, may also cause z bounce if close to land
  119. activeChar.sendPacket(new ValidateLocation(activeChar));
  120. }
  121. else if (diffSq < 360000) // if too large, messes observation
  122. {
  123. if (Config.COORD_SYNCHRONIZE == -1) // Only Z coordinate synched to server,
  124. // mainly used when no geodata but can be used also with geodata
  125. {
  126. activeChar.setXYZ(realX,realY,_z);
  127. return;
  128. }
  129. if (Config.COORD_SYNCHRONIZE == 1) // Trusting also client x,y coordinates (should not be used with geodata)
  130. {
  131. if (!activeChar.isMoving()
  132. || !activeChar.validateMovementHeading(_heading)) // Heading changed on client = possible obstacle
  133. {
  134. // character is not moving, take coordinates from client
  135. if (diffSq < 2500) // 50*50 - attack won't work fluently if even small differences are corrected
  136. activeChar.setXYZ(realX, realY, _z);
  137. else
  138. activeChar.setXYZ(_x, _y, _z);
  139. }
  140. else
  141. activeChar.setXYZ(realX, realY, _z);
  142. activeChar.setHeading(_heading);
  143. return;
  144. }
  145. // Sync 2 (or other),
  146. // intended for geodata. Sends a validation packet to client
  147. // when too far from server calculated true coordinate.
  148. // Due to geodata/zone errors, some Z axis checks are made. (maybe a temporary solution)
  149. // Important: this code part must work together with L2Character.updatePosition
  150. if (Config.GEODATA > 0 && (diffSq > 250000 || Math.abs(dz) > 200))
  151. {
  152. //if ((_z - activeChar.getClientZ()) < 200 && Math.abs(activeChar.getLastServerPosition().getZ()-realZ) > 70)
  153. if (Math.abs(dz) > 200
  154. && Math.abs(dz) < 1500
  155. && Math.abs(_z - activeChar.getClientZ()) < 800 )
  156. {
  157. activeChar.setXYZ(realX, realY, _z);
  158. realZ = _z;
  159. }
  160. else
  161. {
  162. if (Config.DEVELOPER)
  163. _log.info(activeChar.getName() + ": Synchronizing position Server --> Client");
  164. activeChar.sendPacket(new ValidateLocation(activeChar));
  165. }
  166. }
  167. }
  168. activeChar.setClientX(_x);
  169. activeChar.setClientY(_y);
  170. activeChar.setClientZ(_z);
  171. activeChar.setClientHeading(_heading); // No real need to validate heading.
  172. activeChar.setLastServerPosition(realX, realY, realZ);
  173. }
  174. /* (non-Javadoc)
  175. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  176. */
  177. @Override
  178. public String getType()
  179. {
  180. return _C__48_VALIDATEPOSITION;
  181. }
  182. }