DocumentBase.java 44 KB

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