2
0

DocumentBase.java 46 KB

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