Commit d71b7abf authored by Melledy's avatar Melledy
Browse files

Implement script support needed for dungeons

Only a few are supported right now
You will need certain script files in ./resources/Scripts
parent 53cc1822
package emu.grasscutter.scripts.constants;
public class ScriptGadgetState {
public static final int Default = 0;
public static final int GatherDrop = 1;
public static final int ChestLocked = 101;
public static final int ChestOpened = 102;
public static final int ChestTrap = 103;
public static final int ChestBramble = 104;
public static final int ChestFrozen = 105;
public static final int ChestRock = 106;
public static final int GearStart = 201;
public static final int GearStop = 202;
public static final int GearAction1 = 203;
public static final int GearAction2 = 204;
public static final int CrystalResonate1 = 301;
public static final int CrystalResonate2 = 302;
public static final int CrystalExplode = 303;
public static final int CrystalDrain = 304;
public static final int StatueActive = 401;
public static final int Action01 = 901;
public static final int Action02 = 902;
public static final int Action03 = 903;
}
package emu.grasscutter.scripts.constants;
public class ScriptRegionShape {
public static final int NONE = 0;
public static final int SPHERE = 1;
public static final int CUBIC = 2;
}
package emu.grasscutter.scripts.data;
import java.util.List;
import emu.grasscutter.utils.Position;
public class SceneBlock {
public int id;
public Position max;
public Position min;
public List<SceneGroup> groups;
public boolean contains(Position pos) {
return pos.getX() <= max.getX() && pos.getX() >= min.getX() &&
pos.getZ() <= max.getZ() && pos.getZ() >= min.getZ();
}
}
package emu.grasscutter.scripts.data;
import emu.grasscutter.utils.Position;
public class SceneConfig {
public Position vision_anchor;
public Position born_pos;
public Position born_rot;
public Position begin_pos;
public Position size;
}
package emu.grasscutter.scripts.data;
import emu.grasscutter.utils.Position;
public class SceneGadget {
public int level;
public int config_id;
public int gadget_id;
public int state;
public Position pos;
public Position rot;
}
package emu.grasscutter.scripts.data;
import java.util.List;
import emu.grasscutter.utils.Position;
public class SceneGroup {
public int id;
public int refresh_id;
public Position pos;
public List<SceneMonster> monsters;
public List<SceneGadget> gadgets;
public List<SceneTrigger> triggers;
public List<SceneSuite> suites;
public SceneInitConfig init_config;
}
package emu.grasscutter.scripts.data;
import emu.grasscutter.utils.Position;
public class SceneInitConfig {
public int suite;
public int end_suite;
public int rand_suite;
}
package emu.grasscutter.scripts.data;
import emu.grasscutter.utils.Position;
public class SceneMonster {
public int level;
public int config_id;
public int monster_id;
public Position pos;
public Position rot;
}
package emu.grasscutter.scripts.data;
import java.util.List;
import emu.grasscutter.utils.Position;
public class SceneSuite {
public List<String> triggers;
public int rand_weight;
}
package emu.grasscutter.scripts.data;
public class SceneTrigger {
public String name;
public int config_id;
public int event;
public String source;
public String condition;
public String action;
}
package emu.grasscutter.scripts.serializer;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
public class LuaSerializer implements Serializer {
@Override
public <T> List<T> toList(Class<T> type, Object obj) {
return serializeList(type, (LuaTable) obj);
}
@Override
public <T> T toObject(Class<T> type, Object obj) {
return serialize(type, (LuaTable) obj);
}
public <T> List<T> serializeList(Class<T> type, LuaTable table) {
List<T> list = new ArrayList();
try {
LuaValue[] keys = table.keys();
for (LuaValue k : keys) {
try {
LuaValue keyValue = table.get(k);
T object = null;
if (keyValue.istable()) {
object = serialize(type, keyValue.checktable());
} else if (keyValue.isint()) {
object = (T) (Integer) keyValue.toint();
} else if (keyValue.isnumber()) {
object = (T) (Float) keyValue.tofloat(); // terrible...
} else if (keyValue.isstring()) {
object = (T) keyValue.tojstring();
} else {
object = (T) keyValue;
}
if (object != null) {
list.add(object);
}
} catch (Exception ex) {
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public <T> T serialize(Class<T> type, LuaTable table) {
T object = null;
if (type == List.class) {
try {
Class<T> listType = (Class<T>) type.getTypeParameters()[0].getClass();
return (T) serializeList(listType, table);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
try {
object = type.getDeclaredConstructor().newInstance(null);
LuaValue[] keys = table.keys();
for (LuaValue k : keys) {
try {
Field field = object.getClass().getDeclaredField(k.checkjstring());
if (field == null) {
continue;
}
field.setAccessible(true);
LuaValue keyValue = table.get(k);
if (keyValue.istable()) {
field.set(object, serialize(field.getType(), keyValue.checktable()));
} else if (field.getType().equals(float.class)) {
field.setFloat(object, keyValue.tofloat());
} else if (field.getType().equals(int.class)) {
field.setInt(object, keyValue.toint());
} else if (field.getType().equals(String.class)) {
field.set(object, keyValue.tojstring());
} else {
field.set(object, keyValue);
}
} catch (Exception ex) {
//ex.printStackTrace();
continue;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
}
package emu.grasscutter.scripts.serializer;
import java.util.List;
import org.luaj.vm2.LuaTable;
public interface Serializer {
public <T> List<T> toList(Class<T> type, Object obj);
public <T> T toObject(Class<T> type, Object obj);
}
package emu.grasscutter.server.packet.recv;
import emu.grasscutter.net.packet.Opcodes;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.SelectWorktopOptionReqOuterClass.SelectWorktopOptionReq;
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.packet.send.PacketSelectWorktopOptionRsp;
@Opcodes(PacketOpcodes.SelectWorktopOptionReq)
public class HandlerSelectWorktopOptionReq extends PacketHandler {
@Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
SelectWorktopOptionReq req = SelectWorktopOptionReq.parseFrom(payload);
session.getPlayer().getScene().getScriptManager().onOptionSelect(req.getGadgetEntityId(), req.getOptionId());
session.send(new PacketSelectWorktopOptionRsp(req.getGadgetEntityId(), req.getOptionId()));
}
}
package emu.grasscutter.server.packet.send;
import emu.grasscutter.game.dungeons.DungeonChallenge;
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.DungeonChallengeBeginNotifyOuterClass.DungeonChallengeBeginNotify;
public class PacketDungeonChallengeBeginNotify extends BasePacket {
public PacketDungeonChallengeBeginNotify(DungeonChallenge challenge) {
super(PacketOpcodes.DungeonChallengeBeginNotify);
DungeonChallengeBeginNotify proto = DungeonChallengeBeginNotify.newBuilder()
.setChallengeId(challenge.getChallengeId())
.setChallengeIndex(challenge.getChallengeIndex())
.build();
this.setData(proto);
}
}
package emu.grasscutter.server.packet.send;
import emu.grasscutter.game.dungeons.DungeonChallenge;
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.DungeonChallengeFinishNotifyOuterClass.DungeonChallengeFinishNotify;
public class PacketDungeonChallengeFinishNotify extends BasePacket {
public PacketDungeonChallengeFinishNotify(DungeonChallenge challenge) {
super(PacketOpcodes.DungeonChallengeFinishNotify);
DungeonChallengeFinishNotify proto = DungeonChallengeFinishNotify.newBuilder()
.setChallengeIndex(challenge.getChallengeId())
.setIsSuccess(challenge.isSuccess())
.build();
this.setData(proto);
}
}
package emu.grasscutter.server.packet.send;
import emu.grasscutter.game.entity.EntityGadget;
import emu.grasscutter.game.entity.EntityBaseGadget;
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.GadgetInteractRspOuterClass.GadgetInteractRsp;
......@@ -8,7 +8,7 @@ import emu.grasscutter.net.proto.InteractTypeOuterClass.InteractType;
import emu.grasscutter.net.proto.RetcodeOuterClass;
public class PacketGadgetInteractRsp extends BasePacket {
public PacketGadgetInteractRsp(EntityGadget gadget, InteractType interact) {
public PacketGadgetInteractRsp(EntityBaseGadget gadget, InteractType interact) {
super(PacketOpcodes.GadgetInteractRsp);
GadgetInteractRsp proto = GadgetInteractRsp.newBuilder()
......
package emu.grasscutter.server.packet.send;
import emu.grasscutter.game.entity.EntityGadget;
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.GadgetStateNotifyOuterClass.GadgetStateNotify;
public class PacketGadgetStateNotify extends BasePacket {
public PacketGadgetStateNotify(EntityGadget gadget, int newState) {
super(PacketOpcodes.GadgetStateNotify);
GadgetStateNotify proto = GadgetStateNotify.newBuilder()
.setGadgetEntityId(gadget.getId())
.setGadgetState(newState)
.setIsEnableInteract(true)
.build();
this.setData(proto);
}
}
package emu.grasscutter.server.packet.send;
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.SelectWorktopOptionRspOuterClass.SelectWorktopOptionRsp;
public class PacketSelectWorktopOptionRsp extends BasePacket {
public PacketSelectWorktopOptionRsp(int entityId, int optionId) {
super(PacketOpcodes.SelectWorktopOptionRsp);
SelectWorktopOptionRsp proto = SelectWorktopOptionRsp.newBuilder()
.setGadgetEntityId(entityId)
.setOptionId(optionId)
.build();
this.setData(proto);
}
}
package emu.grasscutter.server.packet.send;
import emu.grasscutter.game.entity.EntityGadget;
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.WorktopOptionNotifyOuterClass.WorktopOptionNotify;
public class PacketWorktopOptionNotify extends BasePacket {
public PacketWorktopOptionNotify(EntityGadget gadget) {
super(PacketOpcodes.WorktopOptionNotify);
WorktopOptionNotify.Builder proto = WorktopOptionNotify.newBuilder()
.setGadgetEntityId(gadget.getId());
if (gadget.getWorktopOptions() != null) {
proto.addAllOptionList(gadget.getWorktopOptions());
}
this.setData(proto);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment