2
0

DocumentBase.java 45 KB

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