WayPointNode.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * $Header: WayPointNode.java, 20/07/2005 19:49:29 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $
  5. * $Date: 20/07/2005 19:49:29 $
  6. * $Revision: 1 $
  7. * $Log: WayPointNode.java,v $
  8. * Revision 1 20/07/2005 19:49:29 luisantonioa
  9. * Added copyright notice
  10. *
  11. *
  12. * This program is free software: you can redistribute it and/or modify it under
  13. * the terms of the GNU General Public License as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option) any later
  15. * version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. package com.l2jserver.gameserver.model.waypoint;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.WeakHashMap;
  30. import javolution.util.FastList;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.gameserver.idfactory.IdFactory;
  33. import com.l2jserver.gameserver.model.L2Object;
  34. import com.l2jserver.gameserver.model.actor.L2Character;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. import com.l2jserver.gameserver.network.serverpackets.MyTargetSelected;
  37. import com.l2jserver.gameserver.util.Point3D;
  38. /**
  39. * This class ...
  40. *
  41. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  42. */
  43. public class WayPointNode extends L2Object
  44. {
  45. private int _id;
  46. private String _title, _type;
  47. private static final String NORMAL = "Node", SELECTED = "Selected", LINKED = "Linked";
  48. private static int _lineId = 5560;
  49. private static final String LINE_TYPE = "item";
  50. private Map<WayPointNode, List<WayPointNode>> _linkLists;
  51. /**
  52. * @param objectId
  53. */
  54. public WayPointNode(int objectId)
  55. {
  56. super(objectId);
  57. _linkLists = Collections.synchronizedMap(new WeakHashMap<WayPointNode, List<WayPointNode>>());
  58. }
  59. /* (non-Javadoc)
  60. * @see com.l2jserver.gameserver.model.L2Object#isAutoAttackable(com.l2jserver.gameserver.model.L2Character)
  61. */
  62. @Override
  63. public boolean isAutoAttackable(L2Character attacker)
  64. {
  65. return false;
  66. }
  67. public static WayPointNode spawn(String type, int id, int x, int y, int z)
  68. {
  69. WayPointNode newNode = new WayPointNode(IdFactory.getInstance().getNextId());
  70. newNode.getPoly().setPolyInfo(type, id + "");
  71. newNode.spawnMe(x, y, z);
  72. return newNode;
  73. }
  74. public static WayPointNode spawn(boolean isItemId, int id, L2PcInstance player)
  75. {
  76. return spawn(isItemId ? "item" : "npc", id, player.getX(), player.getY(), player.getZ());
  77. }
  78. public static WayPointNode spawn(boolean isItemId, int id, Point3D point)
  79. {
  80. return spawn(isItemId ? "item" : "npc", id, point.getX(), point.getY(), point.getZ());
  81. }
  82. public static WayPointNode spawn(Point3D point)
  83. {
  84. return spawn(Config.NEW_NODE_TYPE, Config.NEW_NODE_ID, point.getX(), point.getY(), point.getZ());
  85. }
  86. public static WayPointNode spawn(L2PcInstance player)
  87. {
  88. return spawn(Config.NEW_NODE_TYPE, Config.NEW_NODE_ID, player.getX(), player.getY(), player.getZ());
  89. }
  90. @Override
  91. public void onAction(L2PcInstance player, boolean interact)
  92. {
  93. if (player.getTarget() != this)
  94. {
  95. player.setTarget(this);
  96. MyTargetSelected my = new MyTargetSelected(getObjectId(), 0);
  97. player.sendPacket(my);
  98. }
  99. }
  100. public void setNormalInfo(String type, int id, String title)
  101. {
  102. _type = type;
  103. changeID(id, title);
  104. }
  105. public void setNormalInfo(String type, int id)
  106. {
  107. _type = type;
  108. changeID(id);
  109. }
  110. private void changeID(int id)
  111. {
  112. _id = id;
  113. toggleVisible();
  114. toggleVisible();
  115. }
  116. private void changeID(int id, String title)
  117. {
  118. setName(title);
  119. setTitle(title);
  120. changeID(id);
  121. }
  122. public void setLinked()
  123. {
  124. changeID(Config.LINKED_NODE_ID, LINKED);
  125. }
  126. public void setNormal()
  127. {
  128. changeID(Config.NEW_NODE_ID, NORMAL);
  129. }
  130. public void setSelected()
  131. {
  132. changeID(Config.SELECTED_NODE_ID, SELECTED);
  133. }
  134. @Override
  135. public boolean isMarker()
  136. {
  137. return true;
  138. }
  139. public final String getTitle()
  140. {
  141. return _title;
  142. }
  143. public final void setTitle(String title)
  144. {
  145. _title = title;
  146. }
  147. public int getId()
  148. {
  149. return _id;
  150. }
  151. public String getType()
  152. {
  153. return _type;
  154. }
  155. public void setType(String type)
  156. {
  157. _type = type;
  158. }
  159. /**
  160. * @param target
  161. * @param selectedNode
  162. */
  163. public static void drawLine(WayPointNode nodeA, WayPointNode nodeB)
  164. {
  165. int x1 = nodeA.getX(), y1 = nodeA.getY(), z1 = nodeA.getZ();
  166. int x2 = nodeB.getX(), y2 = nodeB.getY(), z2 = nodeB.getZ();
  167. int modX = x1 - x2 > 0 ? -1 : 1;
  168. int modY = y1 - y2 > 0 ? -1 : 1;
  169. int modZ = z1 - z2 > 0 ? -1 : 1;
  170. int diffX = Math.abs(x1 - x2);
  171. int diffY = Math.abs(y1 - y2);
  172. int diffZ = Math.abs(z1 - z2);
  173. int distance = (int) Math.sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ);
  174. int steps = distance / 40;
  175. List<WayPointNode> lineNodes = new FastList<WayPointNode>();
  176. for (int i = 0; i < steps; i++)
  177. {
  178. x1 = x1 + (modX * diffX / steps);
  179. y1 = y1 + (modY * diffY / steps);
  180. z1 = z1 + (modZ * diffZ / steps);
  181. lineNodes.add(WayPointNode.spawn(LINE_TYPE, _lineId, x1, y1, z1));
  182. }
  183. nodeA.addLineInfo(nodeB, lineNodes);
  184. nodeB.addLineInfo(nodeA, lineNodes);
  185. }
  186. public void addLineInfo(WayPointNode node, List<WayPointNode> line)
  187. {
  188. _linkLists.put(node, line);
  189. }
  190. /**
  191. * @param target
  192. * @param selectedNode
  193. */
  194. public static void eraseLine(WayPointNode target, WayPointNode selectedNode)
  195. {
  196. List<WayPointNode> lineNodes = target.getLineInfo(selectedNode);
  197. if (lineNodes == null)
  198. return;
  199. for (WayPointNode node : lineNodes)
  200. {
  201. node.decayMe();
  202. }
  203. target.eraseLine(selectedNode);
  204. selectedNode.eraseLine(target);
  205. }
  206. /**
  207. * @param target
  208. */
  209. public void eraseLine(WayPointNode target)
  210. {
  211. _linkLists.remove(target);
  212. }
  213. /**
  214. * @param selectedNode
  215. * @return
  216. */
  217. private List<WayPointNode> getLineInfo(WayPointNode selectedNode)
  218. {
  219. return _linkLists.get(selectedNode);
  220. }
  221. public static void setLineId(int line_id)
  222. {
  223. _lineId = line_id;
  224. }
  225. public List<WayPointNode> getLineNodes()
  226. {
  227. List<WayPointNode> list = new FastList<WayPointNode>();
  228. for (List<WayPointNode> points : _linkLists.values())
  229. {
  230. list.addAll(points);
  231. }
  232. return list;
  233. }
  234. }