DocumentBase.java 42 KB

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