DocumentBase.java 43 KB

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