ValidatePosition.java 6.8 KB

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