DocumentBase.java 44 KB

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