DocumentBase.java 44 KB

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