2022-04-29 05:19:14 +00:00
|
|
|
package emu.grasscutter.scripts;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
import org.luaj.vm2.LuaTable;
|
2022-04-29 09:38:25 +00:00
|
|
|
import org.luaj.vm2.LuaValue;
|
2022-04-29 05:19:14 +00:00
|
|
|
|
|
|
|
import emu.grasscutter.Grasscutter;
|
|
|
|
import emu.grasscutter.data.GameData;
|
|
|
|
import emu.grasscutter.data.def.MonsterData;
|
|
|
|
import emu.grasscutter.game.dungeons.DungeonChallenge;
|
|
|
|
import emu.grasscutter.game.entity.EntityGadget;
|
|
|
|
import emu.grasscutter.game.entity.EntityMonster;
|
|
|
|
import emu.grasscutter.game.entity.GameEntity;
|
2022-04-29 07:00:23 +00:00
|
|
|
import emu.grasscutter.scripts.constants.EventType;
|
2022-04-29 05:19:14 +00:00
|
|
|
import emu.grasscutter.scripts.data.SceneGroup;
|
|
|
|
import emu.grasscutter.scripts.data.SceneMonster;
|
2022-04-30 08:08:38 +00:00
|
|
|
import emu.grasscutter.scripts.data.SceneRegion;
|
2022-04-29 07:00:23 +00:00
|
|
|
import emu.grasscutter.scripts.data.ScriptArgs;
|
2022-04-29 05:19:14 +00:00
|
|
|
import emu.grasscutter.server.packet.send.PacketGadgetStateNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketWorktopOptionNotify;
|
|
|
|
|
|
|
|
public class ScriptLib {
|
|
|
|
private final SceneScriptManager sceneScriptManager;
|
|
|
|
|
|
|
|
public ScriptLib(SceneScriptManager sceneScriptManager) {
|
|
|
|
this.sceneScriptManager = sceneScriptManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SceneScriptManager getSceneScriptManager() {
|
|
|
|
return sceneScriptManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int SetGadgetStateByConfigId(int configId, int gadgetState) {
|
|
|
|
Optional<GameEntity> entity = getSceneScriptManager().getScene().getEntities().values().stream()
|
|
|
|
.filter(e -> e.getConfigId() == configId).findFirst();
|
|
|
|
|
|
|
|
if (entity.isEmpty()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(entity.get() instanceof EntityGadget)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityGadget gadget = (EntityGadget) entity.get();
|
|
|
|
gadget.setState(gadgetState);
|
|
|
|
|
|
|
|
getSceneScriptManager().getScene().broadcastPacket(new PacketGadgetStateNotify(gadget, gadgetState));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int SetGroupGadgetStateByConfigId(int groupId, int configId, int gadgetState) {
|
|
|
|
List<GameEntity> list = getSceneScriptManager().getScene().getEntities().values().stream()
|
|
|
|
.filter(e -> e.getGroupId() == groupId).toList();
|
|
|
|
|
|
|
|
for (GameEntity entity : list) {
|
2022-04-29 07:52:40 +00:00
|
|
|
if (!(entity instanceof EntityGadget)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-29 05:19:14 +00:00
|
|
|
EntityGadget gadget = (EntityGadget) entity;
|
|
|
|
gadget.setState(gadgetState);
|
|
|
|
|
|
|
|
getSceneScriptManager().getScene().broadcastPacket(new PacketGadgetStateNotify(gadget, gadgetState));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int SetWorktopOptionsByGroupId(int groupId, int configId, int[] options) {
|
|
|
|
Optional<GameEntity> entity = getSceneScriptManager().getScene().getEntities().values().stream()
|
|
|
|
.filter(e -> e.getConfigId() == configId && e.getGroupId() == groupId).findFirst();
|
|
|
|
|
|
|
|
if (entity.isEmpty()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(entity.get() instanceof EntityGadget)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityGadget gadget = (EntityGadget) entity.get();
|
|
|
|
gadget.addWorktopOptions(options);
|
|
|
|
|
|
|
|
getSceneScriptManager().getScene().broadcastPacket(new PacketWorktopOptionNotify(gadget));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int DelWorktopOptionByGroupId(int groupId, int configId, int option) {
|
|
|
|
Optional<GameEntity> entity = getSceneScriptManager().getScene().getEntities().values().stream()
|
|
|
|
.filter(e -> e.getConfigId() == configId && e.getGroupId() == groupId).findFirst();
|
|
|
|
|
|
|
|
if (entity.isEmpty()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(entity.get() instanceof EntityGadget)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityGadget gadget = (EntityGadget) entity.get();
|
|
|
|
gadget.removeWorktopOption(option);
|
|
|
|
|
|
|
|
getSceneScriptManager().getScene().broadcastPacket(new PacketWorktopOptionNotify(gadget));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some fields are guessed
|
|
|
|
public int AutoMonsterTide(int challengeIndex, int groupId, int[] config_ids, int param4, int param5, int param6) {
|
|
|
|
SceneGroup group = getSceneScriptManager().getGroupById(groupId);
|
|
|
|
|
|
|
|
if (group == null || group.monsters == null) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO just spawn all from group for now
|
2022-04-29 08:03:16 +00:00
|
|
|
this.getSceneScriptManager().spawnMonstersInGroup(group);
|
2022-04-29 05:19:14 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-29 09:38:25 +00:00
|
|
|
public int AddExtraGroupSuite(int groupId, int suite) {
|
2022-04-29 07:52:40 +00:00
|
|
|
SceneGroup group = getSceneScriptManager().getGroupById(groupId);
|
|
|
|
|
|
|
|
if (group == null || group.monsters == null) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO just spawn all from group for now
|
2022-04-29 10:06:33 +00:00
|
|
|
this.getSceneScriptManager().spawnMonstersInGroup(group, suite);
|
2022-04-29 07:52:40 +00:00
|
|
|
|
2022-04-29 07:49:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-29 07:52:40 +00:00
|
|
|
// param3 (probably time limit for timed dungeons)
|
2022-05-04 06:13:42 +00:00
|
|
|
public int ActiveChallenge(int challengeId, int challengeIndex, int param3, int groupId, int objectiveKills, int param5) {
|
2022-04-29 05:19:14 +00:00
|
|
|
SceneGroup group = getSceneScriptManager().getGroupById(groupId);
|
|
|
|
|
|
|
|
if (group == null || group.monsters == null) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DungeonChallenge challenge = new DungeonChallenge(getSceneScriptManager().getScene(), group);
|
|
|
|
challenge.setChallengeId(challengeId);
|
|
|
|
challenge.setChallengeIndex(challengeIndex);
|
2022-05-04 06:13:42 +00:00
|
|
|
challenge.setObjective(objectiveKills);
|
2022-04-29 05:19:14 +00:00
|
|
|
|
|
|
|
getSceneScriptManager().getScene().setChallenge(challenge);
|
|
|
|
|
|
|
|
challenge.start();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-29 09:38:25 +00:00
|
|
|
public int GetGroupMonsterCountByGroupId(int groupId) {
|
|
|
|
return (int) getSceneScriptManager().getScene().getEntities().values().stream()
|
|
|
|
.filter(e -> e instanceof EntityMonster && e.getGroupId() == groupId)
|
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
2022-04-29 10:06:33 +00:00
|
|
|
public int GetGroupVariableValue(String var) {
|
|
|
|
return getSceneScriptManager().getVariables().getOrDefault(var, 0);
|
2022-04-29 09:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 06:13:42 +00:00
|
|
|
public int SetGroupVariableValue(String var, int value) {
|
2022-04-29 09:38:25 +00:00
|
|
|
getSceneScriptManager().getVariables().put(var, value);
|
2022-05-04 06:13:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LuaValue ChangeGroupVariableValue(String var, int value) {
|
|
|
|
getSceneScriptManager().getVariables().put(var, getSceneScriptManager().getVariables().get(var) + value);
|
2022-04-29 09:38:25 +00:00
|
|
|
return LuaValue.ZERO;
|
|
|
|
}
|
|
|
|
|
2022-04-29 05:19:14 +00:00
|
|
|
public int RefreshGroup(LuaTable table) {
|
2022-04-29 09:38:25 +00:00
|
|
|
// Kill and Respawn?
|
|
|
|
int groupId = table.get("group_id").toint();
|
|
|
|
int suite = table.get("suite").toint();
|
|
|
|
|
|
|
|
SceneGroup group = getSceneScriptManager().getGroupById(groupId);
|
|
|
|
|
|
|
|
if (group == null || group.monsters == null) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-29 10:06:33 +00:00
|
|
|
this.getSceneScriptManager().spawnMonstersInGroup(group, suite);
|
2022-05-04 06:13:42 +00:00
|
|
|
this.getSceneScriptManager().spawnGadgetsInGroup(group, suite);
|
2022-04-29 09:38:25 +00:00
|
|
|
|
2022-04-29 05:19:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-30 08:08:38 +00:00
|
|
|
public int GetRegionEntityCount(LuaTable table) {
|
|
|
|
int regionId = table.get("region_eid").toint();
|
|
|
|
int entityType = table.get("entity_type").toint();
|
|
|
|
|
|
|
|
SceneRegion region = this.getSceneScriptManager().getRegionById(regionId);
|
|
|
|
|
|
|
|
if (region == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) region.getEntities().intStream().filter(e -> e >> 24 == entityType).count();
|
|
|
|
}
|
|
|
|
|
2022-04-29 05:19:14 +00:00
|
|
|
public void PrintContextLog(String msg) {
|
|
|
|
Grasscutter.getLogger().info("[LUA] " + msg);
|
|
|
|
}
|
|
|
|
}
|