DocumentBase.java 41 KB

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