PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Trunk/src/net/sf/odinms/server/life/MapleLifeFactory.java

https://github.com/system32/NinjaMS
Java | 151 lines | 117 code | 14 blank | 20 comment | 31 complexity | f939fc67d15e72791dd00028509dc3a3 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*
  2. This file is part of the OdinMS Maple Story Server
  3. Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
  4. Matthias Butz <matze@odinms.de>
  5. Jan Christian Meyer <vimes@odinms.de>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License version 3
  8. as published by the Free Software Foundation. You may not use, modify
  9. or distribute this program under any other version of the
  10. GNU Affero General Public License.
  11. This program 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
  14. GNU Affero General Public License for more details.
  15. You should have received a copy of the GNU Affero General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package net.sf.odinms.server.life;
  19. import java.io.File;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. import java.util.Map;
  25. import net.sf.odinms.provider.MapleData;
  26. import net.sf.odinms.provider.MapleDataProvider;
  27. import net.sf.odinms.provider.MapleDataProviderFactory;
  28. import net.sf.odinms.provider.MapleDataTool;
  29. import net.sf.odinms.provider.wz.MapleDataType;
  30. import net.sf.odinms.server.maps.MapleMapFactory;
  31. import net.sf.odinms.tools.Pair;
  32. import net.sf.odinms.tools.StringUtil;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. public class MapleLifeFactory {
  36. private static Logger log = LoggerFactory.getLogger(MapleMapFactory.class);
  37. private static MapleDataProvider data = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("net.sf.odinms.wzpath") + "/Mob.wz"));
  38. private static MapleDataProvider stringDataWZ = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("net.sf.odinms.wzpath") + "/String.wz"));
  39. private static MapleData mobStringData = stringDataWZ.getData("Mob.img");
  40. private static MapleData npcStringData = stringDataWZ.getData("Npc.img");
  41. private static Map<Integer, MapleMonsterStats> monsterStats = new HashMap<Integer, MapleMonsterStats>();
  42. public static AbstractLoadedMapleLife getLife(int id, String type) {
  43. if (type.equalsIgnoreCase("n")) {
  44. return getNPC(id);
  45. } else if (type.equalsIgnoreCase("m")) {
  46. return getMonster(id);
  47. } else {
  48. log.warn("Unknown Life type: {}", type);
  49. return null;
  50. }
  51. }
  52. public static MapleMonster getMonster (int mid) {
  53. MapleMonsterStats stats = monsterStats.get(Integer.valueOf(mid));
  54. if (stats == null) {
  55. MapleData monsterData = data.getData(StringUtil.getLeftPaddedStr(Integer.toString(mid) + ".img", '0', 11));
  56. if (monsterData == null) {
  57. return null;
  58. }
  59. MapleData monsterInfoData = monsterData.getChildByPath("info");
  60. stats = new MapleMonsterStats();
  61. stats.setHp(MapleDataTool.getIntConvert("maxHP", monsterInfoData));
  62. stats.setMp(MapleDataTool.getIntConvert("maxMP", monsterInfoData, 0));
  63. stats.setExp(MapleDataTool.getIntConvert("exp", monsterInfoData, 0));
  64. stats.setLevel(MapleDataTool.getIntConvert("level", monsterInfoData));
  65. stats.setBoss (MapleDataTool.getIntConvert("boss", monsterInfoData, 0) > 0);
  66. stats.setUndead (MapleDataTool.getIntConvert("undead", monsterInfoData, 0) > 0);
  67. stats.setName(MapleDataTool.getString(mid + "/name", mobStringData, "MISSINGNO"));
  68. stats.setBuffToGive(MapleDataTool.getIntConvert("buff", monsterInfoData, -1));
  69. MapleData firstAttackData = monsterInfoData.getChildByPath("firstAttack");
  70. int firstAttack = 0;
  71. if (firstAttackData != null) {
  72. if (firstAttackData.getType() == MapleDataType.FLOAT) {
  73. firstAttack = Math.round(MapleDataTool.getFloat(firstAttackData));
  74. } else {
  75. firstAttack = MapleDataTool.getInt(firstAttackData);
  76. }
  77. }
  78. stats.setFirstAttack(firstAttack > 0);
  79. if (stats.isBoss() || mid == 8810018) {
  80. MapleData hpTagColor = monsterInfoData.getChildByPath("hpTagColor");
  81. MapleData hpTagBgColor = monsterInfoData.getChildByPath("hpTagBgcolor");
  82. if (hpTagBgColor == null || hpTagColor == null) {
  83. log.trace("Monster " + stats.getName() + " (" + mid + ") flagged as boss without boss HP bars.");
  84. stats.setTagColor(0);
  85. stats.setTagBgColor(0);
  86. } else {
  87. stats.setTagColor(MapleDataTool.getIntConvert("hpTagColor", monsterInfoData));
  88. stats.setTagBgColor(MapleDataTool.getIntConvert("hpTagBgcolor", monsterInfoData));
  89. }
  90. }
  91. for (MapleData idata : monsterData) {
  92. if (!idata.getName().equals("info")) {
  93. int delay = 0;
  94. for (MapleData pic : idata.getChildren()) {
  95. delay += MapleDataTool.getIntConvert("delay", pic, 0);
  96. }
  97. stats.setAnimationTime(idata.getName(), delay);
  98. }
  99. }
  100. MapleData reviveInfo = monsterInfoData.getChildByPath("revive");
  101. if (reviveInfo != null) {
  102. List<Integer> revives = new LinkedList<Integer>();
  103. for (MapleData data : reviveInfo) {
  104. revives.add(MapleDataTool.getInt(data));
  105. }
  106. stats.setRevives(revives);
  107. }
  108. decodeElementalString(stats, MapleDataTool.getString("elemAttr", monsterInfoData, ""));
  109. MapleData monsterSkillData = monsterInfoData.getChildByPath("skill");
  110. if (monsterSkillData != null) {
  111. int i = 0;
  112. List<Pair<Integer, Integer>> skills = new ArrayList<Pair<Integer, Integer>>();
  113. while(monsterSkillData.getChildByPath(Integer.toString(i)) != null) {
  114. skills.add(new Pair<Integer, Integer>(Integer.valueOf(MapleDataTool.getInt(i + "/skill", monsterSkillData, 0)), Integer.valueOf(MapleDataTool.getInt(i + "/level", monsterSkillData, 0))));
  115. i++;
  116. }
  117. stats.setSkills(skills);
  118. }
  119. monsterStats.put(Integer.valueOf(mid), stats);
  120. }
  121. MapleMonster ret = new MapleMonster(mid, stats);
  122. return ret;
  123. }
  124. public static void decodeElementalString (MapleMonsterStats stats, String elemAttr) {
  125. for (int i = 0; i < elemAttr.length(); i += 2) {
  126. Element e = Element.getFromChar(elemAttr.charAt(i));
  127. ElementalEffectiveness ee = ElementalEffectiveness.getByNumber(Integer.valueOf(String.valueOf(elemAttr.charAt(i+1))));
  128. stats.setEffectiveness(e, ee);
  129. }
  130. }
  131. public static MapleNPC getNPC(int nid) {
  132. return new MapleNPC(nid, new MapleNPCStats(MapleDataTool.getString(nid + "/name", npcStringData, "MISSINGNO")));
  133. }
  134. }