DocumentBase.java 45 KB

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