DocumentBase.java 44 KB

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