Skip to content

Commit

Permalink
Store accept conditions like fail and finish content are
Browse files Browse the repository at this point in the history
Took the oppertunity to clean up some old code as well

conditions are stored in QuestManager
  • Loading branch information
scooterboo committed Sep 10, 2023
1 parent 1b7d289 commit 95cf7c1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 69 deletions.
61 changes: 2 additions & 59 deletions src/main/java/emu/grasscutter/game/quest/GameMainQuest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,6 @@ public void finish() {
this.state = ParentQuestState.PARENT_QUEST_STATE_FINISHED;
}

/*
* We also need to check for unfinished childQuests in this MainQuest
* force them to complete and send a packet about this to the user,
* because at some points there are special "invisible" child quests that control
* some situations.
*
* For example, subQuest 35312 is responsible for the event of leaving the territory
* of the island with a statue and automatically returns the character back,
* quest 35311 completes the main quest line 353 and starts 35501 from
* new MainQuest 355 but if 35312 is not completed after the completion
* of the main quest 353 - the character will not be able to leave place
* (return again and again)
*/
// this.getChildQuests().values().stream()
// .filter(p -> p.state != QuestState.QUEST_STATE_FINISHED)
// .forEach(GameQuest::finish);

this.getOwner().getSession().send(new PacketFinishedParentQuestUpdateNotify(this));
this.getOwner().getSession().send(new PacketCodexDataUpdateNotify(this));

Expand Down Expand Up @@ -383,46 +366,6 @@ public void checkProgress() {
}
}

public void tryAcceptSubQuests(QuestCond condType, String paramStr, int... params) {
try {
List<GameQuest> subQuestsWithCond =
getChildQuests().values().stream()
.filter(
p ->
p.getState() == QuestState.QUEST_STATE_UNSTARTED
|| p.getState() == QuestState.UNFINISHED)
.filter(
p ->
p.getQuestData().getAcceptCond().stream()
.anyMatch(
q ->
condType == QuestCond.QUEST_COND_NONE || q.getType() == condType))
.toList();
var questSystem = owner.getServer().getQuestSystem();

for (GameQuest subQuestWithCond : subQuestsWithCond) {
var acceptCond = subQuestWithCond.getQuestData().getAcceptCond();
int[] accept = new int[acceptCond.size()];

for (int i = 0; i < subQuestWithCond.getQuestData().getAcceptCond().size(); i++) {
var condition = acceptCond.get(i);
boolean result =
questSystem.triggerCondition(
getOwner(), subQuestWithCond.getQuestData(), condition, paramStr, params);
accept[i] = result ? 1 : 0;
}

boolean shouldAccept =
LogicType.calculate(subQuestWithCond.getQuestData().getAcceptCondComb(), accept);

if (shouldAccept) subQuestWithCond.start();
}
this.save();
} catch (Exception e) {
Grasscutter.getLogger().error("An error occurred while trying to accept quest.", e);
}
}

public void tryFailSubQuests(QuestContent condType, String paramStr, int... params) {
try {
List<GameQuest> subQuestsWithCond =
Expand All @@ -437,15 +380,15 @@ public void tryFailSubQuests(QuestContent condType, String paramStr, int... para
for (GameQuest subQuestWithCond : subQuestsWithCond) {
val failCond = subQuestWithCond.getQuestData().getFailCond();

for (int i = 0; i < subQuestWithCond.getQuestData().getFailCond().size(); i++) {
for (int i = 0; i < failCond.size(); i++) {
val condition = failCond.get(i);
if (condition.getType() == condType) {
boolean result =
this.getOwner()
.getServer()
.getQuestSystem()
.triggerContent(subQuestWithCond, condition, paramStr, params);
subQuestWithCond.getFailProgressList()[i] = result ? 1 : 0;
subQuestWithCond.setFailProgress(i, result ? 1 : 0);
if (result) {
getOwner().getSession().send(new PacketQuestProgressUpdateNotify(subQuestWithCond));
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/emu/grasscutter/game/quest/GameQuest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public void setFailProgress(int index, int value) {
public boolean clearProgress(boolean notifyDelete) {
// TODO improve
var oldState = state;
if (questData.getAcceptCond() != null && questData.getAcceptCond().size() != 0) {
this.getMainQuest().getQuestManager().getAcceptProgressLists().put(this.getSubQuestId(), new int[questData.getAcceptCond().size()]);
}

if (questData.getFinishCond() != null && questData.getFinishCond().size() != 0) {
for (var condition : questData.getFinishCond()) {
if (condition.getType() == QuestContent.QUEST_CONTENT_LUA_NOTIFY) {
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/emu/grasscutter/game/quest/QuestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public final class QuestManager extends BasePlayerManager {
@Getter private final Player player;

@Getter private final Int2ObjectMap<GameMainQuest> mainQuests;
@Getter private Int2ObjectMap<int[]> acceptProgressLists;
@Getter private final List<Integer> loggedQuests;

private long lastHourCheck = 0;
Expand All @@ -52,6 +53,7 @@ public QuestManager(Player player) {
this.player = player;
this.mainQuests = new Int2ObjectOpenHashMap<>();
this.loggedQuests = new ArrayList<>();
acceptProgressLists = new Int2ObjectOpenHashMap<>();

if (DEBUG) {
this.loggedQuests.addAll(
Expand Down Expand Up @@ -484,8 +486,6 @@ public void queueEvent(QuestCond condType, String paramStr, int... params) {
eventExecutor.submit(() -> triggerEvent(condType, paramStr, params));
}

// QUEST_EXEC are handled directly by each subQuest

public void triggerEvent(QuestCond condType, String paramStr, int... params) {
Grasscutter.getLogger().trace("Trigger Event {}, {}, {}", condType, paramStr, params);
var potentialQuests = GameData.getQuestDataByConditions(condType, params[0], paramStr);
Expand All @@ -502,15 +502,17 @@ public void triggerEvent(QuestCond condType, String paramStr, int... params) {
return;
}
val acceptCond = questData.getAcceptCond();
int[] accept = new int[acceptCond.size()];
acceptProgressLists.putIfAbsent(questData.getId(), new int[acceptCond.size()]);
for (int i = 0; i < acceptCond.size(); i++) {
val condition = acceptCond.get(i);
boolean result =
questSystem.triggerCondition(owner, questData, condition, paramStr, params);
accept[i] = result ? 1 : 0;
if (condition.getType() == condType) {
boolean result =
questSystem.triggerCondition(owner, questData, condition, paramStr, params);
acceptProgressLists.get(questData.getId())[i] = result ? 1 : 0;
}
}

boolean shouldAccept = LogicType.calculate(questData.getAcceptCondComb(), accept);
boolean shouldAccept = LogicType.calculate(questData.getAcceptCondComb(), acceptProgressLists.get(questData.getId()));
if (this.loggedQuests.contains(questData.getId())) {
Grasscutter.getLogger()
.debug(
Expand All @@ -522,7 +524,7 @@ public void triggerEvent(QuestCond condType, String paramStr, int... params) {
Arrays.stream(params)
.mapToObj(String::valueOf)
.collect(Collectors.joining(", ")));
for (var i = 0; i < accept.length; i++) {
for (var i = 0; i < acceptCond.size(); i++) {
var condition = acceptCond.get(i);
Grasscutter.getLogger()
.debug(
Expand All @@ -532,14 +534,14 @@ public void triggerEvent(QuestCond condType, String paramStr, int... params) {
.filter(value -> value > 0)
.mapToObj(String::valueOf)
.collect(Collectors.joining(", ")),
accept[i] == 1 ? "success" : "failure");
acceptProgressLists.get(questData.getId())[i] == 1 ? "success" : "failure");
}
}

if (shouldAccept) {
GameQuest quest = owner.getQuestManager().addQuest(questData);
Grasscutter.getLogger()
.debug("Added quest {} result {}", questData.getSubId(), quest != null);
.debug("Added quest {}", questData.getSubId());
}
});
}
Expand Down

0 comments on commit 95cf7c1

Please sign in to comment.