DocumentBase.java 46 KB

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