WayPointNode.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.network.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(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(),
  89. player.getZ());
  90. }
  91. @Override
  92. public void onAction(L2PcInstance player)
  93. {
  94. if (player.getTarget() != this)
  95. {
  96. player.setTarget(this);
  97. MyTargetSelected my = new MyTargetSelected(getObjectId(), 0);
  98. player.sendPacket(my);
  99. }
  100. }
  101. public void setNormalInfo(String type, int id, String title)
  102. {
  103. _type = type;
  104. changeID(id, title);
  105. }
  106. public void setNormalInfo(String type, int id)
  107. {
  108. _type = type;
  109. changeID(id);
  110. }
  111. private void changeID(int id)
  112. {
  113. _id = id;
  114. toggleVisible();
  115. toggleVisible();
  116. }
  117. private void changeID(int id, String title)
  118. {
  119. setName(title);
  120. setTitle(title);
  121. changeID(id);
  122. }
  123. public void setLinked()
  124. {
  125. changeID(Config.LINKED_NODE_ID, LINKED);
  126. }
  127. public void setNormal()
  128. {
  129. changeID(Config.NEW_NODE_ID, NORMAL);
  130. }
  131. public void setSelected()
  132. {
  133. changeID(Config.SELECTED_NODE_ID, SELECTED);
  134. }
  135. @Override
  136. public boolean isMarker()
  137. {
  138. return true;
  139. }
  140. public final String getTitle()
  141. {
  142. return _title;
  143. }
  144. public final void setTitle(String title)
  145. {
  146. _title = title;
  147. }
  148. public int getId()
  149. {
  150. return _id;
  151. }
  152. public String getType()
  153. {
  154. return _type;
  155. }
  156. public void setType(String type)
  157. {
  158. _type = type;
  159. }
  160. /**
  161. * @param target
  162. * @param selectedNode
  163. */
  164. public static void drawLine(WayPointNode nodeA, WayPointNode nodeB)
  165. {
  166. int x1 = nodeA.getX(), y1 = nodeA.getY(), z1 = nodeA.getZ();
  167. int x2 = nodeB.getX(), y2 = nodeB.getY(), z2 = nodeB.getZ();
  168. int modX = x1 - x2 > 0 ? -1 : 1;
  169. int modY = y1 - y2 > 0 ? -1 : 1;
  170. int modZ = z1 - z2 > 0 ? -1 : 1;
  171. int diffX = Math.abs(x1 - x2);
  172. int diffY = Math.abs(y1 - y2);
  173. int diffZ = Math.abs(z1 - z2);
  174. int distance = (int) Math.sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ);
  175. int steps = distance / 40;
  176. List<WayPointNode> lineNodes = new FastList<WayPointNode>();
  177. for (int i = 0; i < steps; i++)
  178. {
  179. x1 = x1 + (modX * diffX / steps);
  180. y1 = y1 + (modY * diffY / steps);
  181. z1 = z1 + (modZ * diffZ / steps);
  182. lineNodes.add(WayPointNode.spawn(LINE_TYPE, _lineId, x1, y1, z1));
  183. }
  184. nodeA.addLineInfo(nodeB, lineNodes);
  185. nodeB.addLineInfo(nodeA, lineNodes);
  186. }
  187. public void addLineInfo(WayPointNode node, List<WayPointNode> line)
  188. {
  189. _linkLists.put(node, line);
  190. }
  191. /**
  192. * @param target
  193. * @param selectedNode
  194. */
  195. public static void eraseLine(WayPointNode target, WayPointNode selectedNode)
  196. {
  197. List<WayPointNode> lineNodes = target.getLineInfo(selectedNode);
  198. if (lineNodes == null) 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. }