DocumentBase.java 44 KB

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