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