DocumentBase.java 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.engines;
  20. import java.io.File;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.StringTokenizer;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import javax.xml.parsers.DocumentBuilderFactory;
  28. import javolution.util.FastMap;
  29. import org.w3c.dom.Document;
  30. import org.w3c.dom.NamedNodeMap;
  31. import org.w3c.dom.Node;
  32. import com.l2jserver.gameserver.datatables.ItemTable;
  33. import com.l2jserver.gameserver.enums.InstanceType;
  34. import com.l2jserver.gameserver.enums.PcRace;
  35. import com.l2jserver.gameserver.model.StatsSet;
  36. import com.l2jserver.gameserver.model.base.PlayerState;
  37. import com.l2jserver.gameserver.model.conditions.Condition;
  38. import com.l2jserver.gameserver.model.conditions.ConditionChangeWeapon;
  39. import com.l2jserver.gameserver.model.conditions.ConditionGameChance;
  40. import com.l2jserver.gameserver.model.conditions.ConditionGameTime;
  41. import com.l2jserver.gameserver.model.conditions.ConditionGameTime.CheckGameTime;
  42. import com.l2jserver.gameserver.model.conditions.ConditionLogicAnd;
  43. import com.l2jserver.gameserver.model.conditions.ConditionLogicNot;
  44. import com.l2jserver.gameserver.model.conditions.ConditionLogicOr;
  45. import com.l2jserver.gameserver.model.conditions.ConditionMinDistance;
  46. import com.l2jserver.gameserver.model.conditions.ConditionPlayerActiveEffectId;
  47. import com.l2jserver.gameserver.model.conditions.ConditionPlayerActiveSkillId;
  48. import com.l2jserver.gameserver.model.conditions.ConditionPlayerAgathionId;
  49. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCallPc;
  50. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanEscape;
  51. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanPossessHolything;
  52. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanRefuelAirship;
  53. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanSummon;
  54. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanSweep;
  55. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanTransform;
  56. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanUntransform;
  57. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCharges;
  58. import com.l2jserver.gameserver.model.conditions.ConditionPlayerClassIdRestriction;
  59. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCloakStatus;
  60. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCp;
  61. import com.l2jserver.gameserver.model.conditions.ConditionPlayerFlyMounted;
  62. import com.l2jserver.gameserver.model.conditions.ConditionPlayerGrade;
  63. import com.l2jserver.gameserver.model.conditions.ConditionPlayerHasCastle;
  64. import com.l2jserver.gameserver.model.conditions.ConditionPlayerHasClanHall;
  65. import com.l2jserver.gameserver.model.conditions.ConditionPlayerHasFort;
  66. import com.l2jserver.gameserver.model.conditions.ConditionPlayerHasPet;
  67. import com.l2jserver.gameserver.model.conditions.ConditionPlayerHp;
  68. import com.l2jserver.gameserver.model.conditions.ConditionPlayerInsideZoneId;
  69. import com.l2jserver.gameserver.model.conditions.ConditionPlayerInstanceId;
  70. import com.l2jserver.gameserver.model.conditions.ConditionPlayerInvSize;
  71. import com.l2jserver.gameserver.model.conditions.ConditionPlayerIsClanLeader;
  72. import com.l2jserver.gameserver.model.conditions.ConditionPlayerIsHero;
  73. import com.l2jserver.gameserver.model.conditions.ConditionPlayerLandingZone;
  74. import com.l2jserver.gameserver.model.conditions.ConditionPlayerLevel;
  75. import com.l2jserver.gameserver.model.conditions.ConditionPlayerLevelRange;
  76. import com.l2jserver.gameserver.model.conditions.ConditionPlayerMp;
  77. import com.l2jserver.gameserver.model.conditions.ConditionPlayerPkCount;
  78. import com.l2jserver.gameserver.model.conditions.ConditionPlayerPledgeClass;
  79. import com.l2jserver.gameserver.model.conditions.ConditionPlayerRace;
  80. import com.l2jserver.gameserver.model.conditions.ConditionPlayerRangeFromNpc;
  81. import com.l2jserver.gameserver.model.conditions.ConditionPlayerServitorNpcId;
  82. import com.l2jserver.gameserver.model.conditions.ConditionPlayerSex;
  83. import com.l2jserver.gameserver.model.conditions.ConditionPlayerSiegeSide;
  84. import com.l2jserver.gameserver.model.conditions.ConditionPlayerSouls;
  85. import com.l2jserver.gameserver.model.conditions.ConditionPlayerState;
  86. import com.l2jserver.gameserver.model.conditions.ConditionPlayerSubclass;
  87. import com.l2jserver.gameserver.model.conditions.ConditionPlayerTransformationId;
  88. import com.l2jserver.gameserver.model.conditions.ConditionPlayerTvTEvent;
  89. import com.l2jserver.gameserver.model.conditions.ConditionPlayerVehicleMounted;
  90. import com.l2jserver.gameserver.model.conditions.ConditionPlayerWeight;
  91. import com.l2jserver.gameserver.model.conditions.ConditionSiegeZone;
  92. import com.l2jserver.gameserver.model.conditions.ConditionSlotItemId;
  93. import com.l2jserver.gameserver.model.conditions.ConditionTargetAbnormal;
  94. import com.l2jserver.gameserver.model.conditions.ConditionTargetActiveEffectId;
  95. import com.l2jserver.gameserver.model.conditions.ConditionTargetActiveSkillId;
  96. import com.l2jserver.gameserver.model.conditions.ConditionTargetAggro;
  97. import com.l2jserver.gameserver.model.conditions.ConditionTargetClassIdRestriction;
  98. import com.l2jserver.gameserver.model.conditions.ConditionTargetInvSize;
  99. import com.l2jserver.gameserver.model.conditions.ConditionTargetLevel;
  100. import com.l2jserver.gameserver.model.conditions.ConditionTargetLevelRange;
  101. import com.l2jserver.gameserver.model.conditions.ConditionTargetMyPartyExceptMe;
  102. import com.l2jserver.gameserver.model.conditions.ConditionTargetNpcId;
  103. import com.l2jserver.gameserver.model.conditions.ConditionTargetNpcType;
  104. import com.l2jserver.gameserver.model.conditions.ConditionTargetPlayable;
  105. import com.l2jserver.gameserver.model.conditions.ConditionTargetRace;
  106. import com.l2jserver.gameserver.model.conditions.ConditionTargetRaceId;
  107. import com.l2jserver.gameserver.model.conditions.ConditionTargetUsesWeaponKind;
  108. import com.l2jserver.gameserver.model.conditions.ConditionTargetWeight;
  109. import com.l2jserver.gameserver.model.conditions.ConditionUsingItemType;
  110. import com.l2jserver.gameserver.model.conditions.ConditionUsingSkill;
  111. import com.l2jserver.gameserver.model.conditions.ConditionWithSkill;
  112. import com.l2jserver.gameserver.model.effects.AbstractEffect;
  113. import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
  114. import com.l2jserver.gameserver.model.items.L2Item;
  115. import com.l2jserver.gameserver.model.items.type.L2ArmorType;
  116. import com.l2jserver.gameserver.model.items.type.L2WeaponType;
  117. import com.l2jserver.gameserver.model.skills.L2Skill;
  118. import com.l2jserver.gameserver.model.skills.funcs.FuncTemplate;
  119. import com.l2jserver.gameserver.model.skills.funcs.Lambda;
  120. import com.l2jserver.gameserver.model.skills.funcs.LambdaCalc;
  121. import com.l2jserver.gameserver.model.skills.funcs.LambdaConst;
  122. import com.l2jserver.gameserver.model.skills.funcs.LambdaStats;
  123. import com.l2jserver.gameserver.model.stats.Env;
  124. import com.l2jserver.gameserver.model.stats.Stats;
  125. /**
  126. * @author mkizub
  127. */
  128. public abstract class DocumentBase
  129. {
  130. protected final Logger _log = Logger.getLogger(getClass().getName());
  131. private final File _file;
  132. protected Map<String, String[]> _tables;
  133. protected DocumentBase(File pFile)
  134. {
  135. _file = pFile;
  136. _tables = new FastMap<>();
  137. }
  138. public Document parse()
  139. {
  140. Document doc = null;
  141. try
  142. {
  143. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  144. factory.setValidating(false);
  145. factory.setIgnoringComments(true);
  146. doc = factory.newDocumentBuilder().parse(_file);
  147. parseDocument(doc);
  148. }
  149. catch (Exception e)
  150. {
  151. _log.log(Level.SEVERE, "Error loading file " + _file, e);
  152. }
  153. return doc;
  154. }
  155. protected abstract void parseDocument(Document doc);
  156. protected abstract StatsSet getStatsSet();
  157. protected abstract String getTableValue(String name);
  158. protected abstract String getTableValue(String name, int idx);
  159. protected void resetTable()
  160. {
  161. _tables = new FastMap<>();
  162. }
  163. protected void setTable(String name, String[] table)
  164. {
  165. _tables.put(name, table);
  166. }
  167. protected void parseTemplate(Node n, Object template)
  168. {
  169. parseTemplate(n, template, false);
  170. }
  171. protected void parseTemplate(Node n, Object template, boolean isChanneling)
  172. {
  173. Condition condition = null;
  174. n = n.getFirstChild();
  175. if (n == null)
  176. {
  177. return;
  178. }
  179. if ("cond".equalsIgnoreCase(n.getNodeName()))
  180. {
  181. condition = parseCondition(n.getFirstChild(), template);
  182. Node msg = n.getAttributes().getNamedItem("msg");
  183. Node msgId = n.getAttributes().getNamedItem("msgId");
  184. if ((condition != null) && (msg != null))
  185. {
  186. condition.setMessage(msg.getNodeValue());
  187. }
  188. else if ((condition != null) && (msgId != null))
  189. {
  190. condition.setMessageId(Integer.decode(getValue(msgId.getNodeValue(), null)));
  191. Node addName = n.getAttributes().getNamedItem("addName");
  192. if ((addName != null) && (Integer.decode(getValue(msgId.getNodeValue(), null)) > 0))
  193. {
  194. condition.addName();
  195. }
  196. }
  197. n = n.getNextSibling();
  198. }
  199. for (; n != null; n = n.getNextSibling())
  200. {
  201. if ("add".equalsIgnoreCase(n.getNodeName()))
  202. {
  203. attachFunc(n, template, "Add", condition);
  204. }
  205. else if ("sub".equalsIgnoreCase(n.getNodeName()))
  206. {
  207. attachFunc(n, template, "Sub", condition);
  208. }
  209. else if ("mul".equalsIgnoreCase(n.getNodeName()))
  210. {
  211. attachFunc(n, template, "Mul", condition);
  212. }
  213. else if ("basemul".equalsIgnoreCase(n.getNodeName()))
  214. {
  215. attachFunc(n, template, "BaseMul", condition);
  216. }
  217. else if ("div".equalsIgnoreCase(n.getNodeName()))
  218. {
  219. attachFunc(n, template, "Div", condition);
  220. }
  221. else if ("set".equalsIgnoreCase(n.getNodeName()))
  222. {
  223. attachFunc(n, template, "Set", condition);
  224. }
  225. else if ("share".equalsIgnoreCase(n.getNodeName()))
  226. {
  227. attachFunc(n, template, "Share", condition);
  228. }
  229. else if ("enchant".equalsIgnoreCase(n.getNodeName()))
  230. {
  231. attachFunc(n, template, "Enchant", condition);
  232. }
  233. else if ("enchanthp".equalsIgnoreCase(n.getNodeName()))
  234. {
  235. attachFunc(n, template, "EnchantHp", condition);
  236. }
  237. else if ("effect".equalsIgnoreCase(n.getNodeName()))
  238. {
  239. if (template instanceof AbstractEffect)
  240. {
  241. throw new RuntimeException("Nested effects");
  242. }
  243. attachEffect(n, template, condition, isChanneling);
  244. }
  245. }
  246. }
  247. protected void attachFunc(Node n, Object template, String name, Condition attachCond)
  248. {
  249. Stats stat = Stats.valueOfXml(n.getAttributes().getNamedItem("stat").getNodeValue());
  250. String order = n.getAttributes().getNamedItem("order").getNodeValue();
  251. Lambda lambda = getLambda(n, template);
  252. int ord = Integer.decode(getValue(order, template));
  253. Condition applayCond = parseCondition(n.getFirstChild(), template);
  254. FuncTemplate ft = new FuncTemplate(attachCond, applayCond, name, stat, ord, lambda);
  255. if (template instanceof L2Item)
  256. {
  257. ((L2Item) template).attach(ft);
  258. }
  259. else if (template instanceof L2Skill)
  260. {
  261. ((L2Skill) template).attach(ft);
  262. }
  263. else if (template instanceof AbstractEffect)
  264. {
  265. ((AbstractEffect) template).attach(ft);
  266. }
  267. }
  268. protected void attachLambdaFunc(Node n, Object template, LambdaCalc calc)
  269. {
  270. String name = n.getNodeName();
  271. final StringBuilder sb = new StringBuilder(name);
  272. sb.setCharAt(0, Character.toUpperCase(name.charAt(0)));
  273. name = sb.toString();
  274. Lambda lambda = getLambda(n, template);
  275. FuncTemplate ft = new FuncTemplate(null, null, name, null, calc.funcs.length, lambda);
  276. calc.addFunc(ft.getFunc(new Env(), calc));
  277. }
  278. protected void attachEffect(Node n, Object template, Condition attachCond)
  279. {
  280. attachEffect(n, template, attachCond, false);
  281. }
  282. protected void attachEffect(Node n, Object template, Condition attachCond, boolean isChanneling)
  283. {
  284. final NamedNodeMap attrs = n.getAttributes();
  285. final StatsSet set = new StatsSet();
  286. for (int i = 0; i < attrs.getLength(); i++)
  287. {
  288. Node att = attrs.item(i);
  289. set.set(att.getNodeName(), getValue(att.getNodeValue(), template));
  290. }
  291. final StatsSet parameters = parseParameters(n.getFirstChild(), template);
  292. final Condition applayCond = parseCondition(n.getFirstChild(), template);
  293. if (template instanceof IIdentifiable)
  294. {
  295. set.set("id", ((IIdentifiable) template).getId());
  296. }
  297. final AbstractEffect effect = AbstractEffect.createEffect(attachCond, applayCond, set, parameters);
  298. parseTemplate(n, effect);
  299. if (template instanceof L2Item)
  300. {
  301. ((L2Item) template).attach(effect);
  302. }
  303. else if (template instanceof L2Skill)
  304. {
  305. final L2Skill skill = (L2Skill) template;
  306. if (isChanneling)
  307. {
  308. skill.attachChanneling(effect);
  309. }
  310. else if (set.getInt("self", 0) == 1)
  311. {
  312. skill.attachSelf(effect);
  313. }
  314. else if (skill.isPassive())
  315. {
  316. skill.attachPassive(effect);
  317. }
  318. else
  319. {
  320. skill.attach(effect);
  321. }
  322. }
  323. }
  324. /**
  325. * Parse effect's parameters.
  326. * @param n the node to start the parsing
  327. * @param template the effect template
  328. * @return the list of parameters if any, {@code null} otherwise
  329. */
  330. private StatsSet parseParameters(Node n, Object template)
  331. {
  332. StatsSet parameters = null;
  333. while ((n != null))
  334. {
  335. // Parse all parameters.
  336. if ((n.getNodeType() == Node.ELEMENT_NODE) && "param".equals(n.getNodeName()))
  337. {
  338. if (parameters == null)
  339. {
  340. parameters = new StatsSet();
  341. }
  342. NamedNodeMap params = n.getAttributes();
  343. for (int i = 0; i < params.getLength(); i++)
  344. {
  345. Node att = params.item(i);
  346. parameters.set(att.getNodeName(), getValue(att.getNodeValue(), template));
  347. }
  348. }
  349. n = n.getNextSibling();
  350. }
  351. return parameters;
  352. }
  353. protected Condition parseCondition(Node n, Object template)
  354. {
  355. while ((n != null) && (n.getNodeType() != Node.ELEMENT_NODE))
  356. {
  357. n = n.getNextSibling();
  358. }
  359. Condition condition = null;
  360. if (n != null)
  361. {
  362. switch (n.getNodeName())
  363. {
  364. case "and":
  365. {
  366. condition = parseLogicAnd(n, template);
  367. break;
  368. }
  369. case "or":
  370. {
  371. condition = parseLogicOr(n, template);
  372. break;
  373. }
  374. case "not":
  375. {
  376. condition = parseLogicNot(n, template);
  377. break;
  378. }
  379. case "player":
  380. {
  381. condition = parsePlayerCondition(n, template);
  382. break;
  383. }
  384. case "target":
  385. {
  386. condition = parseTargetCondition(n, template);
  387. break;
  388. }
  389. case "using":
  390. {
  391. condition = parseUsingCondition(n);
  392. break;
  393. }
  394. case "game":
  395. {
  396. condition = parseGameCondition(n);
  397. break;
  398. }
  399. }
  400. }
  401. return condition;
  402. }
  403. protected Condition parseLogicAnd(Node n, Object template)
  404. {
  405. ConditionLogicAnd cond = new ConditionLogicAnd();
  406. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  407. {
  408. if (n.getNodeType() == Node.ELEMENT_NODE)
  409. {
  410. cond.add(parseCondition(n, template));
  411. }
  412. }
  413. if ((cond.conditions == null) || (cond.conditions.length == 0))
  414. {
  415. _log.severe("Empty <and> condition in " + _file);
  416. }
  417. return cond;
  418. }
  419. protected Condition parseLogicOr(Node n, Object template)
  420. {
  421. ConditionLogicOr cond = new ConditionLogicOr();
  422. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  423. {
  424. if (n.getNodeType() == Node.ELEMENT_NODE)
  425. {
  426. cond.add(parseCondition(n, template));
  427. }
  428. }
  429. if ((cond.conditions == null) || (cond.conditions.length == 0))
  430. {
  431. _log.severe("Empty <or> condition in " + _file);
  432. }
  433. return cond;
  434. }
  435. protected Condition parseLogicNot(Node n, Object template)
  436. {
  437. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  438. {
  439. if (n.getNodeType() == Node.ELEMENT_NODE)
  440. {
  441. return new ConditionLogicNot(parseCondition(n, template));
  442. }
  443. }
  444. _log.severe("Empty <not> condition in " + _file);
  445. return null;
  446. }
  447. protected Condition parsePlayerCondition(Node n, Object template)
  448. {
  449. Condition cond = null;
  450. NamedNodeMap attrs = n.getAttributes();
  451. for (int i = 0; i < attrs.getLength(); i++)
  452. {
  453. Node a = attrs.item(i);
  454. if ("races".equalsIgnoreCase(a.getNodeName()))
  455. {
  456. final String[] racesVal = a.getNodeValue().split(",");
  457. final PcRace[] races = new PcRace[racesVal.length];
  458. for (int r = 0; r < racesVal.length; r++)
  459. {
  460. if (racesVal[r] != null)
  461. {
  462. races[r] = PcRace.valueOf(racesVal[r]);
  463. }
  464. }
  465. cond = joinAnd(cond, new ConditionPlayerRace(races));
  466. }
  467. else if ("level".equalsIgnoreCase(a.getNodeName()))
  468. {
  469. int lvl = Integer.decode(getValue(a.getNodeValue(), template));
  470. cond = joinAnd(cond, new ConditionPlayerLevel(lvl));
  471. }
  472. else if ("levelRange".equalsIgnoreCase(a.getNodeName()))
  473. {
  474. String[] range = getValue(a.getNodeValue(), template).split(";");
  475. if (range.length == 2)
  476. {
  477. int[] lvlRange = new int[2];
  478. lvlRange[0] = Integer.decode(getValue(a.getNodeValue(), template).split(";")[0]);
  479. lvlRange[1] = Integer.decode(getValue(a.getNodeValue(), template).split(";")[1]);
  480. cond = joinAnd(cond, new ConditionPlayerLevelRange(lvlRange));
  481. }
  482. }
  483. else if ("resting".equalsIgnoreCase(a.getNodeName()))
  484. {
  485. boolean val = Boolean.parseBoolean(a.getNodeValue());
  486. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.RESTING, val));
  487. }
  488. else if ("flying".equalsIgnoreCase(a.getNodeName()))
  489. {
  490. boolean val = Boolean.parseBoolean(a.getNodeValue());
  491. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.FLYING, val));
  492. }
  493. else if ("moving".equalsIgnoreCase(a.getNodeName()))
  494. {
  495. boolean val = Boolean.parseBoolean(a.getNodeValue());
  496. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.MOVING, val));
  497. }
  498. else if ("running".equalsIgnoreCase(a.getNodeName()))
  499. {
  500. boolean val = Boolean.parseBoolean(a.getNodeValue());
  501. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.RUNNING, val));
  502. }
  503. else if ("standing".equalsIgnoreCase(a.getNodeName()))
  504. {
  505. boolean val = Boolean.parseBoolean(a.getNodeValue());
  506. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.STANDING, val));
  507. }
  508. else if ("behind".equalsIgnoreCase(a.getNodeName()))
  509. {
  510. boolean val = Boolean.parseBoolean(a.getNodeValue());
  511. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.BEHIND, val));
  512. }
  513. else if ("front".equalsIgnoreCase(a.getNodeName()))
  514. {
  515. boolean val = Boolean.parseBoolean(a.getNodeValue());
  516. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.FRONT, val));
  517. }
  518. else if ("chaotic".equalsIgnoreCase(a.getNodeName()))
  519. {
  520. boolean val = Boolean.parseBoolean(a.getNodeValue());
  521. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.CHAOTIC, val));
  522. }
  523. else if ("olympiad".equalsIgnoreCase(a.getNodeName()))
  524. {
  525. boolean val = Boolean.parseBoolean(a.getNodeValue());
  526. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.OLYMPIAD, val));
  527. }
  528. else if ("ishero".equalsIgnoreCase(a.getNodeName()))
  529. {
  530. boolean val = Boolean.parseBoolean(a.getNodeValue());
  531. cond = joinAnd(cond, new ConditionPlayerIsHero(val));
  532. }
  533. else if ("transformationId".equalsIgnoreCase(a.getNodeName()))
  534. {
  535. int id = Integer.parseInt(a.getNodeValue());
  536. cond = joinAnd(cond, new ConditionPlayerTransformationId(id));
  537. }
  538. else if ("hp".equalsIgnoreCase(a.getNodeName()))
  539. {
  540. int hp = Integer.decode(getValue(a.getNodeValue(), null));
  541. cond = joinAnd(cond, new ConditionPlayerHp(hp));
  542. }
  543. else if ("mp".equalsIgnoreCase(a.getNodeName()))
  544. {
  545. int hp = Integer.decode(getValue(a.getNodeValue(), null));
  546. cond = joinAnd(cond, new ConditionPlayerMp(hp));
  547. }
  548. else if ("cp".equalsIgnoreCase(a.getNodeName()))
  549. {
  550. int cp = Integer.decode(getValue(a.getNodeValue(), null));
  551. cond = joinAnd(cond, new ConditionPlayerCp(cp));
  552. }
  553. else if ("grade".equalsIgnoreCase(a.getNodeName()))
  554. {
  555. int expIndex = Integer.decode(getValue(a.getNodeValue(), template));
  556. cond = joinAnd(cond, new ConditionPlayerGrade(expIndex));
  557. }
  558. else if ("pkCount".equalsIgnoreCase(a.getNodeName()))
  559. {
  560. int expIndex = Integer.decode(getValue(a.getNodeValue(), template));
  561. cond = joinAnd(cond, new ConditionPlayerPkCount(expIndex));
  562. }
  563. else if ("siegezone".equalsIgnoreCase(a.getNodeName()))
  564. {
  565. int value = Integer.decode(getValue(a.getNodeValue(), null));
  566. cond = joinAnd(cond, new ConditionSiegeZone(value, true));
  567. }
  568. else if ("siegeside".equalsIgnoreCase(a.getNodeName()))
  569. {
  570. int value = Integer.decode(getValue(a.getNodeValue(), null));
  571. cond = joinAnd(cond, new ConditionPlayerSiegeSide(value));
  572. }
  573. else if ("charges".equalsIgnoreCase(a.getNodeName()))
  574. {
  575. int value = Integer.decode(getValue(a.getNodeValue(), template));
  576. cond = joinAnd(cond, new ConditionPlayerCharges(value));
  577. }
  578. else if ("souls".equalsIgnoreCase(a.getNodeName()))
  579. {
  580. int value = Integer.decode(getValue(a.getNodeValue(), template));
  581. cond = joinAnd(cond, new ConditionPlayerSouls(value));
  582. }
  583. else if ("weight".equalsIgnoreCase(a.getNodeName()))
  584. {
  585. int weight = Integer.decode(getValue(a.getNodeValue(), null));
  586. cond = joinAnd(cond, new ConditionPlayerWeight(weight));
  587. }
  588. else if ("invSize".equalsIgnoreCase(a.getNodeName()))
  589. {
  590. int size = Integer.decode(getValue(a.getNodeValue(), null));
  591. cond = joinAnd(cond, new ConditionPlayerInvSize(size));
  592. }
  593. else if ("isClanLeader".equalsIgnoreCase(a.getNodeName()))
  594. {
  595. boolean val = Boolean.parseBoolean(a.getNodeValue());
  596. cond = joinAnd(cond, new ConditionPlayerIsClanLeader(val));
  597. }
  598. else if ("onTvTEvent".equalsIgnoreCase(a.getNodeName()))
  599. {
  600. boolean val = Boolean.parseBoolean(a.getNodeValue());
  601. cond = joinAnd(cond, new ConditionPlayerTvTEvent(val));
  602. }
  603. else if ("pledgeClass".equalsIgnoreCase(a.getNodeName()))
  604. {
  605. int pledgeClass = Integer.decode(getValue(a.getNodeValue(), null));
  606. cond = joinAnd(cond, new ConditionPlayerPledgeClass(pledgeClass));
  607. }
  608. else if ("clanHall".equalsIgnoreCase(a.getNodeName()))
  609. {
  610. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  611. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  612. while (st.hasMoreTokens())
  613. {
  614. String item = st.nextToken().trim();
  615. array.add(Integer.decode(getValue(item, null)));
  616. }
  617. cond = joinAnd(cond, new ConditionPlayerHasClanHall(array));
  618. }
  619. else if ("fort".equalsIgnoreCase(a.getNodeName()))
  620. {
  621. int fort = Integer.decode(getValue(a.getNodeValue(), null));
  622. cond = joinAnd(cond, new ConditionPlayerHasFort(fort));
  623. }
  624. else if ("castle".equalsIgnoreCase(a.getNodeName()))
  625. {
  626. int castle = Integer.decode(getValue(a.getNodeValue(), null));
  627. cond = joinAnd(cond, new ConditionPlayerHasCastle(castle));
  628. }
  629. else if ("sex".equalsIgnoreCase(a.getNodeName()))
  630. {
  631. int sex = Integer.decode(getValue(a.getNodeValue(), null));
  632. cond = joinAnd(cond, new ConditionPlayerSex(sex));
  633. }
  634. else if ("flyMounted".equalsIgnoreCase(a.getNodeName()))
  635. {
  636. boolean val = Boolean.parseBoolean(a.getNodeValue());
  637. cond = joinAnd(cond, new ConditionPlayerFlyMounted(val));
  638. }
  639. else if ("vehicleMounted".equalsIgnoreCase(a.getNodeName()))
  640. {
  641. boolean val = Boolean.parseBoolean(a.getNodeValue());
  642. cond = joinAnd(cond, new ConditionPlayerVehicleMounted(val));
  643. }
  644. else if ("landingZone".equalsIgnoreCase(a.getNodeName()))
  645. {
  646. boolean val = Boolean.parseBoolean(a.getNodeValue());
  647. cond = joinAnd(cond, new ConditionPlayerLandingZone(val));
  648. }
  649. else if ("active_effect_id".equalsIgnoreCase(a.getNodeName()))
  650. {
  651. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  652. cond = joinAnd(cond, new ConditionPlayerActiveEffectId(effect_id));
  653. }
  654. else if ("active_effect_id_lvl".equalsIgnoreCase(a.getNodeName()))
  655. {
  656. String val = getValue(a.getNodeValue(), template);
  657. int effect_id = Integer.decode(getValue(val.split(",")[0], template));
  658. int effect_lvl = Integer.decode(getValue(val.split(",")[1], template));
  659. cond = joinAnd(cond, new ConditionPlayerActiveEffectId(effect_id, effect_lvl));
  660. }
  661. else if ("active_skill_id".equalsIgnoreCase(a.getNodeName()))
  662. {
  663. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  664. cond = joinAnd(cond, new ConditionPlayerActiveSkillId(skill_id));
  665. }
  666. else if ("active_skill_id_lvl".equalsIgnoreCase(a.getNodeName()))
  667. {
  668. String val = getValue(a.getNodeValue(), template);
  669. int skill_id = Integer.decode(getValue(val.split(",")[0], template));
  670. int skill_lvl = Integer.decode(getValue(val.split(",")[1], template));
  671. cond = joinAnd(cond, new ConditionPlayerActiveSkillId(skill_id, skill_lvl));
  672. }
  673. else if ("class_id_restriction".equalsIgnoreCase(a.getNodeName()))
  674. {
  675. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  676. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  677. while (st.hasMoreTokens())
  678. {
  679. String item = st.nextToken().trim();
  680. array.add(Integer.decode(getValue(item, null)));
  681. }
  682. cond = joinAnd(cond, new ConditionPlayerClassIdRestriction(array));
  683. }
  684. else if ("subclass".equalsIgnoreCase(a.getNodeName()))
  685. {
  686. boolean val = Boolean.parseBoolean(a.getNodeValue());
  687. cond = joinAnd(cond, new ConditionPlayerSubclass(val));
  688. }
  689. else if ("instanceId".equalsIgnoreCase(a.getNodeName()))
  690. {
  691. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  692. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  693. while (st.hasMoreTokens())
  694. {
  695. String item = st.nextToken().trim();
  696. array.add(Integer.decode(getValue(item, null)));
  697. }
  698. cond = joinAnd(cond, new ConditionPlayerInstanceId(array));
  699. }
  700. else if ("agathionId".equalsIgnoreCase(a.getNodeName()))
  701. {
  702. int agathionId = Integer.decode(a.getNodeValue());
  703. cond = joinAnd(cond, new ConditionPlayerAgathionId(agathionId));
  704. }
  705. else if ("cloakStatus".equalsIgnoreCase(a.getNodeName()))
  706. {
  707. int val = Integer.parseInt(a.getNodeValue());
  708. cond = joinAnd(cond, new ConditionPlayerCloakStatus(val));
  709. }
  710. else if ("hasPet".equalsIgnoreCase(a.getNodeName()))
  711. {
  712. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  713. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  714. while (st.hasMoreTokens())
  715. {
  716. String item = st.nextToken().trim();
  717. array.add(Integer.decode(getValue(item, null)));
  718. }
  719. cond = joinAnd(cond, new ConditionPlayerHasPet(array));
  720. }
  721. else if ("servitorNpcId".equalsIgnoreCase(a.getNodeName()))
  722. {
  723. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  724. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  725. while (st.hasMoreTokens())
  726. {
  727. String item = st.nextToken().trim();
  728. array.add(Integer.decode(getValue(item, null)));
  729. }
  730. cond = joinAnd(cond, new ConditionPlayerServitorNpcId(array));
  731. }
  732. else if ("npcIdRadius".equalsIgnoreCase(a.getNodeName()))
  733. {
  734. final StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  735. if (st.countTokens() == 3)
  736. {
  737. final String[] ids = st.nextToken().split(";");
  738. final int[] npcIds = new int[ids.length];
  739. for (int index = 0; index < ids.length; index++)
  740. {
  741. npcIds[index] = Integer.parseInt(getValue(ids[index], template));
  742. }
  743. final int radius = Integer.parseInt(st.nextToken());
  744. final boolean val = Boolean.parseBoolean(st.nextToken());
  745. cond = joinAnd(cond, new ConditionPlayerRangeFromNpc(npcIds, radius, val));
  746. }
  747. }
  748. else if ("callPc".equalsIgnoreCase(a.getNodeName()))
  749. {
  750. cond = joinAnd(cond, new ConditionPlayerCallPc(Boolean.parseBoolean(a.getNodeValue())));
  751. }
  752. else if ("canEscape".equalsIgnoreCase(a.getNodeName()))
  753. {
  754. cond = joinAnd(cond, new ConditionPlayerCanEscape(Boolean.parseBoolean(a.getNodeValue())));
  755. }
  756. else if ("canPossessHolything".equalsIgnoreCase(a.getNodeName()))
  757. {
  758. cond = joinAnd(cond, new ConditionPlayerCanPossessHolything(Boolean.parseBoolean(a.getNodeValue())));
  759. }
  760. else if ("canRefuelAirship".equalsIgnoreCase(a.getNodeName()))
  761. {
  762. cond = joinAnd(cond, new ConditionPlayerCanRefuelAirship(Integer.parseInt(a.getNodeValue())));
  763. }
  764. else if ("canSummon".equalsIgnoreCase(a.getNodeName()))
  765. {
  766. cond = joinAnd(cond, new ConditionPlayerCanSummon(Boolean.parseBoolean(a.getNodeValue())));
  767. }
  768. else if ("canSweep".equalsIgnoreCase(a.getNodeName()))
  769. {
  770. cond = joinAnd(cond, new ConditionPlayerCanSweep(Boolean.parseBoolean(a.getNodeValue())));
  771. }
  772. else if ("canTransform".equalsIgnoreCase(a.getNodeName()))
  773. {
  774. cond = joinAnd(cond, new ConditionPlayerCanTransform(Boolean.parseBoolean(a.getNodeValue())));
  775. }
  776. else if ("canUntransform".equalsIgnoreCase(a.getNodeName()))
  777. {
  778. cond = joinAnd(cond, new ConditionPlayerCanUntransform(Boolean.parseBoolean(a.getNodeValue())));
  779. }
  780. else if ("insideZoneId".equalsIgnoreCase(a.getNodeName()))
  781. {
  782. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  783. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  784. while (st.hasMoreTokens())
  785. {
  786. String item = st.nextToken().trim();
  787. array.add(Integer.decode(getValue(item, null)));
  788. }
  789. cond = joinAnd(cond, new ConditionPlayerInsideZoneId(array));
  790. }
  791. }
  792. if (cond == null)
  793. {
  794. _log.severe("Unrecognized <player> condition in " + _file);
  795. }
  796. return cond;
  797. }
  798. protected Condition parseTargetCondition(Node n, Object template)
  799. {
  800. Condition cond = null;
  801. NamedNodeMap attrs = n.getAttributes();
  802. for (int i = 0; i < attrs.getLength(); i++)
  803. {
  804. Node a = attrs.item(i);
  805. if ("aggro".equalsIgnoreCase(a.getNodeName()))
  806. {
  807. boolean val = Boolean.parseBoolean(a.getNodeValue());
  808. cond = joinAnd(cond, new ConditionTargetAggro(val));
  809. }
  810. else if ("siegezone".equalsIgnoreCase(a.getNodeName()))
  811. {
  812. int value = Integer.decode(getValue(a.getNodeValue(), null));
  813. cond = joinAnd(cond, new ConditionSiegeZone(value, false));
  814. }
  815. else if ("level".equalsIgnoreCase(a.getNodeName()))
  816. {
  817. int lvl = Integer.decode(getValue(a.getNodeValue(), template));
  818. cond = joinAnd(cond, new ConditionTargetLevel(lvl));
  819. }
  820. else if ("levelRange".equalsIgnoreCase(a.getNodeName()))
  821. {
  822. String[] range = getValue(a.getNodeValue(), template).split(";");
  823. if (range.length == 2)
  824. {
  825. int[] lvlRange = new int[2];
  826. lvlRange[0] = Integer.decode(getValue(a.getNodeValue(), template).split(";")[0]);
  827. lvlRange[1] = Integer.decode(getValue(a.getNodeValue(), template).split(";")[1]);
  828. cond = joinAnd(cond, new ConditionTargetLevelRange(lvlRange));
  829. }
  830. }
  831. else if ("myPartyExceptMe".equalsIgnoreCase(a.getNodeName()))
  832. {
  833. cond = joinAnd(cond, new ConditionTargetMyPartyExceptMe(Boolean.parseBoolean(a.getNodeValue())));
  834. }
  835. else if ("playable".equalsIgnoreCase(a.getNodeName()))
  836. {
  837. cond = joinAnd(cond, new ConditionTargetPlayable());
  838. }
  839. else if ("class_id_restriction".equalsIgnoreCase(a.getNodeName()))
  840. {
  841. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  842. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  843. while (st.hasMoreTokens())
  844. {
  845. String item = st.nextToken().trim();
  846. array.add(Integer.decode(getValue(item, null)));
  847. }
  848. cond = joinAnd(cond, new ConditionTargetClassIdRestriction(array));
  849. }
  850. else if ("active_effect_id".equalsIgnoreCase(a.getNodeName()))
  851. {
  852. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  853. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id));
  854. }
  855. else if ("active_effect_id_lvl".equalsIgnoreCase(a.getNodeName()))
  856. {
  857. String val = getValue(a.getNodeValue(), template);
  858. int effect_id = Integer.decode(getValue(val.split(",")[0], template));
  859. int effect_lvl = Integer.decode(getValue(val.split(",")[1], template));
  860. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id, effect_lvl));
  861. }
  862. else if ("active_skill_id".equalsIgnoreCase(a.getNodeName()))
  863. {
  864. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  865. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id));
  866. }
  867. else if ("active_skill_id_lvl".equalsIgnoreCase(a.getNodeName()))
  868. {
  869. String val = getValue(a.getNodeValue(), template);
  870. int skill_id = Integer.decode(getValue(val.split(",")[0], template));
  871. int skill_lvl = Integer.decode(getValue(val.split(",")[1], template));
  872. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id, skill_lvl));
  873. }
  874. else if ("abnormal".equalsIgnoreCase(a.getNodeName()))
  875. {
  876. int abnormalId = Integer.decode(getValue(a.getNodeValue(), template));
  877. cond = joinAnd(cond, new ConditionTargetAbnormal(abnormalId));
  878. }
  879. else if ("mindistance".equalsIgnoreCase(a.getNodeName()))
  880. {
  881. int distance = Integer.decode(getValue(a.getNodeValue(), null));
  882. cond = joinAnd(cond, new ConditionMinDistance(distance * distance));
  883. }
  884. // used for npc race
  885. else if ("race_id".equalsIgnoreCase(a.getNodeName()))
  886. {
  887. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  888. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  889. while (st.hasMoreTokens())
  890. {
  891. String item = st.nextToken().trim();
  892. array.add(Integer.decode(getValue(item, null)));
  893. }
  894. cond = joinAnd(cond, new ConditionTargetRaceId(array));
  895. }
  896. // used for pc race
  897. else if ("races".equalsIgnoreCase(a.getNodeName()))
  898. {
  899. final String[] racesVal = a.getNodeValue().split(",");
  900. final PcRace[] races = new PcRace[racesVal.length];
  901. for (int r = 0; r < racesVal.length; r++)
  902. {
  903. if (racesVal[r] != null)
  904. {
  905. races[r] = PcRace.valueOf(racesVal[r]);
  906. }
  907. }
  908. cond = joinAnd(cond, new ConditionTargetRace(races));
  909. }
  910. else if ("using".equalsIgnoreCase(a.getNodeName()))
  911. {
  912. int mask = 0;
  913. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  914. while (st.hasMoreTokens())
  915. {
  916. String item = st.nextToken().trim();
  917. for (L2WeaponType wt : L2WeaponType.values())
  918. {
  919. if (wt.getName().equals(item))
  920. {
  921. mask |= wt.mask();
  922. break;
  923. }
  924. }
  925. for (L2ArmorType at : L2ArmorType.values())
  926. {
  927. if (at.getName().equals(item))
  928. {
  929. mask |= at.mask();
  930. break;
  931. }
  932. }
  933. }
  934. cond = joinAnd(cond, new ConditionTargetUsesWeaponKind(mask));
  935. }
  936. else if ("npcId".equalsIgnoreCase(a.getNodeName()))
  937. {
  938. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  939. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  940. while (st.hasMoreTokens())
  941. {
  942. String item = st.nextToken().trim();
  943. array.add(Integer.decode(getValue(item, null)));
  944. }
  945. cond = joinAnd(cond, new ConditionTargetNpcId(array));
  946. }
  947. else if ("npcType".equalsIgnoreCase(a.getNodeName()))
  948. {
  949. String values = getValue(a.getNodeValue(), template).trim();
  950. String[] valuesSplit = values.split(",");
  951. InstanceType[] types = new InstanceType[valuesSplit.length];
  952. InstanceType type;
  953. for (int j = 0; j < valuesSplit.length; j++)
  954. {
  955. type = Enum.valueOf(InstanceType.class, valuesSplit[j]);
  956. if (type == null)
  957. {
  958. throw new IllegalArgumentException("Instance type not recognized: " + valuesSplit[j]);
  959. }
  960. types[j] = type;
  961. }
  962. cond = joinAnd(cond, new ConditionTargetNpcType(types));
  963. }
  964. else if ("weight".equalsIgnoreCase(a.getNodeName()))
  965. {
  966. int weight = Integer.decode(getValue(a.getNodeValue(), null));
  967. cond = joinAnd(cond, new ConditionTargetWeight(weight));
  968. }
  969. else if ("invSize".equalsIgnoreCase(a.getNodeName()))
  970. {
  971. int size = Integer.decode(getValue(a.getNodeValue(), null));
  972. cond = joinAnd(cond, new ConditionTargetInvSize(size));
  973. }
  974. }
  975. if (cond == null)
  976. {
  977. _log.severe("Unrecognized <target> condition in " + _file);
  978. }
  979. return cond;
  980. }
  981. protected Condition parseUsingCondition(Node n)
  982. {
  983. Condition cond = null;
  984. NamedNodeMap attrs = n.getAttributes();
  985. for (int i = 0; i < attrs.getLength(); i++)
  986. {
  987. Node a = attrs.item(i);
  988. if ("kind".equalsIgnoreCase(a.getNodeName()))
  989. {
  990. int mask = 0;
  991. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  992. while (st.hasMoreTokens())
  993. {
  994. int old = mask;
  995. String item = st.nextToken().trim();
  996. if (ItemTable._weaponTypes.containsKey(item))
  997. {
  998. mask |= ItemTable._weaponTypes.get(item).mask();
  999. }
  1000. if (ItemTable._armorTypes.containsKey(item))
  1001. {
  1002. mask |= ItemTable._armorTypes.get(item).mask();
  1003. }
  1004. if (old == mask)
  1005. {
  1006. _log.info("[parseUsingCondition=\"kind\"] Unknown item type name: " + item);
  1007. }
  1008. }
  1009. cond = joinAnd(cond, new ConditionUsingItemType(mask));
  1010. }
  1011. else if ("skill".equalsIgnoreCase(a.getNodeName()))
  1012. {
  1013. int id = Integer.parseInt(a.getNodeValue());
  1014. cond = joinAnd(cond, new ConditionUsingSkill(id));
  1015. }
  1016. else if ("slotitem".equalsIgnoreCase(a.getNodeName()))
  1017. {
  1018. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ";");
  1019. int id = Integer.parseInt(st.nextToken().trim());
  1020. int slot = Integer.parseInt(st.nextToken().trim());
  1021. int enchant = 0;
  1022. if (st.hasMoreTokens())
  1023. {
  1024. enchant = Integer.parseInt(st.nextToken().trim());
  1025. }
  1026. cond = joinAnd(cond, new ConditionSlotItemId(slot, id, enchant));
  1027. }
  1028. else if ("weaponChange".equalsIgnoreCase(a.getNodeName()))
  1029. {
  1030. boolean val = Boolean.parseBoolean(a.getNodeValue());
  1031. cond = joinAnd(cond, new ConditionChangeWeapon(val));
  1032. }
  1033. }
  1034. if (cond == null)
  1035. {
  1036. _log.severe("Unrecognized <using> condition in " + _file);
  1037. }
  1038. return cond;
  1039. }
  1040. protected Condition parseGameCondition(Node n)
  1041. {
  1042. Condition cond = null;
  1043. NamedNodeMap attrs = n.getAttributes();
  1044. for (int i = 0; i < attrs.getLength(); i++)
  1045. {
  1046. Node a = attrs.item(i);
  1047. if ("skill".equalsIgnoreCase(a.getNodeName()))
  1048. {
  1049. boolean val = Boolean.parseBoolean(a.getNodeValue());
  1050. cond = joinAnd(cond, new ConditionWithSkill(val));
  1051. }
  1052. if ("night".equalsIgnoreCase(a.getNodeName()))
  1053. {
  1054. boolean val = Boolean.parseBoolean(a.getNodeValue());
  1055. cond = joinAnd(cond, new ConditionGameTime(CheckGameTime.NIGHT, val));
  1056. }
  1057. if ("chance".equalsIgnoreCase(a.getNodeName()))
  1058. {
  1059. int val = Integer.decode(getValue(a.getNodeValue(), null));
  1060. cond = joinAnd(cond, new ConditionGameChance(val));
  1061. }
  1062. }
  1063. if (cond == null)
  1064. {
  1065. _log.severe("Unrecognized <game> condition in " + _file);
  1066. }
  1067. return cond;
  1068. }
  1069. protected void parseTable(Node n)
  1070. {
  1071. NamedNodeMap attrs = n.getAttributes();
  1072. String name = attrs.getNamedItem("name").getNodeValue();
  1073. if (name.charAt(0) != '#')
  1074. {
  1075. throw new IllegalArgumentException("Table name must start with #");
  1076. }
  1077. StringTokenizer data = new StringTokenizer(n.getFirstChild().getNodeValue());
  1078. List<String> array = new ArrayList<>(data.countTokens());
  1079. while (data.hasMoreTokens())
  1080. {
  1081. array.add(data.nextToken());
  1082. }
  1083. setTable(name, array.toArray(new String[array.size()]));
  1084. }
  1085. protected void parseBeanSet(Node n, StatsSet set, Integer level)
  1086. {
  1087. String name = n.getAttributes().getNamedItem("name").getNodeValue().trim();
  1088. String value = n.getAttributes().getNamedItem("val").getNodeValue().trim();
  1089. char ch = value.isEmpty() ? ' ' : value.charAt(0);
  1090. if ((ch == '#') || (ch == '-') || Character.isDigit(ch))
  1091. {
  1092. set.set(name, String.valueOf(getValue(value, level)));
  1093. }
  1094. else
  1095. {
  1096. set.set(name, value);
  1097. }
  1098. }
  1099. protected void setExtractableSkillData(StatsSet set, String value)
  1100. {
  1101. set.set("capsuled_items_skill", value);
  1102. }
  1103. protected Lambda getLambda(Node n, Object template)
  1104. {
  1105. Node nval = n.getAttributes().getNamedItem("val");
  1106. if (nval != null)
  1107. {
  1108. String val = nval.getNodeValue();
  1109. if (val.charAt(0) == '#')
  1110. { // table by level
  1111. return new LambdaConst(Double.parseDouble(getTableValue(val)));
  1112. }
  1113. else if (val.charAt(0) == '$')
  1114. {
  1115. if (val.equalsIgnoreCase("$player_level"))
  1116. {
  1117. return new LambdaStats(LambdaStats.StatsType.PLAYER_LEVEL);
  1118. }
  1119. if (val.equalsIgnoreCase("$target_level"))
  1120. {
  1121. return new LambdaStats(LambdaStats.StatsType.TARGET_LEVEL);
  1122. }
  1123. if (val.equalsIgnoreCase("$player_max_hp"))
  1124. {
  1125. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_HP);
  1126. }
  1127. if (val.equalsIgnoreCase("$player_max_mp"))
  1128. {
  1129. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_MP);
  1130. }
  1131. // try to find value out of item fields
  1132. StatsSet set = getStatsSet();
  1133. String field = set.getString(val.substring(1));
  1134. if (field != null)
  1135. {
  1136. return new LambdaConst(Double.parseDouble(getValue(field, template)));
  1137. }
  1138. // failed
  1139. throw new IllegalArgumentException("Unknown value " + val);
  1140. }
  1141. else
  1142. {
  1143. return new LambdaConst(Double.parseDouble(val));
  1144. }
  1145. }
  1146. LambdaCalc calc = new LambdaCalc();
  1147. n = n.getFirstChild();
  1148. while ((n != null) && (n.getNodeType() != Node.ELEMENT_NODE))
  1149. {
  1150. n = n.getNextSibling();
  1151. }
  1152. if ((n == null) || !"val".equals(n.getNodeName()))
  1153. {
  1154. throw new IllegalArgumentException("Value not specified");
  1155. }
  1156. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  1157. {
  1158. if (n.getNodeType() != Node.ELEMENT_NODE)
  1159. {
  1160. continue;
  1161. }
  1162. attachLambdaFunc(n, template, calc);
  1163. }
  1164. return calc;
  1165. }
  1166. protected String getValue(String value, Object template)
  1167. {
  1168. // is it a table?
  1169. if (value.charAt(0) == '#')
  1170. {
  1171. if (template instanceof L2Skill)
  1172. {
  1173. return getTableValue(value);
  1174. }
  1175. else if (template instanceof Integer)
  1176. {
  1177. return getTableValue(value, ((Integer) template).intValue());
  1178. }
  1179. else
  1180. {
  1181. throw new IllegalStateException();
  1182. }
  1183. }
  1184. return value;
  1185. }
  1186. protected Condition joinAnd(Condition cond, Condition c)
  1187. {
  1188. if (cond == null)
  1189. {
  1190. return c;
  1191. }
  1192. if (cond instanceof ConditionLogicAnd)
  1193. {
  1194. ((ConditionLogicAnd) cond).add(c);
  1195. return cond;
  1196. }
  1197. ConditionLogicAnd and = new ConditionLogicAnd();
  1198. and.add(cond);
  1199. and.add(c);
  1200. return and;
  1201. }
  1202. }