forked from MagicBane/Server
				
			
				 5 changed files with 183 additions and 11 deletions
			
			
		| @ -0,0 +1,112 @@ | |||||||
|  | package engine.QuestSystem; | ||||||
|  | 
 | ||||||
|  | import engine.InterestManagement.WorldGrid; | ||||||
|  | import engine.net.client.msg.ErrorPopupMsg; | ||||||
|  | import engine.objects.*; | ||||||
|  | import engine.server.MBServerStatics; | ||||||
|  | 
 | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.HashSet; | ||||||
|  | import java.util.Random; | ||||||
|  | 
 | ||||||
|  | public class QuestManager { | ||||||
|  |     public static HashMap<PlayerCharacter,QuestObject> acceptedQuests; | ||||||
|  |     public static HashMap<PlayerCharacter,ArrayList<String>> completedQuests; | ||||||
|  | 
 | ||||||
|  |     public static boolean grantsQuest(NPC npc){ | ||||||
|  |         if(npc == null) | ||||||
|  |             return false; | ||||||
|  | 
 | ||||||
|  |         if(npc.contract == null) | ||||||
|  |             return false; | ||||||
|  | 
 | ||||||
|  |         return npc.contract.getName().contains("ZoneLore") || npc.contract.getName().equals("Barrowlands Sentry"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void displayCurrentQuest(PlayerCharacter pc){ | ||||||
|  |         if(acceptedQuests.containsKey(pc)){ | ||||||
|  |             QuestObject obj = acceptedQuests.get(pc); | ||||||
|  |             String output = "You have embarked on the noble quest: "; | ||||||
|  |             output += obj.QuestName + ". "; | ||||||
|  |             output += obj.description; | ||||||
|  |             output += ". " + obj.objectiveCount + " / " + obj.objectiveCountRequired; | ||||||
|  |             ErrorPopupMsg.sendErrorMsg(pc, output); | ||||||
|  |         }else{ | ||||||
|  |             ErrorPopupMsg.sendErrorMsg(pc, "You have no active quest."); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void acceptQuest(PlayerCharacter pc, QuestObject obj){ | ||||||
|  |         if (completedQuests.containsKey(pc)) { | ||||||
|  |             if(completedQuests.get(pc).contains(obj.QuestName)){ | ||||||
|  |                 ErrorPopupMsg.sendErrorMsg(pc, "You have already completed the quest: " + obj.QuestName); | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if(acceptedQuests.containsKey(pc)){ | ||||||
|  |             if(acceptedQuests.get(pc)!= null){ | ||||||
|  |                 ErrorPopupMsg.sendErrorMsg(pc, "You have already embarked on a noble quest."); | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         acceptedQuests.put(pc,obj); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void completeQuest(PlayerCharacter pc, QuestObject obj){ | ||||||
|  |         //notify the player they have completed their quest
 | ||||||
|  |         ErrorPopupMsg.sendErrorMsg(pc, "You have completed the quest: " + obj.QuestName); | ||||||
|  | 
 | ||||||
|  |         //add completed quest to completion log
 | ||||||
|  |         if (completedQuests.containsKey(pc)) { | ||||||
|  |             completedQuests.get(pc).add(obj.QuestName); | ||||||
|  |         }else{ | ||||||
|  |             ArrayList<String> completed = new ArrayList<>(); | ||||||
|  |             completed.add(obj.QuestName); | ||||||
|  |             completedQuests.put(pc,completed); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         //remove current quest from active log
 | ||||||
|  |         if(acceptedQuests.containsKey(pc)) | ||||||
|  |             acceptedQuests.remove(pc); | ||||||
|  | 
 | ||||||
|  |         //grant rewards
 | ||||||
|  |         pc.grantXP((int) (Experience.maxXPPerKill(pc.getLevel()) * 10)); | ||||||
|  |         pc.getCharItemManager().addGoldToInventory((int) Experience.maxXPPerKill(pc.getLevel()),false); | ||||||
|  |         pc.getCharItemManager().updateInventory(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static QuestObject getQuestForContract(NPC npc){ | ||||||
|  |         QuestObject obj = new QuestObject(); | ||||||
|  |         obj.QuestName = npc.getFirstName() + "'s Quest"; | ||||||
|  | 
 | ||||||
|  |         HashSet<AbstractWorldObject> mobs = WorldGrid.getObjectsInRangePartial(npc.loc,2048, MBServerStatics.MASK_MOB); | ||||||
|  |         if (mobs.isEmpty()) | ||||||
|  |             return null; // Handle the case where the set is empty
 | ||||||
|  | 
 | ||||||
|  |         // Convert HashSet to a List
 | ||||||
|  |         ArrayList<AbstractWorldObject> mobList = new ArrayList<>(mobs); | ||||||
|  | 
 | ||||||
|  |         // Generate a random index
 | ||||||
|  |         Random random = new Random(); | ||||||
|  |         int randomIndex = random.nextInt(mobList.size()); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         if(mobList.get(randomIndex) == null) { | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         Mob mob = (Mob) mobList.get(randomIndex); | ||||||
|  | 
 | ||||||
|  |         obj.progressionNames = new ArrayList<>(); | ||||||
|  |         obj.progressionNames.add(mob.getFirstName()); | ||||||
|  | 
 | ||||||
|  |         obj.objectiveCountRequired = 10; | ||||||
|  |         obj.objectiveCount = 0; | ||||||
|  | 
 | ||||||
|  |         obj.description = "These lands are plagued with " + mob.getFirstName() + " complete the quest by slaying 10 of them!"; | ||||||
|  |         return obj; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,31 @@ | |||||||
|  | package engine.QuestSystem; | ||||||
|  | 
 | ||||||
|  | import engine.objects.ItemBase; | ||||||
|  | import engine.objects.NPC; | ||||||
|  | import engine.objects.PlayerCharacter; | ||||||
|  | 
 | ||||||
|  | import java.util.ArrayList; | ||||||
|  | 
 | ||||||
|  | public class QuestObject { | ||||||
|  | 
 | ||||||
|  |     public String QuestName; | ||||||
|  |     public String description; | ||||||
|  |     public int objectiveCount; | ||||||
|  |     public int objectiveCountRequired; | ||||||
|  |     public ArrayList<String> progressionNames; | ||||||
|  |     public PlayerCharacter owner; | ||||||
|  | 
 | ||||||
|  |     public QuestObject(){ | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  |     public void tryProgress(String option){ | ||||||
|  |         if(this.progressionNames.contains(option)) | ||||||
|  |             this.objectiveCount++; | ||||||
|  |         else | ||||||
|  |             return; | ||||||
|  | 
 | ||||||
|  |         if(this.objectiveCount >= this.objectiveCountRequired){ | ||||||
|  |             QuestManager.completeQuest(this.owner,this); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue