DocumentBase.java 41 KB

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