DocumentBase.java 45 KB

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