DocumentBase.java 44 KB

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