DocumentBase.java 45 KB

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