DocumentBase.java 41 KB

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