DocumentBase.java 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /*
  2. * Copyright (C) 2004-2014 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.HashSet;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import java.util.StringTokenizer;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import javax.xml.parsers.DocumentBuilderFactory;
  30. import javolution.util.FastMap;
  31. import org.w3c.dom.Document;
  32. import org.w3c.dom.NamedNodeMap;
  33. import org.w3c.dom.Node;
  34. import com.l2jserver.gameserver.datatables.ItemTable;
  35. import com.l2jserver.gameserver.enums.InstanceType;
  36. import com.l2jserver.gameserver.enums.NpcRace;
  37. import com.l2jserver.gameserver.enums.PcRace;
  38. import com.l2jserver.gameserver.model.StatsSet;
  39. import com.l2jserver.gameserver.model.base.PlayerState;
  40. import com.l2jserver.gameserver.model.conditions.Condition;
  41. import com.l2jserver.gameserver.model.conditions.ConditionChangeWeapon;
  42. import com.l2jserver.gameserver.model.conditions.ConditionGameChance;
  43. import com.l2jserver.gameserver.model.conditions.ConditionGameTime;
  44. import com.l2jserver.gameserver.model.conditions.ConditionGameTime.CheckGameTime;
  45. import com.l2jserver.gameserver.model.conditions.ConditionLogicAnd;
  46. import com.l2jserver.gameserver.model.conditions.ConditionLogicNot;
  47. import com.l2jserver.gameserver.model.conditions.ConditionLogicOr;
  48. import com.l2jserver.gameserver.model.conditions.ConditionMinDistance;
  49. import com.l2jserver.gameserver.model.conditions.ConditionPlayerActiveEffectId;
  50. import com.l2jserver.gameserver.model.conditions.ConditionPlayerActiveSkillId;
  51. import com.l2jserver.gameserver.model.conditions.ConditionPlayerAgathionId;
  52. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCallPc;
  53. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanEscape;
  54. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanPossessHolything;
  55. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanRefuelAirship;
  56. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanSummon;
  57. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanSweep;
  58. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanTransform;
  59. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCanUntransform;
  60. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCharges;
  61. import com.l2jserver.gameserver.model.conditions.ConditionPlayerClassIdRestriction;
  62. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCloakStatus;
  63. import com.l2jserver.gameserver.model.conditions.ConditionPlayerCp;
  64. import com.l2jserver.gameserver.model.conditions.ConditionPlayerFlyMounted;
  65. import com.l2jserver.gameserver.model.conditions.ConditionPlayerGrade;
  66. import com.l2jserver.gameserver.model.conditions.ConditionPlayerHasCastle;
  67. import com.l2jserver.gameserver.model.conditions.ConditionPlayerHasClanHall;
  68. import com.l2jserver.gameserver.model.conditions.ConditionPlayerHasFort;
  69. import com.l2jserver.gameserver.model.conditions.ConditionPlayerHasPet;
  70. import com.l2jserver.gameserver.model.conditions.ConditionPlayerHp;
  71. import com.l2jserver.gameserver.model.conditions.ConditionPlayerInsideZoneId;
  72. import com.l2jserver.gameserver.model.conditions.ConditionPlayerInstanceId;
  73. import com.l2jserver.gameserver.model.conditions.ConditionPlayerInvSize;
  74. import com.l2jserver.gameserver.model.conditions.ConditionPlayerIsClanLeader;
  75. import com.l2jserver.gameserver.model.conditions.ConditionPlayerIsHero;
  76. import com.l2jserver.gameserver.model.conditions.ConditionPlayerLandingZone;
  77. import com.l2jserver.gameserver.model.conditions.ConditionPlayerLevel;
  78. import com.l2jserver.gameserver.model.conditions.ConditionPlayerLevelRange;
  79. import com.l2jserver.gameserver.model.conditions.ConditionPlayerMp;
  80. import com.l2jserver.gameserver.model.conditions.ConditionPlayerPkCount;
  81. import com.l2jserver.gameserver.model.conditions.ConditionPlayerPledgeClass;
  82. import com.l2jserver.gameserver.model.conditions.ConditionPlayerRace;
  83. import com.l2jserver.gameserver.model.conditions.ConditionPlayerRangeFromNpc;
  84. import com.l2jserver.gameserver.model.conditions.ConditionPlayerServitorNpcId;
  85. import com.l2jserver.gameserver.model.conditions.ConditionPlayerSex;
  86. import com.l2jserver.gameserver.model.conditions.ConditionPlayerSiegeSide;
  87. import com.l2jserver.gameserver.model.conditions.ConditionPlayerSouls;
  88. import com.l2jserver.gameserver.model.conditions.ConditionPlayerState;
  89. import com.l2jserver.gameserver.model.conditions.ConditionPlayerSubclass;
  90. import com.l2jserver.gameserver.model.conditions.ConditionPlayerTransformationId;
  91. import com.l2jserver.gameserver.model.conditions.ConditionPlayerTvTEvent;
  92. import com.l2jserver.gameserver.model.conditions.ConditionPlayerVehicleMounted;
  93. import com.l2jserver.gameserver.model.conditions.ConditionPlayerWeight;
  94. import com.l2jserver.gameserver.model.conditions.ConditionSiegeZone;
  95. import com.l2jserver.gameserver.model.conditions.ConditionSlotItemId;
  96. import com.l2jserver.gameserver.model.conditions.ConditionTargetAbnormal;
  97. import com.l2jserver.gameserver.model.conditions.ConditionTargetActiveEffectId;
  98. import com.l2jserver.gameserver.model.conditions.ConditionTargetActiveSkillId;
  99. import com.l2jserver.gameserver.model.conditions.ConditionTargetAggro;
  100. import com.l2jserver.gameserver.model.conditions.ConditionTargetClassIdRestriction;
  101. import com.l2jserver.gameserver.model.conditions.ConditionTargetInvSize;
  102. import com.l2jserver.gameserver.model.conditions.ConditionTargetLevel;
  103. import com.l2jserver.gameserver.model.conditions.ConditionTargetLevelRange;
  104. import com.l2jserver.gameserver.model.conditions.ConditionTargetMyPartyExceptMe;
  105. import com.l2jserver.gameserver.model.conditions.ConditionTargetNpcId;
  106. import com.l2jserver.gameserver.model.conditions.ConditionTargetNpcRace;
  107. import com.l2jserver.gameserver.model.conditions.ConditionTargetNpcType;
  108. import com.l2jserver.gameserver.model.conditions.ConditionTargetPlayable;
  109. import com.l2jserver.gameserver.model.conditions.ConditionTargetRace;
  110. import com.l2jserver.gameserver.model.conditions.ConditionTargetUsesWeaponKind;
  111. import com.l2jserver.gameserver.model.conditions.ConditionTargetWeight;
  112. import com.l2jserver.gameserver.model.conditions.ConditionUsingItemType;
  113. import com.l2jserver.gameserver.model.conditions.ConditionUsingSkill;
  114. import com.l2jserver.gameserver.model.conditions.ConditionWithSkill;
  115. import com.l2jserver.gameserver.model.effects.AbstractEffect;
  116. import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
  117. import com.l2jserver.gameserver.model.items.L2Item;
  118. import com.l2jserver.gameserver.model.items.type.L2ArmorType;
  119. import com.l2jserver.gameserver.model.items.type.L2WeaponType;
  120. import com.l2jserver.gameserver.model.skills.EffectScope;
  121. import com.l2jserver.gameserver.model.skills.L2Skill;
  122. import com.l2jserver.gameserver.model.skills.funcs.FuncTemplate;
  123. import com.l2jserver.gameserver.model.skills.funcs.Lambda;
  124. import com.l2jserver.gameserver.model.skills.funcs.LambdaCalc;
  125. import com.l2jserver.gameserver.model.skills.funcs.LambdaConst;
  126. import com.l2jserver.gameserver.model.skills.funcs.LambdaStats;
  127. import com.l2jserver.gameserver.model.stats.Env;
  128. import com.l2jserver.gameserver.model.stats.Stats;
  129. /**
  130. * @author mkizub
  131. */
  132. public abstract class DocumentBase
  133. {
  134. protected final Logger _log = Logger.getLogger(getClass().getName());
  135. private final File _file;
  136. protected Map<String, String[]> _tables;
  137. protected DocumentBase(File pFile)
  138. {
  139. _file = pFile;
  140. _tables = new FastMap<>();
  141. }
  142. public Document parse()
  143. {
  144. Document doc = null;
  145. try
  146. {
  147. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  148. factory.setValidating(false);
  149. factory.setIgnoringComments(true);
  150. doc = factory.newDocumentBuilder().parse(_file);
  151. parseDocument(doc);
  152. }
  153. catch (Exception e)
  154. {
  155. _log.log(Level.SEVERE, "Error loading file " + _file, e);
  156. }
  157. return doc;
  158. }
  159. protected abstract void parseDocument(Document doc);
  160. protected abstract StatsSet getStatsSet();
  161. protected abstract String getTableValue(String name);
  162. protected abstract String getTableValue(String name, int idx);
  163. protected void resetTable()
  164. {
  165. _tables = new FastMap<>();
  166. }
  167. protected void setTable(String name, String[] table)
  168. {
  169. _tables.put(name, table);
  170. }
  171. protected void parseTemplate(Node n, Object template)
  172. {
  173. parseTemplate(n, template, null);
  174. }
  175. protected void parseTemplate(Node n, Object template, EffectScope effectScope)
  176. {
  177. Condition condition = null;
  178. n = n.getFirstChild();
  179. if (n == null)
  180. {
  181. return;
  182. }
  183. if ("cond".equalsIgnoreCase(n.getNodeName()))
  184. {
  185. condition = parseCondition(n.getFirstChild(), template);
  186. Node msg = n.getAttributes().getNamedItem("msg");
  187. Node msgId = n.getAttributes().getNamedItem("msgId");
  188. if ((condition != null) && (msg != null))
  189. {
  190. condition.setMessage(msg.getNodeValue());
  191. }
  192. else if ((condition != null) && (msgId != null))
  193. {
  194. condition.setMessageId(Integer.decode(getValue(msgId.getNodeValue(), null)));
  195. Node addName = n.getAttributes().getNamedItem("addName");
  196. if ((addName != null) && (Integer.decode(getValue(msgId.getNodeValue(), null)) > 0))
  197. {
  198. condition.addName();
  199. }
  200. }
  201. n = n.getNextSibling();
  202. }
  203. for (; n != null; n = n.getNextSibling())
  204. {
  205. if ("add".equalsIgnoreCase(n.getNodeName()))
  206. {
  207. attachFunc(n, template, "Add", condition);
  208. }
  209. else if ("sub".equalsIgnoreCase(n.getNodeName()))
  210. {
  211. attachFunc(n, template, "Sub", condition);
  212. }
  213. else if ("mul".equalsIgnoreCase(n.getNodeName()))
  214. {
  215. attachFunc(n, template, "Mul", condition);
  216. }
  217. else if ("basemul".equalsIgnoreCase(n.getNodeName()))
  218. {
  219. attachFunc(n, template, "BaseMul", condition);
  220. }
  221. else if ("div".equalsIgnoreCase(n.getNodeName()))
  222. {
  223. attachFunc(n, template, "Div", condition);
  224. }
  225. else if ("set".equalsIgnoreCase(n.getNodeName()))
  226. {
  227. attachFunc(n, template, "Set", condition);
  228. }
  229. else if ("share".equalsIgnoreCase(n.getNodeName()))
  230. {
  231. attachFunc(n, template, "Share", condition);
  232. }
  233. else if ("enchant".equalsIgnoreCase(n.getNodeName()))
  234. {
  235. attachFunc(n, template, "Enchant", condition);
  236. }
  237. else if ("enchanthp".equalsIgnoreCase(n.getNodeName()))
  238. {
  239. attachFunc(n, template, "EnchantHp", condition);
  240. }
  241. else if ("effect".equalsIgnoreCase(n.getNodeName()))
  242. {
  243. if (template instanceof AbstractEffect)
  244. {
  245. throw new RuntimeException("Nested effects");
  246. }
  247. attachEffect(n, template, condition, effectScope);
  248. }
  249. }
  250. }
  251. protected void attachFunc(Node n, Object template, String name, Condition attachCond)
  252. {
  253. Stats stat = Stats.valueOfXml(n.getAttributes().getNamedItem("stat").getNodeValue());
  254. String order = n.getAttributes().getNamedItem("order").getNodeValue();
  255. Lambda lambda = getLambda(n, template);
  256. int ord = Integer.decode(getValue(order, template));
  257. Condition applayCond = parseCondition(n.getFirstChild(), template);
  258. FuncTemplate ft = new FuncTemplate(attachCond, applayCond, name, stat, ord, lambda);
  259. if (template instanceof L2Item)
  260. {
  261. ((L2Item) template).attach(ft);
  262. }
  263. else if (template instanceof L2Skill)
  264. {
  265. ((L2Skill) template).attach(ft);
  266. }
  267. else if (template instanceof AbstractEffect)
  268. {
  269. ((AbstractEffect) template).attach(ft);
  270. }
  271. }
  272. protected void attachLambdaFunc(Node n, Object template, LambdaCalc calc)
  273. {
  274. String name = n.getNodeName();
  275. final StringBuilder sb = new StringBuilder(name);
  276. sb.setCharAt(0, Character.toUpperCase(name.charAt(0)));
  277. name = sb.toString();
  278. Lambda lambda = getLambda(n, template);
  279. FuncTemplate ft = new FuncTemplate(null, null, name, null, calc.funcs.length, lambda);
  280. calc.addFunc(ft.getFunc(new Env(), calc));
  281. }
  282. protected void attachEffect(Node n, Object template, Condition attachCond)
  283. {
  284. attachEffect(n, template, attachCond, null);
  285. }
  286. protected void attachEffect(Node n, Object template, Condition attachCond, EffectScope effectScope)
  287. {
  288. final NamedNodeMap attrs = n.getAttributes();
  289. final StatsSet set = new StatsSet();
  290. for (int i = 0; i < attrs.getLength(); i++)
  291. {
  292. Node att = attrs.item(i);
  293. set.set(att.getNodeName(), getValue(att.getNodeValue(), template));
  294. }
  295. final StatsSet parameters = parseParameters(n.getFirstChild(), template);
  296. final Condition applayCond = parseCondition(n.getFirstChild(), template);
  297. if (template instanceof IIdentifiable)
  298. {
  299. set.set("id", ((IIdentifiable) template).getId());
  300. }
  301. final AbstractEffect effect = AbstractEffect.createEffect(attachCond, applayCond, set, parameters);
  302. parseTemplate(n, effect);
  303. if (template instanceof L2Item)
  304. {
  305. ((L2Item) template).attach(effect);
  306. }
  307. else if (template instanceof L2Skill)
  308. {
  309. final L2Skill skill = (L2Skill) template;
  310. if (effectScope != null)
  311. {
  312. skill.addEffect(effectScope, effect);
  313. }
  314. else if (skill.isPassive())
  315. {
  316. skill.addEffect(EffectScope.PASSIVE, effect);
  317. }
  318. else
  319. {
  320. skill.addEffect(EffectScope.GENERAL, 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 ("npcRace".equalsIgnoreCase(a.getNodeName()))
  886. {
  887. final String[] values = a.getNodeValue().split(",");
  888. final Set<NpcRace> array = new HashSet<>(values.length);
  889. for (String value : values)
  890. {
  891. array.add(NpcRace.valueOf(getValue(value, null)));
  892. }
  893. cond = joinAnd(cond, new ConditionTargetNpcRace(array));
  894. }
  895. // used for pc race
  896. else if ("races".equalsIgnoreCase(a.getNodeName()))
  897. {
  898. final String[] racesVal = a.getNodeValue().split(",");
  899. final PcRace[] races = new PcRace[racesVal.length];
  900. for (int r = 0; r < racesVal.length; r++)
  901. {
  902. if (racesVal[r] != null)
  903. {
  904. races[r] = PcRace.valueOf(racesVal[r]);
  905. }
  906. }
  907. cond = joinAnd(cond, new ConditionTargetRace(races));
  908. }
  909. else if ("using".equalsIgnoreCase(a.getNodeName()))
  910. {
  911. int mask = 0;
  912. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  913. while (st.hasMoreTokens())
  914. {
  915. String item = st.nextToken().trim();
  916. for (L2WeaponType wt : L2WeaponType.values())
  917. {
  918. if (wt.getName().equals(item))
  919. {
  920. mask |= wt.mask();
  921. break;
  922. }
  923. }
  924. for (L2ArmorType at : L2ArmorType.values())
  925. {
  926. if (at.getName().equals(item))
  927. {
  928. mask |= at.mask();
  929. break;
  930. }
  931. }
  932. }
  933. cond = joinAnd(cond, new ConditionTargetUsesWeaponKind(mask));
  934. }
  935. else if ("npcId".equalsIgnoreCase(a.getNodeName()))
  936. {
  937. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  938. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  939. while (st.hasMoreTokens())
  940. {
  941. String item = st.nextToken().trim();
  942. array.add(Integer.decode(getValue(item, null)));
  943. }
  944. cond = joinAnd(cond, new ConditionTargetNpcId(array));
  945. }
  946. else if ("npcType".equalsIgnoreCase(a.getNodeName()))
  947. {
  948. String values = getValue(a.getNodeValue(), template).trim();
  949. String[] valuesSplit = values.split(",");
  950. InstanceType[] types = new InstanceType[valuesSplit.length];
  951. InstanceType type;
  952. for (int j = 0; j < valuesSplit.length; j++)
  953. {
  954. type = Enum.valueOf(InstanceType.class, valuesSplit[j]);
  955. if (type == null)
  956. {
  957. throw new IllegalArgumentException("Instance type not recognized: " + valuesSplit[j]);
  958. }
  959. types[j] = type;
  960. }
  961. cond = joinAnd(cond, new ConditionTargetNpcType(types));
  962. }
  963. else if ("weight".equalsIgnoreCase(a.getNodeName()))
  964. {
  965. int weight = Integer.decode(getValue(a.getNodeValue(), null));
  966. cond = joinAnd(cond, new ConditionTargetWeight(weight));
  967. }
  968. else if ("invSize".equalsIgnoreCase(a.getNodeName()))
  969. {
  970. int size = Integer.decode(getValue(a.getNodeValue(), null));
  971. cond = joinAnd(cond, new ConditionTargetInvSize(size));
  972. }
  973. }
  974. if (cond == null)
  975. {
  976. _log.severe("Unrecognized <target> condition in " + _file);
  977. }
  978. return cond;
  979. }
  980. protected Condition parseUsingCondition(Node n)
  981. {
  982. Condition cond = null;
  983. NamedNodeMap attrs = n.getAttributes();
  984. for (int i = 0; i < attrs.getLength(); i++)
  985. {
  986. Node a = attrs.item(i);
  987. if ("kind".equalsIgnoreCase(a.getNodeName()))
  988. {
  989. int mask = 0;
  990. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  991. while (st.hasMoreTokens())
  992. {
  993. int old = mask;
  994. String item = st.nextToken().trim();
  995. if (ItemTable._weaponTypes.containsKey(item))
  996. {
  997. mask |= ItemTable._weaponTypes.get(item).mask();
  998. }
  999. if (ItemTable._armorTypes.containsKey(item))
  1000. {
  1001. mask |= ItemTable._armorTypes.get(item).mask();
  1002. }
  1003. if (old == mask)
  1004. {
  1005. _log.info("[parseUsingCondition=\"kind\"] Unknown item type name: " + item);
  1006. }
  1007. }
  1008. cond = joinAnd(cond, new ConditionUsingItemType(mask));
  1009. }
  1010. else if ("skill".equalsIgnoreCase(a.getNodeName()))
  1011. {
  1012. int id = Integer.parseInt(a.getNodeValue());
  1013. cond = joinAnd(cond, new ConditionUsingSkill(id));
  1014. }
  1015. else if ("slotitem".equalsIgnoreCase(a.getNodeName()))
  1016. {
  1017. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ";");
  1018. int id = Integer.parseInt(st.nextToken().trim());
  1019. int slot = Integer.parseInt(st.nextToken().trim());
  1020. int enchant = 0;
  1021. if (st.hasMoreTokens())
  1022. {
  1023. enchant = Integer.parseInt(st.nextToken().trim());
  1024. }
  1025. cond = joinAnd(cond, new ConditionSlotItemId(slot, id, enchant));
  1026. }
  1027. else if ("weaponChange".equalsIgnoreCase(a.getNodeName()))
  1028. {
  1029. boolean val = Boolean.parseBoolean(a.getNodeValue());
  1030. cond = joinAnd(cond, new ConditionChangeWeapon(val));
  1031. }
  1032. }
  1033. if (cond == null)
  1034. {
  1035. _log.severe("Unrecognized <using> condition in " + _file);
  1036. }
  1037. return cond;
  1038. }
  1039. protected Condition parseGameCondition(Node n)
  1040. {
  1041. Condition cond = null;
  1042. NamedNodeMap attrs = n.getAttributes();
  1043. for (int i = 0; i < attrs.getLength(); i++)
  1044. {
  1045. Node a = attrs.item(i);
  1046. if ("skill".equalsIgnoreCase(a.getNodeName()))
  1047. {
  1048. boolean val = Boolean.parseBoolean(a.getNodeValue());
  1049. cond = joinAnd(cond, new ConditionWithSkill(val));
  1050. }
  1051. if ("night".equalsIgnoreCase(a.getNodeName()))
  1052. {
  1053. boolean val = Boolean.parseBoolean(a.getNodeValue());
  1054. cond = joinAnd(cond, new ConditionGameTime(CheckGameTime.NIGHT, val));
  1055. }
  1056. if ("chance".equalsIgnoreCase(a.getNodeName()))
  1057. {
  1058. int val = Integer.decode(getValue(a.getNodeValue(), null));
  1059. cond = joinAnd(cond, new ConditionGameChance(val));
  1060. }
  1061. }
  1062. if (cond == null)
  1063. {
  1064. _log.severe("Unrecognized <game> condition in " + _file);
  1065. }
  1066. return cond;
  1067. }
  1068. protected void parseTable(Node n)
  1069. {
  1070. NamedNodeMap attrs = n.getAttributes();
  1071. String name = attrs.getNamedItem("name").getNodeValue();
  1072. if (name.charAt(0) != '#')
  1073. {
  1074. throw new IllegalArgumentException("Table name must start with #");
  1075. }
  1076. StringTokenizer data = new StringTokenizer(n.getFirstChild().getNodeValue());
  1077. List<String> array = new ArrayList<>(data.countTokens());
  1078. while (data.hasMoreTokens())
  1079. {
  1080. array.add(data.nextToken());
  1081. }
  1082. setTable(name, array.toArray(new String[array.size()]));
  1083. }
  1084. protected void parseBeanSet(Node n, StatsSet set, Integer level)
  1085. {
  1086. String name = n.getAttributes().getNamedItem("name").getNodeValue().trim();
  1087. String value = n.getAttributes().getNamedItem("val").getNodeValue().trim();
  1088. char ch = value.isEmpty() ? ' ' : value.charAt(0);
  1089. if ((ch == '#') || (ch == '-') || Character.isDigit(ch))
  1090. {
  1091. set.set(name, String.valueOf(getValue(value, level)));
  1092. }
  1093. else
  1094. {
  1095. set.set(name, value);
  1096. }
  1097. }
  1098. protected void setExtractableSkillData(StatsSet set, String value)
  1099. {
  1100. set.set("capsuled_items_skill", value);
  1101. }
  1102. protected Lambda getLambda(Node n, Object template)
  1103. {
  1104. Node nval = n.getAttributes().getNamedItem("val");
  1105. if (nval != null)
  1106. {
  1107. String val = nval.getNodeValue();
  1108. if (val.charAt(0) == '#')
  1109. { // table by level
  1110. return new LambdaConst(Double.parseDouble(getTableValue(val)));
  1111. }
  1112. else if (val.charAt(0) == '$')
  1113. {
  1114. if (val.equalsIgnoreCase("$player_level"))
  1115. {
  1116. return new LambdaStats(LambdaStats.StatsType.PLAYER_LEVEL);
  1117. }
  1118. if (val.equalsIgnoreCase("$target_level"))
  1119. {
  1120. return new LambdaStats(LambdaStats.StatsType.TARGET_LEVEL);
  1121. }
  1122. if (val.equalsIgnoreCase("$player_max_hp"))
  1123. {
  1124. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_HP);
  1125. }
  1126. if (val.equalsIgnoreCase("$player_max_mp"))
  1127. {
  1128. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_MP);
  1129. }
  1130. // try to find value out of item fields
  1131. StatsSet set = getStatsSet();
  1132. String field = set.getString(val.substring(1));
  1133. if (field != null)
  1134. {
  1135. return new LambdaConst(Double.parseDouble(getValue(field, template)));
  1136. }
  1137. // failed
  1138. throw new IllegalArgumentException("Unknown value " + val);
  1139. }
  1140. else
  1141. {
  1142. return new LambdaConst(Double.parseDouble(val));
  1143. }
  1144. }
  1145. LambdaCalc calc = new LambdaCalc();
  1146. n = n.getFirstChild();
  1147. while ((n != null) && (n.getNodeType() != Node.ELEMENT_NODE))
  1148. {
  1149. n = n.getNextSibling();
  1150. }
  1151. if ((n == null) || !"val".equals(n.getNodeName()))
  1152. {
  1153. throw new IllegalArgumentException("Value not specified");
  1154. }
  1155. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  1156. {
  1157. if (n.getNodeType() != Node.ELEMENT_NODE)
  1158. {
  1159. continue;
  1160. }
  1161. attachLambdaFunc(n, template, calc);
  1162. }
  1163. return calc;
  1164. }
  1165. protected String getValue(String value, Object template)
  1166. {
  1167. // is it a table?
  1168. if (value.charAt(0) == '#')
  1169. {
  1170. if (template instanceof L2Skill)
  1171. {
  1172. return getTableValue(value);
  1173. }
  1174. else if (template instanceof Integer)
  1175. {
  1176. return getTableValue(value, ((Integer) template).intValue());
  1177. }
  1178. else
  1179. {
  1180. throw new IllegalStateException();
  1181. }
  1182. }
  1183. return value;
  1184. }
  1185. protected Condition joinAnd(Condition cond, Condition c)
  1186. {
  1187. if (cond == null)
  1188. {
  1189. return c;
  1190. }
  1191. if (cond instanceof ConditionLogicAnd)
  1192. {
  1193. ((ConditionLogicAnd) cond).add(c);
  1194. return cond;
  1195. }
  1196. ConditionLogicAnd and = new ConditionLogicAnd();
  1197. and.add(cond);
  1198. and.add(c);
  1199. return and;
  1200. }
  1201. }