ValidatePosition.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.network.clientpackets;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.model.L2World;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.model.zone.ZoneId;
  24. import com.l2jserver.gameserver.network.serverpackets.GetOnVehicle;
  25. import com.l2jserver.gameserver.network.serverpackets.ValidateLocation;
  26. /**
  27. * This class ...
  28. * @version $Revision: 1.13.4.7 $ $Date: 2005/03/27 15:29:30 $
  29. */
  30. public class ValidatePosition extends L2GameClientPacket
  31. {
  32. private static final String _C__59_VALIDATEPOSITION = "[C] 59 ValidatePosition";
  33. private int _x;
  34. private int _y;
  35. private int _z;
  36. private int _heading;
  37. private int _data; // vehicle id
  38. @Override
  39. protected void readImpl()
  40. {
  41. _x = readD();
  42. _y = readD();
  43. _z = readD();
  44. _heading = readD();
  45. _data = readD();
  46. }
  47. @Override
  48. protected void runImpl()
  49. {
  50. final L2PcInstance activeChar = getClient().getActiveChar();
  51. if ((activeChar == null) || activeChar.isTeleporting() || activeChar.inObserverMode())
  52. {
  53. return;
  54. }
  55. final int realX = activeChar.getX();
  56. final int realY = activeChar.getY();
  57. int realZ = activeChar.getZ();
  58. if (Config.DEVELOPER)
  59. {
  60. _log.fine("client pos: " + _x + " " + _y + " " + _z + " head " + _heading);
  61. _log.fine("server pos: " + realX + " " + realY + " " + realZ + " head " + activeChar.getHeading());
  62. }
  63. if ((_x == 0) && (_y == 0))
  64. {
  65. if (realX != 0)
  66. {
  67. return;
  68. }
  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. {
  82. sendPacket(new GetOnVehicle(activeChar.getObjectId(), _data, activeChar.getInVehiclePosition()));
  83. }
  84. }
  85. return;
  86. }
  87. if (activeChar.isInAirShip())
  88. {
  89. // Zoey76: TODO: Implement or cleanup.
  90. // if (Config.COORD_SYNCHRONIZE == 2)
  91. // {
  92. // dx = _x - activeChar.getInVehiclePosition().getX();
  93. // dy = _y - activeChar.getInVehiclePosition().getY();
  94. // dz = _z - activeChar.getInVehiclePosition().getZ();
  95. // diffSq = ((dx * dx) + (dy * dy));
  96. // if (diffSq > 250000)
  97. // {
  98. // sendPacket(new GetOnVehicle(activeChar.getObjectId(), _data, activeChar.getInBoatPosition()));
  99. // }
  100. // }
  101. return;
  102. }
  103. if (activeChar.isFalling(_z))
  104. {
  105. return; // disable validations during fall to avoid "jumping"
  106. }
  107. dx = _x - realX;
  108. dy = _y - realY;
  109. dz = _z - realZ;
  110. diffSq = ((dx * dx) + (dy * dy));
  111. // Zoey76: TODO: Implement or cleanup.
  112. // L2Party party = activeChar.getParty();
  113. // if ((party != null) && (activeChar.getLastPartyPositionDistance(_x, _y, _z) > 150))
  114. // {
  115. // activeChar.setLastPartyPosition(_x, _y, _z);
  116. // party.broadcastToPartyMembers(activeChar, new PartyMemberPosition(activeChar));
  117. // }
  118. // Don't allow flying transformations outside gracia area!
  119. if (activeChar.isFlyingMounted() && (_x > L2World.GRACIA_MAX_X))
  120. {
  121. activeChar.untransform();
  122. }
  123. if (activeChar.isFlying() || activeChar.isInsideZone(ZoneId.WATER))
  124. {
  125. activeChar.setXYZ(realX, realY, _z);
  126. if (diffSq > 90000)
  127. {
  128. activeChar.sendPacket(new ValidateLocation(activeChar));
  129. }
  130. }
  131. else if (diffSq < 360000) // if too large, messes observation
  132. {
  133. if (Config.COORD_SYNCHRONIZE == -1) // Only Z coordinate synched to server,
  134. // mainly used when no geodata but can be used also with geodata
  135. {
  136. activeChar.setXYZ(realX, realY, _z);
  137. return;
  138. }
  139. if (Config.COORD_SYNCHRONIZE == 1) // Trusting also client x,y coordinates (should not be used with geodata)
  140. {
  141. if (!activeChar.isMoving() || !activeChar.validateMovementHeading(_heading)) // Heading changed on client = possible obstacle
  142. {
  143. // character is not moving, take coordinates from client
  144. if (diffSq < 2500)
  145. {
  146. activeChar.setXYZ(realX, realY, _z);
  147. }
  148. else
  149. {
  150. activeChar.setXYZ(_x, _y, _z);
  151. }
  152. }
  153. else
  154. {
  155. activeChar.setXYZ(realX, realY, _z);
  156. }
  157. activeChar.setHeading(_heading);
  158. return;
  159. }
  160. // Sync 2 (or other),
  161. // intended for geodata. Sends a validation packet to client
  162. // when too far from server calculated true coordinate.
  163. // Due to geodata/zone errors, some Z axis checks are made. (maybe a temporary solution)
  164. // Important: this code part must work together with L2Character.updatePosition
  165. if ((diffSq > 250000) || (Math.abs(dz) > 200))
  166. {
  167. // if ((_z - activeChar.getClientZ()) < 200 && Math.abs(activeChar.getLastServerPosition().getZ()-realZ) > 70)
  168. if ((Math.abs(dz) > 200) && (Math.abs(dz) < 1500) && (Math.abs(_z - activeChar.getClientZ()) < 800))
  169. {
  170. activeChar.setXYZ(realX, realY, _z);
  171. realZ = _z;
  172. }
  173. else
  174. {
  175. if (Config.DEVELOPER)
  176. {
  177. _log.info(activeChar.getName() + ": Synchronizing position Server --> Client");
  178. }
  179. activeChar.sendPacket(new ValidateLocation(activeChar));
  180. }
  181. }
  182. }
  183. activeChar.setClientX(_x);
  184. activeChar.setClientY(_y);
  185. activeChar.setClientZ(_z);
  186. activeChar.setClientHeading(_heading); // No real need to validate heading.
  187. activeChar.setLastServerPosition(realX, realY, realZ);
  188. }
  189. @Override
  190. public String getType()
  191. {
  192. return _C__59_VALIDATEPOSITION;
  193. }
  194. }