WayPointNode.java 6.3 KB

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