MapleQuest.java
Upload User: gwt600
Upload Date: 2021-06-03
Package Size: 704k
Code Size: 9k
Category:

Games

Development Platform:

Java

  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. /*
  19.  * MapleQuest.java
  20.  *
  21.  * Created on 10. Dezember 2007, 23:09
  22.  */
  23. package net.sf.odinms.server.quest;
  24. import java.io.File;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.HashMap;
  28. import java.util.HashSet;
  29. import java.util.LinkedList;
  30. import java.util.List;
  31. import java.util.Map;
  32. import java.util.Set;
  33. import net.sf.odinms.client.MapleCharacter;
  34. import net.sf.odinms.client.MapleQuestStatus;
  35. import net.sf.odinms.client.MapleQuestStatus.Status;
  36. import net.sf.odinms.provider.MapleData;
  37. import net.sf.odinms.provider.MapleDataProvider;
  38. import net.sf.odinms.provider.MapleDataProviderFactory;
  39. import net.sf.odinms.provider.MapleDataTool;
  40. import org.slf4j.Logger;
  41. import org.slf4j.LoggerFactory;
  42. /**
  43.  *
  44.  * @author Matze
  45.  */
  46. public class MapleQuest {
  47. private static Map<Integer,MapleQuest> quests = new HashMap<Integer,MapleQuest>();
  48. protected int id;
  49. protected List<MapleQuestRequirement> startReqs;
  50. protected List<MapleQuestRequirement> completeReqs;
  51. protected List<MapleQuestAction> startActs;
  52. protected List<MapleQuestAction> completeActs;
  53. protected List<Integer> relevantMobs;
  54. private boolean autoStart;
  55. private boolean autoPreComplete;
  56. private boolean repeatable = false;
  57. private static MapleDataProvider questData = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("net.sf.odinms.wzpath") + "/Quest.wz"));
  58. private static MapleData actions = questData.getData("Act.img");
  59. private static MapleData requirements = questData.getData("Check.img");
  60. private static MapleData info = questData.getData("QuestInfo.img");
  61. protected static final Logger log = LoggerFactory.getLogger(MapleQuest.class);
  62. protected MapleQuest() {
  63. relevantMobs = new LinkedList<Integer>();
  64. }
  65. /** Creates a new instance of MapleQuest */
  66. private MapleQuest(int id) {
  67. this.id = id;
  68. relevantMobs = new LinkedList<Integer>();
  69. // read reqs
  70. MapleData startReqData = requirements.getChildByPath(String.valueOf(id)).getChildByPath("0");
  71. startReqs = new LinkedList<MapleQuestRequirement>();
  72. if (startReqData != null) {
  73. for (MapleData startReq : startReqData.getChildren()) {
  74. MapleQuestRequirementType type = MapleQuestRequirementType.getByWZName(startReq.getName());
  75. if (type.equals(MapleQuestRequirementType.INTERVAL))
  76. repeatable = true;
  77. MapleQuestRequirement req = new MapleQuestRequirement(
  78. this, type, startReq);
  79. if (req.getType().equals(MapleQuestRequirementType.MOB)) {
  80. for (MapleData mob : startReq.getChildren()) {
  81. relevantMobs.add(MapleDataTool.getInt(mob.getChildByPath("id")));
  82. }
  83. }
  84. startReqs.add(req);
  85. }
  86. }
  87. MapleData completeReqData = requirements.getChildByPath(String.valueOf(id)).getChildByPath("1");
  88. completeReqs = new LinkedList<MapleQuestRequirement>();
  89. if (completeReqData != null) {
  90. for (MapleData completeReq : completeReqData.getChildren()) {
  91. MapleQuestRequirement req = new MapleQuestRequirement(
  92. this, MapleQuestRequirementType.getByWZName(completeReq.getName()), completeReq);
  93. if (req.getType().equals(MapleQuestRequirementType.MOB)) {
  94. for (MapleData mob : completeReq.getChildren()) {
  95. relevantMobs.add(MapleDataTool.getInt(mob.getChildByPath("id")));
  96. }
  97. }
  98. completeReqs.add(req);
  99. }
  100. }
  101. // read acts
  102. MapleData startActData = actions.getChildByPath(String.valueOf(id)).getChildByPath("0");
  103. startActs = new LinkedList<MapleQuestAction>();
  104. if (startActData != null) {
  105. for (MapleData startAct : startActData.getChildren()) {
  106. MapleQuestActionType questActionType = MapleQuestActionType.getByWZName(startAct.getName());
  107. startActs.add(new MapleQuestAction(questActionType, startAct, this));
  108. }
  109. }
  110. MapleData completeActData = actions.getChildByPath(String.valueOf(id)).getChildByPath("1");
  111. completeActs = new LinkedList<MapleQuestAction>();
  112. if (completeActData != null) {
  113. for (MapleData completeAct : completeActData.getChildren()) {
  114. completeActs.add(new MapleQuestAction(
  115. MapleQuestActionType.getByWZName(completeAct.getName()), completeAct, this));
  116. }
  117. }
  118. MapleData questInfo = info.getChildByPath(String.valueOf(id));
  119. autoStart = MapleDataTool.getInt("autoStart", questInfo, 0) == 1;
  120. autoPreComplete = MapleDataTool.getInt("autoPreComplete", questInfo, 0) == 1;
  121. }
  122. public static MapleQuest getInstance(int id) {
  123. MapleQuest ret = quests.get(id);
  124. if (ret == null) {
  125. if (id > 99999)
  126. ret = new MapleCustomQuest(id);
  127. else
  128. ret = new MapleQuest(id);
  129. quests.put(id, ret);
  130. }
  131. return ret;
  132. }
  133. private boolean canStart(MapleCharacter c, Integer npcid) {
  134. if (c.getQuest(this).getStatus() != Status.NOT_STARTED && !(c.getQuest(this).getStatus() == Status.COMPLETED && repeatable)) {
  135. return false;
  136. }
  137. for (MapleQuestRequirement r : startReqs) {
  138. if (!r.check(c, npcid)) return false;
  139. }
  140. return true;
  141. }
  142. public boolean canComplete(MapleCharacter c, Integer npcid) {
  143. if (!c.getQuest(this).getStatus().equals(Status.STARTED)) return false;
  144. for (MapleQuestRequirement r : completeReqs) {
  145. if (!r.check(c, npcid)) return false;
  146. }
  147. return true;
  148. }
  149. public void start(MapleCharacter c, int npc) {
  150. if ((autoStart || checkNPCOnMap(c, npc)) && canStart(c, npc)) {
  151. for (MapleQuestAction a : startActs) {
  152. a.run(c, null);
  153. }
  154. MapleQuestStatus oldStatus = c.getQuest(this);
  155. MapleQuestStatus newStatus = new MapleQuestStatus(this, MapleQuestStatus.Status.STARTED, npc);
  156. newStatus.setCompletionTime(oldStatus.getCompletionTime());
  157. newStatus.setForfeited(oldStatus.getForfeited());
  158. c.updateQuest(newStatus);
  159. }
  160. }
  161. public void complete(MapleCharacter c, int npc) {
  162. complete(c, npc, null);
  163. }
  164. public void complete(MapleCharacter c, int npc, Integer selection) {
  165. if ((autoPreComplete || checkNPCOnMap(c, npc)) && canComplete(c, npc)) {
  166. for (MapleQuestAction a : completeActs) {
  167. if (!a.check(c)) {
  168. return;
  169. }
  170. }
  171. for (MapleQuestAction a : completeActs) {
  172. a.run(c, selection);
  173. }
  174. // we save forfeits only for logging purposes, they shouldn't matter anymore
  175. // completion time is set by the constructor
  176. MapleQuestStatus oldStatus = c.getQuest(this);
  177. MapleQuestStatus newStatus = new MapleQuestStatus(this, MapleQuestStatus.Status.COMPLETED, npc);
  178. newStatus.setForfeited(oldStatus.getForfeited());
  179. c.updateQuest(newStatus);
  180. }
  181. }
  182. public void forfeit(MapleCharacter c) {
  183. if (!c.getQuest(this).getStatus().equals(Status.STARTED)) return;
  184. MapleQuestStatus oldStatus = c.getQuest(this);
  185. MapleQuestStatus newStatus = new MapleQuestStatus(this, MapleQuestStatus.Status.NOT_STARTED);
  186. newStatus.setForfeited(oldStatus.getForfeited() + 1);
  187. newStatus.setCompletionTime(oldStatus.getCompletionTime());
  188. c.updateQuest(newStatus);
  189. }
  190. public void forceStart(MapleCharacter c, int npc) {
  191. MapleQuestStatus oldStatus = c.getQuest(this);
  192. MapleQuestStatus newStatus = new MapleQuestStatus(this, MapleQuestStatus.Status.STARTED, npc);
  193. newStatus.setForfeited(oldStatus.getForfeited());
  194. c.updateQuest(newStatus);
  195. }
  196. public void forceComplete(MapleCharacter c, int npc) {
  197. MapleQuestStatus oldStatus = c.getQuest(this);
  198. MapleQuestStatus newStatus = new MapleQuestStatus(this, MapleQuestStatus.Status.COMPLETED, npc);
  199. newStatus.setForfeited(oldStatus.getForfeited());
  200. c.updateQuest(newStatus);
  201. }
  202. public int getId() {
  203. return id;
  204. }
  205. public List<Integer> getRelevantMobs() {
  206. return Collections.unmodifiableList(relevantMobs);
  207. }
  208. private boolean checkNPCOnMap(MapleCharacter player, int npcid) {
  209. if (player.getMap().containsNPC(npcid)) {
  210. return true;
  211. } else {
  212. return false;
  213. }
  214. }
  215. public List<Integer> getQuestItemsToShowOnlyIfQuestIsActivated() {
  216. Set<Integer> delta = new HashSet<Integer>();
  217. for(MapleQuestRequirement mqr : this.completeReqs) {
  218. if(mqr.getType() != MapleQuestRequirementType.ITEM) continue;
  219. delta.addAll(mqr.getQuestItemsToShowOnlyIfQuestIsActivated());
  220. }
  221. List<Integer> returnThis = new ArrayList<Integer>();
  222. returnThis.addAll(delta);
  223. return Collections.unmodifiableList(returnThis); 
  224. }
  225. }