From ffc1f801e6cab1bd470aa9c674532f108362c8ed Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Fri, 29 Apr 2022 03:06:33 -0700 Subject: [PATCH] Implement spawning monsters by suite --- .../scripts/SceneScriptManager.java | 37 +++++++++++++++++-- .../emu/grasscutter/scripts/ScriptLib.java | 10 ++--- .../grasscutter/scripts/data/SceneGroup.java | 4 ++ .../grasscutter/scripts/data/SceneSuite.java | 3 ++ 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java b/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java index 5413021ea..2f2f31219 100644 --- a/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java +++ b/src/main/java/emu/grasscutter/scripts/SceneScriptManager.java @@ -6,6 +6,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import javax.script.Bindings; import javax.script.CompiledScript; @@ -43,7 +44,7 @@ public class SceneScriptManager { private final Scene scene; private final ScriptLib scriptLib; private final LuaValue scriptLibLua; - private final Map variables; + private final Map variables; private Bindings bindings; private SceneConfig config; @@ -91,7 +92,7 @@ public class SceneScriptManager { return blocks; } - public Map getVariables() { + public Map getVariables() { return variables; } @@ -212,8 +213,22 @@ public class SceneScriptManager { group.suites = ScriptLoader.getSerializer().toList(SceneSuite.class, bindings.get("suites")); group.init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, bindings.get("init_config")); + // Add variables to suite List variables = ScriptLoader.getSerializer().toList(SceneVar.class, bindings.get("variables")); - variables.forEach(var -> this.getVariables().put(var.name, LuaValue.valueOf(var.value))); + variables.forEach(var -> this.getVariables().put(var.name, var.value)); + + // Add monsters to suite TODO optimize + HashMap map = (HashMap) group.monsters.stream().collect(Collectors.toMap(m -> m.config_id, m -> m)); + + for (SceneSuite suite : group.suites) { + suite.sceneMonsters = new ArrayList<>(suite.monsters.size()); + for (int id : suite.monsters) { + SceneMonster monster = map.get(id); + if (monster != null) { + suite.sceneMonsters.add(monster); + } + } + } } catch (ScriptException e) { Grasscutter.getLogger().error("Error loading group " + group.id + " in scene " + getScene().getId(), e); } @@ -244,10 +259,24 @@ public class SceneScriptManager { } } + public void spawnMonstersInGroup(SceneGroup group, int suiteIndex) { + spawnMonstersInGroup(group, group.getSuiteByIndex(suiteIndex)); + } + public void spawnMonstersInGroup(SceneGroup group) { + spawnMonstersInGroup(group, null); + } + + public void spawnMonstersInGroup(SceneGroup group, SceneSuite suite) { + List monsters = group.monsters; + + if (suite != null) { + monsters = suite.sceneMonsters; + } + List toAdd = new ArrayList<>(); - for (SceneMonster monster : group.monsters) { + for (SceneMonster monster : monsters) { MonsterData data = GameData.getMonsterDataMap().get(monster.monster_id); if (data == null) { diff --git a/src/main/java/emu/grasscutter/scripts/ScriptLib.java b/src/main/java/emu/grasscutter/scripts/ScriptLib.java index 9a20359e5..57cd73dd3 100644 --- a/src/main/java/emu/grasscutter/scripts/ScriptLib.java +++ b/src/main/java/emu/grasscutter/scripts/ScriptLib.java @@ -129,7 +129,7 @@ public class ScriptLib { } // TODO just spawn all from group for now - this.getSceneScriptManager().spawnMonstersInGroup(group); + this.getSceneScriptManager().spawnMonstersInGroup(group, suite); return 0; } @@ -158,11 +158,11 @@ public class ScriptLib { .count(); } - public LuaValue GetGroupVariableValue(String var) { - return getSceneScriptManager().getVariables().getOrDefault(var, LuaValue.NIL); + public int GetGroupVariableValue(String var) { + return getSceneScriptManager().getVariables().getOrDefault(var, 0); } - public LuaValue ChangeGroupVariableValue(String var, LuaValue value) { + public LuaValue ChangeGroupVariableValue(String var, int value) { getSceneScriptManager().getVariables().put(var, value); return LuaValue.ZERO; } @@ -179,7 +179,7 @@ public class ScriptLib { } // TODO just spawn all from group for now - this.getSceneScriptManager().spawnMonstersInGroup(group); + this.getSceneScriptManager().spawnMonstersInGroup(group, suite); return 0; } diff --git a/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java b/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java index 833f4f9e2..d72d02d53 100644 --- a/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java +++ b/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java @@ -26,4 +26,8 @@ public class SceneGroup { public boolean setLoaded(boolean loaded) { return loaded; } + + public SceneSuite getSuiteByIndex(int index) { + return suites.get(index - 1); + } } diff --git a/src/main/java/emu/grasscutter/scripts/data/SceneSuite.java b/src/main/java/emu/grasscutter/scripts/data/SceneSuite.java index be9bc0f08..d84504569 100644 --- a/src/main/java/emu/grasscutter/scripts/data/SceneSuite.java +++ b/src/main/java/emu/grasscutter/scripts/data/SceneSuite.java @@ -5,6 +5,9 @@ import java.util.List; import emu.grasscutter.utils.Position; public class SceneSuite { + public List monsters; public List triggers; public int rand_weight; + + public transient List sceneMonsters; }