WayPointNode.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.model.waypoint;
  16. import java.util.Collections;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.WeakHashMap;
  20. import javolution.util.FastList;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.idfactory.IdFactory;
  23. import com.l2jserver.gameserver.model.L2Object;
  24. import com.l2jserver.gameserver.model.actor.L2Character;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.network.serverpackets.MyTargetSelected;
  27. import com.l2jserver.gameserver.util.Point3D;
  28. /**
  29. * This class ...
  30. *
  31. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  32. */
  33. public class WayPointNode extends L2Object
  34. {
  35. private int _id;
  36. private String _title, _type;
  37. private static final String NORMAL = "Node", SELECTED = "Selected", LINKED = "Linked";
  38. private static int _lineId = 5560;
  39. private static final String LINE_TYPE = "item";
  40. private Map<WayPointNode, List<WayPointNode>> _linkLists;
  41. /**
  42. * @param objectId
  43. */
  44. public WayPointNode(int objectId)
  45. {
  46. super(objectId);
  47. _linkLists = Collections.synchronizedMap(new WeakHashMap<WayPointNode, List<WayPointNode>>());
  48. }
  49. @Override
  50. public boolean isAutoAttackable(L2Character attacker)
  51. {
  52. return false;
  53. }
  54. public static WayPointNode spawn(String type, int id, int x, int y, int z)
  55. {
  56. WayPointNode newNode = new WayPointNode(IdFactory.getInstance().getNextId());
  57. newNode.getPoly().setPolyInfo(type, id + "");
  58. newNode.spawnMe(x, y, z);
  59. return newNode;
  60. }
  61. public static WayPointNode spawn(boolean isItemId, int id, L2PcInstance player)
  62. {
  63. return spawn(isItemId ? "item" : "npc", id, player.getX(), player.getY(), player.getZ());
  64. }
  65. public static WayPointNode spawn(boolean isItemId, int id, Point3D point)
  66. {
  67. return spawn(isItemId ? "item" : "npc", id, point.getX(), point.getY(), point.getZ());
  68. }
  69. public static WayPointNode spawn(Point3D point)
  70. {
  71. return spawn(Config.NEW_NODE_TYPE, Config.NEW_NODE_ID, point.getX(), point.getY(), point.getZ());
  72. }
  73. public static WayPointNode spawn(L2PcInstance player)
  74. {
  75. return spawn(Config.NEW_NODE_TYPE, Config.NEW_NODE_ID, player.getX(), player.getY(), player.getZ());
  76. }
  77. @Override
  78. public void onAction(L2PcInstance player, boolean interact)
  79. {
  80. if (player.getTarget() != this)
  81. {
  82. player.setTarget(this);
  83. MyTargetSelected my = new MyTargetSelected(getObjectId(), 0);
  84. player.sendPacket(my);
  85. }
  86. }
  87. public void setNormalInfo(String type, int id, String title)
  88. {
  89. _type = type;
  90. changeID(id, title);
  91. }
  92. public void setNormalInfo(String type, int id)
  93. {
  94. _type = type;
  95. changeID(id);
  96. }
  97. private void changeID(int id)
  98. {
  99. _id = id;
  100. toggleVisible();
  101. toggleVisible();
  102. }
  103. private void changeID(int id, String title)
  104. {
  105. setName(title);
  106. setTitle(title);
  107. changeID(id);
  108. }
  109. public void setLinked()
  110. {
  111. changeID(Config.LINKED_NODE_ID, LINKED);
  112. }
  113. public void setNormal()
  114. {
  115. changeID(Config.NEW_NODE_ID, NORMAL);
  116. }
  117. public void setSelected()
  118. {
  119. changeID(Config.SELECTED_NODE_ID, SELECTED);
  120. }
  121. @Override
  122. public boolean isMarker()
  123. {
  124. return true;
  125. }
  126. public final String getTitle()
  127. {
  128. return _title;
  129. }
  130. public final void setTitle(String title)
  131. {
  132. _title = title;
  133. }
  134. public int getId()
  135. {
  136. return _id;
  137. }
  138. public String getType()
  139. {
  140. return _type;
  141. }
  142. public void setType(String type)
  143. {
  144. _type = type;
  145. }
  146. /**
  147. * @param nodeA
  148. * @param nodeB
  149. */
  150. public static void drawLine(WayPointNode nodeA, WayPointNode nodeB)
  151. {
  152. int x1 = nodeA.getX(), y1 = nodeA.getY(), z1 = nodeA.getZ();
  153. int x2 = nodeB.getX(), y2 = nodeB.getY(), z2 = nodeB.getZ();
  154. int modX = x1 - x2 > 0 ? -1 : 1;
  155. int modY = y1 - y2 > 0 ? -1 : 1;
  156. int modZ = z1 - z2 > 0 ? -1 : 1;
  157. int diffX = Math.abs(x1 - x2);
  158. int diffY = Math.abs(y1 - y2);
  159. int diffZ = Math.abs(z1 - z2);
  160. int distance = (int) Math.sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ);
  161. int steps = distance / 40;
  162. List<WayPointNode> lineNodes = new FastList<>();
  163. for (int i = 0; i < steps; i++)
  164. {
  165. x1 = x1 + (modX * diffX / steps);
  166. y1 = y1 + (modY * diffY / steps);
  167. z1 = z1 + (modZ * diffZ / steps);
  168. lineNodes.add(WayPointNode.spawn(LINE_TYPE, _lineId, x1, y1, z1));
  169. }
  170. nodeA.addLineInfo(nodeB, lineNodes);
  171. nodeB.addLineInfo(nodeA, lineNodes);
  172. }
  173. public void addLineInfo(WayPointNode node, List<WayPointNode> line)
  174. {
  175. _linkLists.put(node, line);
  176. }
  177. /**
  178. * @param target
  179. * @param selectedNode
  180. */
  181. public static void eraseLine(WayPointNode target, WayPointNode selectedNode)
  182. {
  183. List<WayPointNode> lineNodes = target.getLineInfo(selectedNode);
  184. if (lineNodes == null)
  185. return;
  186. for (WayPointNode node : lineNodes)
  187. {
  188. node.decayMe();
  189. }
  190. target.eraseLine(selectedNode);
  191. selectedNode.eraseLine(target);
  192. }
  193. /**
  194. * @param target
  195. */
  196. public void eraseLine(WayPointNode target)
  197. {
  198. _linkLists.remove(target);
  199. }
  200. /**
  201. * @param selectedNode
  202. * @return
  203. */
  204. private List<WayPointNode> getLineInfo(WayPointNode selectedNode)
  205. {
  206. return _linkLists.get(selectedNode);
  207. }
  208. public static void setLineId(int line_id)
  209. {
  210. _lineId = line_id;
  211. }
  212. public List<WayPointNode> getLineNodes()
  213. {
  214. List<WayPointNode> list = new FastList<>();
  215. for (List<WayPointNode> points : _linkLists.values())
  216. {
  217. list.addAll(points);
  218. }
  219. return list;
  220. }
  221. }