DocumentBase.java 42 KB

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