mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-16 08:56:04 +01:00
Support Open Chest
This commit is contained in:
@@ -42,7 +42,6 @@ public class SceneScriptManager {
|
||||
private final Int2ObjectOpenHashMap<Set<SceneTrigger>> currentTriggers;
|
||||
private final Int2ObjectOpenHashMap<SceneRegion> regions;
|
||||
private Map<Integer,SceneGroup> sceneGroups;
|
||||
private SceneGroup currentGroup;
|
||||
private ScriptMonsterTideService scriptMonsterTideService;
|
||||
private ScriptMonsterSpawnService scriptMonsterSpawnService;
|
||||
/**
|
||||
@@ -94,10 +93,6 @@ public class SceneScriptManager {
|
||||
return meta.config;
|
||||
}
|
||||
|
||||
public SceneGroup getCurrentGroup() {
|
||||
return currentGroup;
|
||||
}
|
||||
|
||||
public Map<Integer, SceneBlock> getBlocks() {
|
||||
return meta.blocks;
|
||||
}
|
||||
@@ -118,20 +113,19 @@ public class SceneScriptManager {
|
||||
this.triggers.remove(trigger.name);
|
||||
getTriggersByEvent(trigger.event).remove(trigger);
|
||||
}
|
||||
public void resetTriggers(List<String> triggerNames) {
|
||||
for(var name : triggerNames){
|
||||
var instance = triggers.get(name);
|
||||
this.currentTriggers.get(instance.event).clear();
|
||||
this.currentTriggers.get(instance.event).add(instance);
|
||||
}
|
||||
public void resetTriggers(int eventId) {
|
||||
currentTriggers.put(eventId, new HashSet<>());
|
||||
}
|
||||
public void refreshGroup(SceneGroup group, int suiteIndex){
|
||||
var suite = group.getSuiteByIndex(suiteIndex);
|
||||
if(suite == null){
|
||||
return;
|
||||
}
|
||||
if(suite.triggers.size() > 0){
|
||||
resetTriggers(suite.triggers);
|
||||
if(suite.sceneTriggers.size() > 0){
|
||||
for(var trigger : suite.sceneTriggers){
|
||||
resetTriggers(trigger.event);
|
||||
this.currentTriggers.get(trigger.event).add(trigger);
|
||||
}
|
||||
}
|
||||
spawnMonstersInGroup(group, suite);
|
||||
spawnGadgetsInGroup(group, suite);
|
||||
@@ -158,7 +152,7 @@ public class SceneScriptManager {
|
||||
for (SceneGroup group : block.groups) {
|
||||
if (group.id == groupId) {
|
||||
if(!group.isLoaded()){
|
||||
loadGroupFromScript(group);
|
||||
getScene().onLoadGroup(List.of(group));
|
||||
}
|
||||
return group;
|
||||
}
|
||||
@@ -204,9 +198,6 @@ public class SceneScriptManager {
|
||||
group.variables.forEach(var -> this.getVariables().put(var.name, var.value));
|
||||
this.sceneGroups.put(group.id, group);
|
||||
|
||||
if(group.triggers != null){
|
||||
group.triggers.forEach(this::registerTrigger);
|
||||
}
|
||||
if(group.regions != null){
|
||||
group.regions.forEach(this::registerRegion);
|
||||
}
|
||||
@@ -248,7 +239,7 @@ public class SceneScriptManager {
|
||||
}
|
||||
|
||||
var toCreate = gadgets.stream()
|
||||
.map(g -> createGadgets(g.groupId, group.block_id, g))
|
||||
.map(g -> createGadget(g.groupId, group.block_id, g))
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
this.addEntities(toCreate);
|
||||
@@ -265,20 +256,17 @@ public class SceneScriptManager {
|
||||
if(suite == null || suite.sceneMonsters.size() <= 0){
|
||||
return;
|
||||
}
|
||||
this.currentGroup = group;
|
||||
this.addEntities(suite.sceneMonsters.stream()
|
||||
.map(mob -> createMonster(group.id, group.block_id, mob)).toList());
|
||||
|
||||
}
|
||||
|
||||
public void spawnMonstersInGroup(SceneGroup group) {
|
||||
this.currentGroup = group;
|
||||
this.addEntities(group.monsters.values().stream()
|
||||
.map(mob -> createMonster(group.id, group.block_id, mob)).toList());
|
||||
}
|
||||
|
||||
public void startMonsterTideInGroup(SceneGroup group, Integer[] ordersConfigId, int tideCount, int sceneLimit) {
|
||||
this.currentGroup = group;
|
||||
this.scriptMonsterTideService =
|
||||
new ScriptMonsterTideService(this, group, tideCount, sceneLimit, ordersConfigId);
|
||||
|
||||
@@ -289,15 +277,15 @@ public class SceneScriptManager {
|
||||
}
|
||||
this.getScriptMonsterTideService().unload();
|
||||
}
|
||||
public void spawnMonstersByConfigId(int configId, int delayTime) {
|
||||
public void spawnMonstersByConfigId(SceneGroup group, int configId, int delayTime) {
|
||||
// TODO delay
|
||||
getScene().addEntity(
|
||||
createMonster(this.currentGroup.id, this.currentGroup.block_id, this.currentGroup.monsters.get(configId)));
|
||||
getScene().addEntity(createMonster(group.id, group.block_id, group.monsters.get(configId)));
|
||||
}
|
||||
// Events
|
||||
|
||||
public void callEvent(int eventType, ScriptArgs params) {
|
||||
for (SceneTrigger trigger : this.getTriggersByEvent(eventType)) {
|
||||
scriptLib.setCurrentGroup(trigger.currentGroup);
|
||||
LuaValue condition = null;
|
||||
|
||||
if (trigger.condition != null && !trigger.condition.isEmpty()) {
|
||||
@@ -330,6 +318,7 @@ public class SceneScriptManager {
|
||||
safetyCall(trigger.action, action, args);
|
||||
}
|
||||
//TODO some ret may not bool
|
||||
scriptLib.removeCurrentGroup();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,7 +326,7 @@ public class SceneScriptManager {
|
||||
try{
|
||||
return func.call(this.getScriptLibLua(), args);
|
||||
}catch (LuaError error){
|
||||
ScriptLib.logger.error("[LUA] call trigger failed {},{}",name,args,error);
|
||||
ScriptLib.logger.error("[LUA] call trigger failed {},{},{}",name,args,error.getMessage());
|
||||
return LuaValue.valueOf(-1);
|
||||
}
|
||||
}
|
||||
@@ -350,7 +339,7 @@ public class SceneScriptManager {
|
||||
return scriptMonsterSpawnService;
|
||||
}
|
||||
|
||||
public EntityGadget createGadgets(int groupId, int blockId, SceneGadget g) {
|
||||
public EntityGadget createGadget(int groupId, int blockId, SceneGadget g) {
|
||||
EntityGadget entity = new EntityGadget(getScene(), g.gadget_id, g.pos);
|
||||
|
||||
if (entity.getGadgetData() == null){
|
||||
|
||||
@@ -23,6 +23,7 @@ public class ScriptLib {
|
||||
|
||||
public ScriptLib(SceneScriptManager sceneScriptManager) {
|
||||
this.sceneScriptManager = sceneScriptManager;
|
||||
this.currentGroup = new ThreadLocal<>();
|
||||
}
|
||||
|
||||
public SceneScriptManager getSceneScriptManager() {
|
||||
@@ -38,7 +39,17 @@ public class ScriptLib {
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private final ThreadLocal<SceneGroup> currentGroup;
|
||||
public void setCurrentGroup(SceneGroup currentGroup){
|
||||
logger.debug("current {}", currentGroup);
|
||||
this.currentGroup.set(currentGroup);
|
||||
}
|
||||
public Optional<SceneGroup> getCurrentGroup(){
|
||||
return Optional.ofNullable(this.currentGroup.get());
|
||||
}
|
||||
public void removeCurrentGroup(){
|
||||
this.currentGroup.remove();
|
||||
}
|
||||
public int SetGadgetStateByConfigId(int configId, int gadgetState) {
|
||||
logger.debug("[LUA] Call SetGadgetStateByConfigId with {},{}",
|
||||
configId,gadgetState);
|
||||
@@ -100,7 +111,12 @@ public class ScriptLib {
|
||||
getSceneScriptManager().getScene().broadcastPacket(new PacketWorktopOptionNotify(gadget));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public int SetWorktopOptions(LuaTable table){
|
||||
logger.debug("[LUA] Call SetWorktopOptions with {}", printTable(table));
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
public int DelWorktopOptionByGroupId(int groupId, int configId, int option) {
|
||||
logger.debug("[LUA] Call DelWorktopOptionByGroupId with {},{},{}",groupId,configId,option);
|
||||
|
||||
@@ -272,7 +288,8 @@ public class ScriptLib {
|
||||
var1);
|
||||
|
||||
return (int) getSceneScriptManager().getScene().getEntities().values().stream()
|
||||
.filter(e -> e instanceof EntityMonster && e.getGroupId() == getSceneScriptManager().getCurrentGroup().id)
|
||||
.filter(e -> e instanceof EntityMonster &&
|
||||
e.getGroupId() == getCurrentGroup().map(sceneGroup -> sceneGroup.id).orElse(-1))
|
||||
.count();
|
||||
}
|
||||
public int SetMonsterBattleByGroup(int var1, int var2, int var3){
|
||||
@@ -334,7 +351,11 @@ public class ScriptLib {
|
||||
var configId = table.get("config_id").toint();
|
||||
var delayTime = table.get("delay_time").toint();
|
||||
|
||||
getSceneScriptManager().spawnMonstersByConfigId(configId, delayTime);
|
||||
if(getCurrentGroup().isEmpty()){
|
||||
return 1;
|
||||
}
|
||||
|
||||
getSceneScriptManager().spawnMonstersByConfigId(getCurrentGroup().get(), configId, delayTime);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -353,7 +374,13 @@ public class ScriptLib {
|
||||
printTable(table));
|
||||
var configId = table.get("config_id").toint();
|
||||
|
||||
//TODO
|
||||
var group = getCurrentGroup();
|
||||
if(group.isEmpty()){
|
||||
return 1;
|
||||
}
|
||||
var gadget = group.get().gadgets.get(configId);
|
||||
var entity = getSceneScriptManager().createGadget(group.get().id, group.get().block_id, gadget);
|
||||
getSceneScriptManager().addEntity(entity);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -376,14 +403,42 @@ public class ScriptLib {
|
||||
.filter(g -> g.getConfigId() == configId)
|
||||
.findFirst();
|
||||
if(gadget.isEmpty()){
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
var stat = ((EntityGadget)gadget.get()).getState();
|
||||
return stat;
|
||||
return ((EntityGadget)gadget.get()).getState();
|
||||
}
|
||||
public int SetGadgetStateByConfigId(int configId, LuaTable gadgetState){
|
||||
logger.debug("[LUA] Call SetGadgetStateByConfigId with {},{}",
|
||||
configId, printTable(gadgetState));
|
||||
|
||||
public int MarkPlayerAction(int var1, int var2, int var3, int var4){
|
||||
logger.debug("[LUA] Call MarkPlayerAction with {},{}",
|
||||
var1, var2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int AddQuestProgress(String var1){
|
||||
logger.debug("[LUA] Call AddQuestProgress with {}",
|
||||
var1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* change the state of gadget
|
||||
*/
|
||||
public int ChangeGroupGadget(LuaTable table){
|
||||
logger.debug("[LUA] Call ChangeGroupGadget with {}",
|
||||
printTable(table));
|
||||
var configId = table.get("config_id").toint();
|
||||
var state = table.get("state").toint();
|
||||
|
||||
var entity = getSceneScriptManager().getScene().getEntityByConfigId(configId);
|
||||
if(entity == null){
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(entity instanceof EntityGadget entityGadget){
|
||||
getSceneScriptManager().getScene().updateGadgetState(entityGadget, state);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package emu.grasscutter.scripts.constants;
|
||||
|
||||
public class EventType {
|
||||
public static final int EVENT_NONE = 0;
|
||||
/**
|
||||
* param1: monster.configId
|
||||
*/
|
||||
public static final int EVENT_ANY_MONSTER_DIE = 1;
|
||||
public static final int EVENT_ANY_GADGET_DIE = 2;
|
||||
public static final int EVENT_VARIABLE_CHANGE = 3;
|
||||
|
||||
@@ -34,7 +34,7 @@ public class SceneGroup {
|
||||
* ConfigId - Gadget
|
||||
*/
|
||||
public Map<Integer, SceneGadget> gadgets;
|
||||
public List<SceneTrigger> triggers;
|
||||
public Map<String, SceneTrigger> triggers;
|
||||
public List<SceneRegion> regions;
|
||||
public List<SceneSuite> suites;
|
||||
public SceneInitConfig init_config;
|
||||
@@ -88,8 +88,9 @@ public class SceneGroup {
|
||||
.collect(Collectors.toMap(x -> x.config_id, y -> y));
|
||||
gadgets.values().forEach(m -> m.groupId = id);
|
||||
|
||||
triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, bindings.get("triggers"));
|
||||
triggers.forEach(t -> t.currentGroup = this);
|
||||
triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, bindings.get("triggers")).stream()
|
||||
.collect(Collectors.toMap(x -> x.name, y -> y));
|
||||
triggers.values().forEach(t -> t.currentGroup = this);
|
||||
|
||||
suites = ScriptLoader.getSerializer().toList(SceneSuite.class, bindings.get("suites"));
|
||||
regions = ScriptLoader.getSerializer().toList(SceneRegion.class, bindings.get("regions"));
|
||||
@@ -113,6 +114,13 @@ public class SceneGroup {
|
||||
.map(gadgets::get)
|
||||
.toList()
|
||||
);
|
||||
|
||||
suite.sceneTriggers = new ArrayList<>(
|
||||
suite.triggers.stream()
|
||||
.filter(triggers::containsKey)
|
||||
.map(triggers::get)
|
||||
.toList()
|
||||
);
|
||||
}
|
||||
|
||||
} catch (ScriptException e) {
|
||||
|
||||
@@ -15,4 +15,5 @@ public class SceneSuite {
|
||||
|
||||
public transient List<SceneMonster> sceneMonsters;
|
||||
public transient List<SceneGadget> sceneGadgets;
|
||||
public transient List<SceneTrigger> sceneTriggers;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ public class SceneTrigger {
|
||||
public String condition;
|
||||
public String action;
|
||||
|
||||
public SceneGroup currentGroup;
|
||||
public transient SceneGroup currentGroup;
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof SceneTrigger sceneTrigger){
|
||||
|
||||
Reference in New Issue
Block a user