DocumentBase.java 44 KB

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