DocumentBase.java 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  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.Skill;
  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. switch (n.getNodeName().toLowerCase())
  210. {
  211. case "add":
  212. {
  213. attachFunc(n, template, "Add", condition);
  214. break;
  215. }
  216. case "sub":
  217. {
  218. attachFunc(n, template, "Sub", condition);
  219. break;
  220. }
  221. case "mul":
  222. {
  223. attachFunc(n, template, "Mul", condition);
  224. break;
  225. }
  226. case "basemul":
  227. {
  228. attachFunc(n, template, "BaseMul", condition);
  229. break;
  230. }
  231. case "div":
  232. {
  233. attachFunc(n, template, "Div", condition);
  234. break;
  235. }
  236. case "set":
  237. {
  238. attachFunc(n, template, "Set", condition);
  239. break;
  240. }
  241. case "share":
  242. {
  243. attachFunc(n, template, "Share", condition);
  244. break;
  245. }
  246. case "enchant":
  247. {
  248. attachFunc(n, template, "Enchant", condition);
  249. break;
  250. }
  251. case "enchanthp":
  252. {
  253. attachFunc(n, template, "EnchantHp", condition);
  254. break;
  255. }
  256. case "effect":
  257. {
  258. if (template instanceof AbstractEffect)
  259. {
  260. throw new RuntimeException("Nested effects");
  261. }
  262. attachEffect(n, template, condition, effectScope);
  263. break;
  264. }
  265. }
  266. }
  267. }
  268. protected void attachFunc(Node n, Object template, String name, Condition attachCond)
  269. {
  270. Stats stat = Stats.valueOfXml(n.getAttributes().getNamedItem("stat").getNodeValue());
  271. String order = n.getAttributes().getNamedItem("order").getNodeValue();
  272. Lambda lambda = getLambda(n, template);
  273. int ord = Integer.decode(getValue(order, template));
  274. Condition applayCond = parseCondition(n.getFirstChild(), template);
  275. FuncTemplate ft = new FuncTemplate(attachCond, applayCond, name, stat, ord, lambda);
  276. if (template instanceof L2Item)
  277. {
  278. ((L2Item) template).attach(ft);
  279. }
  280. else if (template instanceof Skill)
  281. {
  282. ((Skill) template).attach(ft);
  283. }
  284. else if (template instanceof AbstractEffect)
  285. {
  286. ((AbstractEffect) template).attach(ft);
  287. }
  288. }
  289. protected void attachLambdaFunc(Node n, Object template, LambdaCalc calc)
  290. {
  291. String name = n.getNodeName();
  292. final StringBuilder sb = new StringBuilder(name);
  293. sb.setCharAt(0, Character.toUpperCase(name.charAt(0)));
  294. name = sb.toString();
  295. Lambda lambda = getLambda(n, template);
  296. FuncTemplate ft = new FuncTemplate(null, null, name, null, calc.funcs.length, lambda);
  297. calc.addFunc(ft.getFunc(new Env(), calc));
  298. }
  299. protected void attachEffect(Node n, Object template, Condition attachCond)
  300. {
  301. attachEffect(n, template, attachCond, null);
  302. }
  303. protected void attachEffect(Node n, Object template, Condition attachCond, EffectScope effectScope)
  304. {
  305. final NamedNodeMap attrs = n.getAttributes();
  306. final StatsSet set = new StatsSet();
  307. for (int i = 0; i < attrs.getLength(); i++)
  308. {
  309. Node att = attrs.item(i);
  310. set.set(att.getNodeName(), getValue(att.getNodeValue(), template));
  311. }
  312. final StatsSet parameters = parseParameters(n.getFirstChild(), template);
  313. final Condition applayCond = parseCondition(n.getFirstChild(), template);
  314. if (template instanceof IIdentifiable)
  315. {
  316. set.set("id", ((IIdentifiable) template).getId());
  317. }
  318. final AbstractEffect effect = AbstractEffect.createEffect(attachCond, applayCond, set, parameters);
  319. parseTemplate(n, effect);
  320. if (template instanceof L2Item)
  321. {
  322. ((L2Item) template).attach(effect);
  323. }
  324. else if (template instanceof Skill)
  325. {
  326. final Skill skill = (Skill) template;
  327. if (effectScope != null)
  328. {
  329. skill.addEffect(effectScope, effect);
  330. }
  331. else if (skill.isPassive())
  332. {
  333. skill.addEffect(EffectScope.PASSIVE, effect);
  334. }
  335. else
  336. {
  337. skill.addEffect(EffectScope.GENERAL, effect);
  338. }
  339. }
  340. }
  341. /**
  342. * Parse effect's parameters.
  343. * @param n the node to start the parsing
  344. * @param template the effect template
  345. * @return the list of parameters if any, {@code null} otherwise
  346. */
  347. private StatsSet parseParameters(Node n, Object template)
  348. {
  349. StatsSet parameters = null;
  350. while ((n != null))
  351. {
  352. // Parse all parameters.
  353. if ((n.getNodeType() == Node.ELEMENT_NODE) && "param".equals(n.getNodeName()))
  354. {
  355. if (parameters == null)
  356. {
  357. parameters = new StatsSet();
  358. }
  359. NamedNodeMap params = n.getAttributes();
  360. for (int i = 0; i < params.getLength(); i++)
  361. {
  362. Node att = params.item(i);
  363. parameters.set(att.getNodeName(), getValue(att.getNodeValue(), template));
  364. }
  365. }
  366. n = n.getNextSibling();
  367. }
  368. return parameters == null ? StatsSet.EMPTY_STATSET : parameters;
  369. }
  370. protected Condition parseCondition(Node n, Object template)
  371. {
  372. while ((n != null) && (n.getNodeType() != Node.ELEMENT_NODE))
  373. {
  374. n = n.getNextSibling();
  375. }
  376. Condition condition = null;
  377. if (n != null)
  378. {
  379. switch (n.getNodeName().toLowerCase())
  380. {
  381. case "and":
  382. {
  383. condition = parseLogicAnd(n, template);
  384. break;
  385. }
  386. case "or":
  387. {
  388. condition = parseLogicOr(n, template);
  389. break;
  390. }
  391. case "not":
  392. {
  393. condition = parseLogicNot(n, template);
  394. break;
  395. }
  396. case "player":
  397. {
  398. condition = parsePlayerCondition(n, template);
  399. break;
  400. }
  401. case "target":
  402. {
  403. condition = parseTargetCondition(n, template);
  404. break;
  405. }
  406. case "using":
  407. {
  408. condition = parseUsingCondition(n);
  409. break;
  410. }
  411. case "game":
  412. {
  413. condition = parseGameCondition(n);
  414. break;
  415. }
  416. }
  417. }
  418. return condition;
  419. }
  420. protected Condition parseLogicAnd(Node n, Object template)
  421. {
  422. ConditionLogicAnd cond = new ConditionLogicAnd();
  423. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  424. {
  425. if (n.getNodeType() == Node.ELEMENT_NODE)
  426. {
  427. cond.add(parseCondition(n, template));
  428. }
  429. }
  430. if ((cond.conditions == null) || (cond.conditions.length == 0))
  431. {
  432. _log.severe("Empty <and> condition in " + _file);
  433. }
  434. return cond;
  435. }
  436. protected Condition parseLogicOr(Node n, Object template)
  437. {
  438. ConditionLogicOr cond = new ConditionLogicOr();
  439. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  440. {
  441. if (n.getNodeType() == Node.ELEMENT_NODE)
  442. {
  443. cond.add(parseCondition(n, template));
  444. }
  445. }
  446. if ((cond.conditions == null) || (cond.conditions.length == 0))
  447. {
  448. _log.severe("Empty <or> condition in " + _file);
  449. }
  450. return cond;
  451. }
  452. protected Condition parseLogicNot(Node n, Object template)
  453. {
  454. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  455. {
  456. if (n.getNodeType() == Node.ELEMENT_NODE)
  457. {
  458. return new ConditionLogicNot(parseCondition(n, template));
  459. }
  460. }
  461. _log.severe("Empty <not> condition in " + _file);
  462. return null;
  463. }
  464. protected Condition parsePlayerCondition(Node n, Object template)
  465. {
  466. Condition cond = null;
  467. NamedNodeMap attrs = n.getAttributes();
  468. for (int i = 0; i < attrs.getLength(); i++)
  469. {
  470. Node a = attrs.item(i);
  471. switch (a.getNodeName().toLowerCase())
  472. {
  473. case "races":
  474. {
  475. final String[] racesVal = a.getNodeValue().split(",");
  476. final PcRace[] races = new PcRace[racesVal.length];
  477. for (int r = 0; r < racesVal.length; r++)
  478. {
  479. if (racesVal[r] != null)
  480. {
  481. races[r] = PcRace.valueOf(racesVal[r]);
  482. }
  483. }
  484. cond = joinAnd(cond, new ConditionPlayerRace(races));
  485. break;
  486. }
  487. case "level":
  488. {
  489. int lvl = Integer.decode(getValue(a.getNodeValue(), template));
  490. cond = joinAnd(cond, new ConditionPlayerLevel(lvl));
  491. break;
  492. }
  493. case "levelrange":
  494. {
  495. String[] range = getValue(a.getNodeValue(), template).split(";");
  496. if (range.length == 2)
  497. {
  498. int[] lvlRange = new int[2];
  499. lvlRange[0] = Integer.decode(getValue(a.getNodeValue(), template).split(";")[0]);
  500. lvlRange[1] = Integer.decode(getValue(a.getNodeValue(), template).split(";")[1]);
  501. cond = joinAnd(cond, new ConditionPlayerLevelRange(lvlRange));
  502. }
  503. break;
  504. }
  505. case "resting":
  506. {
  507. boolean val = Boolean.parseBoolean(a.getNodeValue());
  508. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.RESTING, val));
  509. break;
  510. }
  511. case "flying":
  512. {
  513. boolean val = Boolean.parseBoolean(a.getNodeValue());
  514. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.FLYING, val));
  515. break;
  516. }
  517. case "moving":
  518. {
  519. boolean val = Boolean.parseBoolean(a.getNodeValue());
  520. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.MOVING, val));
  521. break;
  522. }
  523. case "running":
  524. {
  525. boolean val = Boolean.parseBoolean(a.getNodeValue());
  526. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.RUNNING, val));
  527. break;
  528. }
  529. case "standing":
  530. {
  531. boolean val = Boolean.parseBoolean(a.getNodeValue());
  532. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.STANDING, val));
  533. break;
  534. }
  535. case "behind":
  536. {
  537. boolean val = Boolean.parseBoolean(a.getNodeValue());
  538. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.BEHIND, val));
  539. break;
  540. }
  541. case "front":
  542. {
  543. boolean val = Boolean.parseBoolean(a.getNodeValue());
  544. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.FRONT, val));
  545. break;
  546. }
  547. case "chaotic":
  548. {
  549. boolean val = Boolean.parseBoolean(a.getNodeValue());
  550. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.CHAOTIC, val));
  551. break;
  552. }
  553. case "olympiad":
  554. {
  555. boolean val = Boolean.parseBoolean(a.getNodeValue());
  556. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.OLYMPIAD, val));
  557. break;
  558. }
  559. case "ishero":
  560. {
  561. boolean val = Boolean.parseBoolean(a.getNodeValue());
  562. cond = joinAnd(cond, new ConditionPlayerIsHero(val));
  563. break;
  564. }
  565. case "transformationid":
  566. {
  567. int id = Integer.parseInt(a.getNodeValue());
  568. cond = joinAnd(cond, new ConditionPlayerTransformationId(id));
  569. break;
  570. }
  571. case "hp":
  572. {
  573. int hp = Integer.decode(getValue(a.getNodeValue(), null));
  574. cond = joinAnd(cond, new ConditionPlayerHp(hp));
  575. break;
  576. }
  577. case "mp":
  578. {
  579. int hp = Integer.decode(getValue(a.getNodeValue(), null));
  580. cond = joinAnd(cond, new ConditionPlayerMp(hp));
  581. break;
  582. }
  583. case "cp":
  584. {
  585. int cp = Integer.decode(getValue(a.getNodeValue(), null));
  586. cond = joinAnd(cond, new ConditionPlayerCp(cp));
  587. break;
  588. }
  589. case "grade":
  590. {
  591. int expIndex = Integer.decode(getValue(a.getNodeValue(), template));
  592. cond = joinAnd(cond, new ConditionPlayerGrade(expIndex));
  593. break;
  594. }
  595. case "pkcount":
  596. {
  597. int expIndex = Integer.decode(getValue(a.getNodeValue(), template));
  598. cond = joinAnd(cond, new ConditionPlayerPkCount(expIndex));
  599. break;
  600. }
  601. case "siegezone":
  602. {
  603. int value = Integer.decode(getValue(a.getNodeValue(), null));
  604. cond = joinAnd(cond, new ConditionSiegeZone(value, true));
  605. break;
  606. }
  607. case "siegeside":
  608. {
  609. int value = Integer.decode(getValue(a.getNodeValue(), null));
  610. cond = joinAnd(cond, new ConditionPlayerSiegeSide(value));
  611. break;
  612. }
  613. case "charges":
  614. {
  615. int value = Integer.decode(getValue(a.getNodeValue(), template));
  616. cond = joinAnd(cond, new ConditionPlayerCharges(value));
  617. break;
  618. }
  619. case "souls":
  620. {
  621. int value = Integer.decode(getValue(a.getNodeValue(), template));
  622. cond = joinAnd(cond, new ConditionPlayerSouls(value));
  623. break;
  624. }
  625. case "weight":
  626. {
  627. int weight = Integer.decode(getValue(a.getNodeValue(), null));
  628. cond = joinAnd(cond, new ConditionPlayerWeight(weight));
  629. break;
  630. }
  631. case "invsize":
  632. {
  633. int size = Integer.decode(getValue(a.getNodeValue(), null));
  634. cond = joinAnd(cond, new ConditionPlayerInvSize(size));
  635. break;
  636. }
  637. case "isclanleader":
  638. {
  639. boolean val = Boolean.parseBoolean(a.getNodeValue());
  640. cond = joinAnd(cond, new ConditionPlayerIsClanLeader(val));
  641. break;
  642. }
  643. case "ontvtevent":
  644. {
  645. boolean val = Boolean.parseBoolean(a.getNodeValue());
  646. cond = joinAnd(cond, new ConditionPlayerTvTEvent(val));
  647. break;
  648. }
  649. case "pledgeclass":
  650. {
  651. int pledgeClass = Integer.decode(getValue(a.getNodeValue(), null));
  652. cond = joinAnd(cond, new ConditionPlayerPledgeClass(pledgeClass));
  653. break;
  654. }
  655. case "clanhall":
  656. {
  657. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  658. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  659. while (st.hasMoreTokens())
  660. {
  661. String item = st.nextToken().trim();
  662. array.add(Integer.decode(getValue(item, null)));
  663. }
  664. cond = joinAnd(cond, new ConditionPlayerHasClanHall(array));
  665. break;
  666. }
  667. case "fort":
  668. {
  669. int fort = Integer.decode(getValue(a.getNodeValue(), null));
  670. cond = joinAnd(cond, new ConditionPlayerHasFort(fort));
  671. break;
  672. }
  673. case "castle":
  674. {
  675. int castle = Integer.decode(getValue(a.getNodeValue(), null));
  676. cond = joinAnd(cond, new ConditionPlayerHasCastle(castle));
  677. break;
  678. }
  679. case "sex":
  680. {
  681. int sex = Integer.decode(getValue(a.getNodeValue(), null));
  682. cond = joinAnd(cond, new ConditionPlayerSex(sex));
  683. break;
  684. }
  685. case "flymounted":
  686. {
  687. boolean val = Boolean.parseBoolean(a.getNodeValue());
  688. cond = joinAnd(cond, new ConditionPlayerFlyMounted(val));
  689. break;
  690. }
  691. case "vehiclemounted":
  692. {
  693. boolean val = Boolean.parseBoolean(a.getNodeValue());
  694. cond = joinAnd(cond, new ConditionPlayerVehicleMounted(val));
  695. break;
  696. }
  697. case "landingzone":
  698. {
  699. boolean val = Boolean.parseBoolean(a.getNodeValue());
  700. cond = joinAnd(cond, new ConditionPlayerLandingZone(val));
  701. break;
  702. }
  703. case "active_effect_id":
  704. {
  705. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  706. cond = joinAnd(cond, new ConditionPlayerActiveEffectId(effect_id));
  707. break;
  708. }
  709. case "active_effect_id_lvl":
  710. {
  711. String val = getValue(a.getNodeValue(), template);
  712. int effect_id = Integer.decode(getValue(val.split(",")[0], template));
  713. int effect_lvl = Integer.decode(getValue(val.split(",")[1], template));
  714. cond = joinAnd(cond, new ConditionPlayerActiveEffectId(effect_id, effect_lvl));
  715. break;
  716. }
  717. case "active_skill_id":
  718. {
  719. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  720. cond = joinAnd(cond, new ConditionPlayerActiveSkillId(skill_id));
  721. break;
  722. }
  723. case "active_skill_id_lvl":
  724. {
  725. String val = getValue(a.getNodeValue(), template);
  726. int skill_id = Integer.decode(getValue(val.split(",")[0], template));
  727. int skill_lvl = Integer.decode(getValue(val.split(",")[1], template));
  728. cond = joinAnd(cond, new ConditionPlayerActiveSkillId(skill_id, skill_lvl));
  729. break;
  730. }
  731. case "class_id_restriction":
  732. {
  733. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  734. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  735. while (st.hasMoreTokens())
  736. {
  737. String item = st.nextToken().trim();
  738. array.add(Integer.decode(getValue(item, null)));
  739. }
  740. cond = joinAnd(cond, new ConditionPlayerClassIdRestriction(array));
  741. break;
  742. }
  743. case "subclass":
  744. {
  745. boolean val = Boolean.parseBoolean(a.getNodeValue());
  746. cond = joinAnd(cond, new ConditionPlayerSubclass(val));
  747. break;
  748. }
  749. case "instanceid":
  750. {
  751. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  752. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  753. while (st.hasMoreTokens())
  754. {
  755. String item = st.nextToken().trim();
  756. array.add(Integer.decode(getValue(item, null)));
  757. }
  758. cond = joinAnd(cond, new ConditionPlayerInstanceId(array));
  759. break;
  760. }
  761. case "agathionid":
  762. {
  763. int agathionId = Integer.decode(a.getNodeValue());
  764. cond = joinAnd(cond, new ConditionPlayerAgathionId(agathionId));
  765. break;
  766. }
  767. case "cloakstatus":
  768. {
  769. int val = Integer.parseInt(a.getNodeValue());
  770. cond = joinAnd(cond, new ConditionPlayerCloakStatus(val));
  771. break;
  772. }
  773. case "haspet":
  774. {
  775. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  776. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  777. while (st.hasMoreTokens())
  778. {
  779. String item = st.nextToken().trim();
  780. array.add(Integer.decode(getValue(item, null)));
  781. }
  782. cond = joinAnd(cond, new ConditionPlayerHasPet(array));
  783. break;
  784. }
  785. case "servitornpcid":
  786. {
  787. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  788. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  789. while (st.hasMoreTokens())
  790. {
  791. String item = st.nextToken().trim();
  792. array.add(Integer.decode(getValue(item, null)));
  793. }
  794. cond = joinAnd(cond, new ConditionPlayerServitorNpcId(array));
  795. break;
  796. }
  797. case "npcidradius":
  798. {
  799. final StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  800. if (st.countTokens() == 3)
  801. {
  802. final String[] ids = st.nextToken().split(";");
  803. final int[] npcIds = new int[ids.length];
  804. for (int index = 0; index < ids.length; index++)
  805. {
  806. npcIds[index] = Integer.parseInt(getValue(ids[index], template));
  807. }
  808. final int radius = Integer.parseInt(st.nextToken());
  809. final boolean val = Boolean.parseBoolean(st.nextToken());
  810. cond = joinAnd(cond, new ConditionPlayerRangeFromNpc(npcIds, radius, val));
  811. }
  812. break;
  813. }
  814. case "callpc":
  815. {
  816. cond = joinAnd(cond, new ConditionPlayerCallPc(Boolean.parseBoolean(a.getNodeValue())));
  817. break;
  818. }
  819. case "cancreatebase":
  820. {
  821. cond = joinAnd(cond, new ConditionPlayerCanCreateBase(Boolean.parseBoolean(a.getNodeValue())));
  822. break;
  823. }
  824. case "cancreateoutpost":
  825. {
  826. cond = joinAnd(cond, new ConditionPlayerCanCreateOutpost(Boolean.parseBoolean(a.getNodeValue())));
  827. break;
  828. }
  829. case "canescape":
  830. {
  831. cond = joinAnd(cond, new ConditionPlayerCanEscape(Boolean.parseBoolean(a.getNodeValue())));
  832. break;
  833. }
  834. case "canrefuelairship":
  835. {
  836. cond = joinAnd(cond, new ConditionPlayerCanRefuelAirship(Integer.parseInt(a.getNodeValue())));
  837. break;
  838. }
  839. case "cansummon":
  840. {
  841. cond = joinAnd(cond, new ConditionPlayerCanSummon(Boolean.parseBoolean(a.getNodeValue())));
  842. break;
  843. }
  844. case "cansummonsiegegolem":
  845. {
  846. cond = joinAnd(cond, new ConditionPlayerCanSummonSiegeGolem(Boolean.parseBoolean(a.getNodeValue())));
  847. break;
  848. }
  849. case "cansweep":
  850. {
  851. cond = joinAnd(cond, new ConditionPlayerCanSweep(Boolean.parseBoolean(a.getNodeValue())));
  852. break;
  853. }
  854. case "cantakecastle":
  855. {
  856. cond = joinAnd(cond, new ConditionPlayerCanTakeCastle(Boolean.parseBoolean(a.getNodeValue())));
  857. break;
  858. }
  859. case "cantakefort":
  860. {
  861. cond = joinAnd(cond, new ConditionPlayerCanTakeFort(Boolean.parseBoolean(a.getNodeValue())));
  862. break;
  863. }
  864. case "cantransform":
  865. {
  866. cond = joinAnd(cond, new ConditionPlayerCanTransform(Boolean.parseBoolean(a.getNodeValue())));
  867. break;
  868. }
  869. case "canuntransform":
  870. {
  871. cond = joinAnd(cond, new ConditionPlayerCanUntransform(Boolean.parseBoolean(a.getNodeValue())));
  872. break;
  873. }
  874. case "insidezoneid":
  875. {
  876. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  877. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  878. while (st.hasMoreTokens())
  879. {
  880. String item = st.nextToken().trim();
  881. array.add(Integer.decode(getValue(item, null)));
  882. }
  883. cond = joinAnd(cond, new ConditionPlayerInsideZoneId(array));
  884. break;
  885. }
  886. }
  887. }
  888. if (cond == null)
  889. {
  890. _log.severe("Unrecognized <player> condition in " + _file);
  891. }
  892. return cond;
  893. }
  894. protected Condition parseTargetCondition(Node n, Object template)
  895. {
  896. Condition cond = null;
  897. NamedNodeMap attrs = n.getAttributes();
  898. for (int i = 0; i < attrs.getLength(); i++)
  899. {
  900. Node a = attrs.item(i);
  901. switch (a.getNodeName().toLowerCase())
  902. {
  903. case "aggro":
  904. {
  905. boolean val = Boolean.parseBoolean(a.getNodeValue());
  906. cond = joinAnd(cond, new ConditionTargetAggro(val));
  907. break;
  908. }
  909. case "siegezone":
  910. {
  911. int value = Integer.decode(getValue(a.getNodeValue(), null));
  912. cond = joinAnd(cond, new ConditionSiegeZone(value, false));
  913. break;
  914. }
  915. case "level":
  916. {
  917. int lvl = Integer.decode(getValue(a.getNodeValue(), template));
  918. cond = joinAnd(cond, new ConditionTargetLevel(lvl));
  919. break;
  920. }
  921. case "levelrange":
  922. {
  923. String[] range = getValue(a.getNodeValue(), template).split(";");
  924. if (range.length == 2)
  925. {
  926. int[] lvlRange = new int[2];
  927. lvlRange[0] = Integer.decode(getValue(a.getNodeValue(), template).split(";")[0]);
  928. lvlRange[1] = Integer.decode(getValue(a.getNodeValue(), template).split(";")[1]);
  929. cond = joinAnd(cond, new ConditionTargetLevelRange(lvlRange));
  930. }
  931. break;
  932. }
  933. case "mypartyexceptme":
  934. {
  935. cond = joinAnd(cond, new ConditionTargetMyPartyExceptMe(Boolean.parseBoolean(a.getNodeValue())));
  936. break;
  937. }
  938. case "playable":
  939. {
  940. cond = joinAnd(cond, new ConditionTargetPlayable());
  941. break;
  942. }
  943. case "class_id_restriction":
  944. {
  945. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  946. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  947. while (st.hasMoreTokens())
  948. {
  949. String item = st.nextToken().trim();
  950. array.add(Integer.decode(getValue(item, null)));
  951. }
  952. cond = joinAnd(cond, new ConditionTargetClassIdRestriction(array));
  953. break;
  954. }
  955. case "active_effect_id":
  956. {
  957. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  958. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id));
  959. break;
  960. }
  961. case "active_effect_id_lvl":
  962. {
  963. String val = getValue(a.getNodeValue(), template);
  964. int effect_id = Integer.decode(getValue(val.split(",")[0], template));
  965. int effect_lvl = Integer.decode(getValue(val.split(",")[1], template));
  966. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id, effect_lvl));
  967. break;
  968. }
  969. case "active_skill_id":
  970. {
  971. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  972. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id));
  973. break;
  974. }
  975. case "active_skill_id_lvl":
  976. {
  977. String val = getValue(a.getNodeValue(), template);
  978. int skill_id = Integer.decode(getValue(val.split(",")[0], template));
  979. int skill_lvl = Integer.decode(getValue(val.split(",")[1], template));
  980. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id, skill_lvl));
  981. break;
  982. }
  983. case "abnormal":
  984. {
  985. int abnormalId = Integer.decode(getValue(a.getNodeValue(), template));
  986. cond = joinAnd(cond, new ConditionTargetAbnormal(abnormalId));
  987. break;
  988. }
  989. case "mindistance":
  990. {
  991. int distance = Integer.decode(getValue(a.getNodeValue(), null));
  992. cond = joinAnd(cond, new ConditionMinDistance(distance * distance));
  993. break;
  994. }
  995. case "npcrace":
  996. {
  997. // used for npc race
  998. final String[] values = a.getNodeValue().split(",");
  999. final Set<NpcRace> array = new HashSet<>(values.length);
  1000. for (String value : values)
  1001. {
  1002. array.add(NpcRace.valueOf(getValue(value, null)));
  1003. }
  1004. cond = joinAnd(cond, new ConditionTargetNpcRace(array));
  1005. break;
  1006. }
  1007. case "races":
  1008. {
  1009. // used for pc race
  1010. final String[] racesVal = a.getNodeValue().split(",");
  1011. final PcRace[] races = new PcRace[racesVal.length];
  1012. for (int r = 0; r < racesVal.length; r++)
  1013. {
  1014. if (racesVal[r] != null)
  1015. {
  1016. races[r] = PcRace.valueOf(racesVal[r]);
  1017. }
  1018. }
  1019. cond = joinAnd(cond, new ConditionTargetRace(races));
  1020. break;
  1021. }
  1022. case "using":
  1023. {
  1024. int mask = 0;
  1025. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  1026. while (st.hasMoreTokens())
  1027. {
  1028. String item = st.nextToken().trim();
  1029. for (L2WeaponType wt : L2WeaponType.values())
  1030. {
  1031. if (wt.getName().equals(item))
  1032. {
  1033. mask |= wt.mask();
  1034. break;
  1035. }
  1036. }
  1037. for (L2ArmorType at : L2ArmorType.values())
  1038. {
  1039. if (at.getName().equals(item))
  1040. {
  1041. mask |= at.mask();
  1042. break;
  1043. }
  1044. }
  1045. }
  1046. cond = joinAnd(cond, new ConditionTargetUsesWeaponKind(mask));
  1047. break;
  1048. }
  1049. case "npcid":
  1050. {
  1051. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  1052. ArrayList<Integer> array = new ArrayList<>(st.countTokens());
  1053. while (st.hasMoreTokens())
  1054. {
  1055. String item = st.nextToken().trim();
  1056. array.add(Integer.decode(getValue(item, null)));
  1057. }
  1058. cond = joinAnd(cond, new ConditionTargetNpcId(array));
  1059. break;
  1060. }
  1061. case "npctype":
  1062. {
  1063. String values = getValue(a.getNodeValue(), template).trim();
  1064. String[] valuesSplit = values.split(",");
  1065. InstanceType[] types = new InstanceType[valuesSplit.length];
  1066. InstanceType type;
  1067. for (int j = 0; j < valuesSplit.length; j++)
  1068. {
  1069. type = Enum.valueOf(InstanceType.class, valuesSplit[j]);
  1070. if (type == null)
  1071. {
  1072. throw new IllegalArgumentException("Instance type not recognized: " + valuesSplit[j]);
  1073. }
  1074. types[j] = type;
  1075. }
  1076. cond = joinAnd(cond, new ConditionTargetNpcType(types));
  1077. break;
  1078. }
  1079. case "weight":
  1080. {
  1081. int weight = Integer.decode(getValue(a.getNodeValue(), null));
  1082. cond = joinAnd(cond, new ConditionTargetWeight(weight));
  1083. break;
  1084. }
  1085. case "invsize":
  1086. {
  1087. int size = Integer.decode(getValue(a.getNodeValue(), null));
  1088. cond = joinAnd(cond, new ConditionTargetInvSize(size));
  1089. break;
  1090. }
  1091. }
  1092. }
  1093. if (cond == null)
  1094. {
  1095. _log.severe("Unrecognized <target> condition in " + _file);
  1096. }
  1097. return cond;
  1098. }
  1099. protected Condition parseUsingCondition(Node n)
  1100. {
  1101. Condition cond = null;
  1102. NamedNodeMap attrs = n.getAttributes();
  1103. for (int i = 0; i < attrs.getLength(); i++)
  1104. {
  1105. Node a = attrs.item(i);
  1106. switch (a.getNodeName().toLowerCase())
  1107. {
  1108. case "kind":
  1109. {
  1110. int mask = 0;
  1111. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  1112. while (st.hasMoreTokens())
  1113. {
  1114. int old = mask;
  1115. String item = st.nextToken().trim();
  1116. if (ItemTable._weaponTypes.containsKey(item))
  1117. {
  1118. mask |= ItemTable._weaponTypes.get(item).mask();
  1119. }
  1120. if (ItemTable._armorTypes.containsKey(item))
  1121. {
  1122. mask |= ItemTable._armorTypes.get(item).mask();
  1123. }
  1124. if (old == mask)
  1125. {
  1126. _log.info("[parseUsingCondition=\"kind\"] Unknown item type name: " + item);
  1127. }
  1128. }
  1129. cond = joinAnd(cond, new ConditionUsingItemType(mask));
  1130. break;
  1131. }
  1132. case "skill":
  1133. {
  1134. int id = Integer.parseInt(a.getNodeValue());
  1135. cond = joinAnd(cond, new ConditionUsingSkill(id));
  1136. break;
  1137. }
  1138. case "slotitem":
  1139. {
  1140. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ";");
  1141. int id = Integer.parseInt(st.nextToken().trim());
  1142. int slot = Integer.parseInt(st.nextToken().trim());
  1143. int enchant = 0;
  1144. if (st.hasMoreTokens())
  1145. {
  1146. enchant = Integer.parseInt(st.nextToken().trim());
  1147. }
  1148. cond = joinAnd(cond, new ConditionSlotItemId(slot, id, enchant));
  1149. break;
  1150. }
  1151. case "weaponchange":
  1152. {
  1153. boolean val = Boolean.parseBoolean(a.getNodeValue());
  1154. cond = joinAnd(cond, new ConditionChangeWeapon(val));
  1155. break;
  1156. }
  1157. }
  1158. }
  1159. if (cond == null)
  1160. {
  1161. _log.severe("Unrecognized <using> condition in " + _file);
  1162. }
  1163. return cond;
  1164. }
  1165. protected Condition parseGameCondition(Node n)
  1166. {
  1167. Condition cond = null;
  1168. NamedNodeMap attrs = n.getAttributes();
  1169. for (int i = 0; i < attrs.getLength(); i++)
  1170. {
  1171. Node a = attrs.item(i);
  1172. if ("skill".equalsIgnoreCase(a.getNodeName()))
  1173. {
  1174. boolean val = Boolean.parseBoolean(a.getNodeValue());
  1175. cond = joinAnd(cond, new ConditionWithSkill(val));
  1176. }
  1177. if ("night".equalsIgnoreCase(a.getNodeName()))
  1178. {
  1179. boolean val = Boolean.parseBoolean(a.getNodeValue());
  1180. cond = joinAnd(cond, new ConditionGameTime(CheckGameTime.NIGHT, val));
  1181. }
  1182. if ("chance".equalsIgnoreCase(a.getNodeName()))
  1183. {
  1184. int val = Integer.decode(getValue(a.getNodeValue(), null));
  1185. cond = joinAnd(cond, new ConditionGameChance(val));
  1186. }
  1187. }
  1188. if (cond == null)
  1189. {
  1190. _log.severe("Unrecognized <game> condition in " + _file);
  1191. }
  1192. return cond;
  1193. }
  1194. protected void parseTable(Node n)
  1195. {
  1196. NamedNodeMap attrs = n.getAttributes();
  1197. String name = attrs.getNamedItem("name").getNodeValue();
  1198. if (name.charAt(0) != '#')
  1199. {
  1200. throw new IllegalArgumentException("Table name must start with #");
  1201. }
  1202. StringTokenizer data = new StringTokenizer(n.getFirstChild().getNodeValue());
  1203. List<String> array = new ArrayList<>(data.countTokens());
  1204. while (data.hasMoreTokens())
  1205. {
  1206. array.add(data.nextToken());
  1207. }
  1208. setTable(name, array.toArray(new String[array.size()]));
  1209. }
  1210. protected void parseBeanSet(Node n, StatsSet set, Integer level)
  1211. {
  1212. String name = n.getAttributes().getNamedItem("name").getNodeValue().trim();
  1213. String value = n.getAttributes().getNamedItem("val").getNodeValue().trim();
  1214. char ch = value.isEmpty() ? ' ' : value.charAt(0);
  1215. if ((ch == '#') || (ch == '-') || Character.isDigit(ch))
  1216. {
  1217. set.set(name, String.valueOf(getValue(value, level)));
  1218. }
  1219. else
  1220. {
  1221. set.set(name, value);
  1222. }
  1223. }
  1224. protected void setExtractableSkillData(StatsSet set, String value)
  1225. {
  1226. set.set("capsuled_items_skill", value);
  1227. }
  1228. protected Lambda getLambda(Node n, Object template)
  1229. {
  1230. Node nval = n.getAttributes().getNamedItem("val");
  1231. if (nval != null)
  1232. {
  1233. String val = nval.getNodeValue();
  1234. if (val.charAt(0) == '#')
  1235. { // table by level
  1236. return new LambdaConst(Double.parseDouble(getTableValue(val)));
  1237. }
  1238. else if (val.charAt(0) == '$')
  1239. {
  1240. if (val.equalsIgnoreCase("$player_level"))
  1241. {
  1242. return new LambdaStats(LambdaStats.StatsType.PLAYER_LEVEL);
  1243. }
  1244. if (val.equalsIgnoreCase("$target_level"))
  1245. {
  1246. return new LambdaStats(LambdaStats.StatsType.TARGET_LEVEL);
  1247. }
  1248. if (val.equalsIgnoreCase("$player_max_hp"))
  1249. {
  1250. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_HP);
  1251. }
  1252. if (val.equalsIgnoreCase("$player_max_mp"))
  1253. {
  1254. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_MP);
  1255. }
  1256. // try to find value out of item fields
  1257. StatsSet set = getStatsSet();
  1258. String field = set.getString(val.substring(1));
  1259. if (field != null)
  1260. {
  1261. return new LambdaConst(Double.parseDouble(getValue(field, template)));
  1262. }
  1263. // failed
  1264. throw new IllegalArgumentException("Unknown value " + val);
  1265. }
  1266. else
  1267. {
  1268. return new LambdaConst(Double.parseDouble(val));
  1269. }
  1270. }
  1271. LambdaCalc calc = new LambdaCalc();
  1272. n = n.getFirstChild();
  1273. while ((n != null) && (n.getNodeType() != Node.ELEMENT_NODE))
  1274. {
  1275. n = n.getNextSibling();
  1276. }
  1277. if ((n == null) || !"val".equals(n.getNodeName()))
  1278. {
  1279. throw new IllegalArgumentException("Value not specified");
  1280. }
  1281. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  1282. {
  1283. if (n.getNodeType() != Node.ELEMENT_NODE)
  1284. {
  1285. continue;
  1286. }
  1287. attachLambdaFunc(n, template, calc);
  1288. }
  1289. return calc;
  1290. }
  1291. protected String getValue(String value, Object template)
  1292. {
  1293. // is it a table?
  1294. if (value.charAt(0) == '#')
  1295. {
  1296. if (template instanceof Skill)
  1297. {
  1298. return getTableValue(value);
  1299. }
  1300. else if (template instanceof Integer)
  1301. {
  1302. return getTableValue(value, ((Integer) template).intValue());
  1303. }
  1304. else
  1305. {
  1306. throw new IllegalStateException();
  1307. }
  1308. }
  1309. return value;
  1310. }
  1311. protected Condition joinAnd(Condition cond, Condition c)
  1312. {
  1313. if (cond == null)
  1314. {
  1315. return c;
  1316. }
  1317. if (cond instanceof ConditionLogicAnd)
  1318. {
  1319. ((ConditionLogicAnd) cond).add(c);
  1320. return cond;
  1321. }
  1322. ConditionLogicAnd and = new ConditionLogicAnd();
  1323. and.add(cond);
  1324. and.add(c);
  1325. return and;
  1326. }
  1327. }