DocumentBase.java 42 KB

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