Improve Platforms (#2275)

* Improve Platforms

* add this.
This commit is contained in:
Nazrin 2023-07-29 08:38:34 -07:00 committed by GitHub
parent f3277dee9f
commit 98fbc4e512
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.binout.*;
import emu.grasscutter.data.binout.AbilityModifier.AbilityModifierAction;
import emu.grasscutter.data.binout.config.*;
import emu.grasscutter.data.binout.routes.*;
import emu.grasscutter.data.common.PointData;
import emu.grasscutter.data.custom.*;
import emu.grasscutter.data.excels.trial.TrialAvatarActivityDataData;
@ -107,6 +108,7 @@ public final class ResourceLoader {
// Load default home layout
loadHomeworldDefaultSaveData();
loadNpcBornData();
loadRoutes();
loadBlossomResources();
cacheTalentLevelSets();
// Load activities.
@ -264,6 +266,30 @@ public final class ResourceLoader {
}
}
private static void loadRoutes() {
try {
Files.newDirectoryStream(getResourcePath("BinOutput/LevelDesign/Routes/"),"*.json")
.forEach(
path -> {
try {
val data = JsonUtils.loadToClass(path, SceneRoutes.class);
val routesArray = data.getRoutes();
if (routesArray == null) return;
val routesMap = GameData.getSceneRouteData().getOrDefault(data.getSceneId(), new Int2ObjectOpenHashMap<>());
for (Route route : routesArray) {
routesMap.put(route.getLocalId(), route);
}
GameData.getSceneRouteData().put(data.getSceneId(), routesMap);
} catch (IOException ignored) {
}
});
Grasscutter.getLogger()
.debug("Loaded " + GameData.getSceneNpcBornData().size() + " SceneRouteDatas.");
} catch (IOException e) {
Grasscutter.getLogger().error("Failed to load SceneRouteData folder.");
}
}
private static void cacheTalentLevelSets() {
// All known levels, keyed by proudSkillGroupId
GameData.getProudSkillDataMap()

View File

@ -7,6 +7,7 @@ import emu.grasscutter.data.binout.config.fields.ConfigAbilityData;
import emu.grasscutter.data.excels.GadgetData;
import emu.grasscutter.game.entity.gadget.*;
import emu.grasscutter.game.entity.gadget.platform.BaseRoute;
import emu.grasscutter.game.entity.gadget.platform.ConfigRoute;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.*;
import emu.grasscutter.game.world.*;
@ -246,6 +247,37 @@ public class EntityGadget extends EntityBaseGadget {
if (routeConfig.isStarted()) {
return true;
}
if(routeConfig instanceof ConfigRoute configRoute) {
var route = this.getScene().getSceneRouteById(configRoute.getRouteId());
if(route != null) {
var points = route.getPoints();
val currIndex = configRoute.getStartIndex();
Position prevpos;
if(currIndex == 0) {
prevpos = getPosition();
this.getScene().getScriptManager().callEvent(new ScriptArgs(this.getGroupId(), EventType.EVENT_PLATFORM_REACH_POINT, this.getConfigId(), configRoute.getRouteId()).setParam3(0).setEventSource(this.getConfigId()));
}else {
prevpos = points[currIndex].getPos();
}
double time = 0;
for(var i = currIndex; i < points.length;++i) {
time += points[i].getPos().computeDistance(prevpos) / points[i].getTargetVelocity();
prevpos = points[i].getPos();
val I = i;
configRoute.getScheduledIndexes().add(this.getScene().getScheduler().scheduleDelayedTask(() -> {
if(points[I].isHasReachEvent() && I > currIndex) {
this.getScene().getScriptManager().callEvent(new ScriptArgs(this.getGroupId(), EventType.EVENT_PLATFORM_REACH_POINT, this.getConfigId(), configRoute.getRouteId()).setParam3(I).setEventSource(this.getConfigId()));
}
configRoute.setStartIndex(I);
this.position.set(points[I].getPos());
},(int)time));
}
}
}
getScene().broadcastPacket(new PacketSceneTimeNotify(getScene()));
routeConfig.startRoute(getScene());
getScene().broadcastPacket(new PacketPlatformStartRouteNotify(this));
@ -261,6 +293,14 @@ public class EntityGadget extends EntityBaseGadget {
if (!routeConfig.isStarted()) {
return true;
}
if(routeConfig instanceof ConfigRoute configRoute) {
for(var task : configRoute.getScheduledIndexes()) {
this.getScene().getScheduler().cancelTask(task);
}
configRoute.getScheduledIndexes().clear();
}
routeConfig.stopRoute(getScene());
getScene().broadcastPacket(new PacketPlatformStopRouteNotify(this));

View File

@ -6,25 +6,34 @@ import emu.grasscutter.net.proto.PlatformInfoOuterClass;
import emu.grasscutter.scripts.data.SceneGadget;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
public class ConfigRoute extends BaseRoute {
@Getter @Setter private int routeId;
@Getter @Setter private int startIndex;
@Getter @Setter private List<Integer> scheduledIndexes;
public ConfigRoute(SceneGadget gadget) {
super(gadget);
this.routeId = gadget.route_id;
this.startIndex = 0;
this.scheduledIndexes = new ArrayList<>();
}
public ConfigRoute(Position startRot, boolean startRoute, boolean isActive, int routeId) {
super(startRot, startRoute, isActive);
this.routeId = routeId;
this.startIndex = 0;
this.scheduledIndexes = new ArrayList<>();
}
@Override
public PlatformInfoOuterClass.PlatformInfo.Builder toProto() {
return super.toProto()
.setRouteId(routeId)
.setRouteId(this.routeId)
.setStartIndex(this.startIndex)
.setMovingPlatformType(
MovingPlatformTypeOuterClass.MovingPlatformType.MOVING_PLATFORM_TYPE_USE_CONFIG);
}

View File

@ -31,6 +31,7 @@ import emu.grasscutter.scripts.data.*;
import emu.grasscutter.server.event.entity.EntityCreationEvent;
import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.server.packet.send.*;
import emu.grasscutter.server.scheduler.ServerTaskScheduler;
import emu.grasscutter.utils.objects.KahnsSort;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import java.util.*;
@ -71,6 +72,7 @@ public final class Scene {
private final List<Runnable> afterHostInitCallbacks = new ArrayList<>();
@Getter private GameEntity sceneEntity;
@Getter private final ServerTaskScheduler scheduler;
public Scene(World world, SceneData sceneData) {
this.world = world;
@ -94,6 +96,7 @@ public final class Scene {
this.blossomManager = new BlossomManager(this);
this.unlockedForces = new HashSet<>();
this.sceneEntity = new EntityScene(this);
this.scheduler = new ServerTaskScheduler();
}
public int getId() {
@ -533,6 +536,10 @@ public final class Scene {
return;
}
if(!isPaused) {
this.getScheduler().runTasks();
}
if (this.getScriptManager().isInit()) {
// this.checkBlocks();
this.checkGroups();

View File

@ -1319,6 +1319,13 @@ public class ScriptLib {
}
configRoute.setRouteId(routeId);
configRoute.setStartIndex(0);
configRoute.setStarted(false);
for(var task : configRoute.getScheduledIndexes()) {
sceneScriptManager.get().getScene().getScheduler().cancelTask(task);
}
configRoute.getScheduledIndexes().clear();
sceneScriptManager.get().getScene().broadcastPacket(new PacketPlatformChangeRouteNotify(entityGadget));
return 0;
}

View File

@ -36,7 +36,7 @@ public final class ServerTask implements Runnable {
*/
public boolean shouldRun() {
// Increase tick count.
var ticks = this.ticks++;
++this.ticks;
if (this.delay != -1 && this.considerDelay) {
// Check if the task should run.
var shouldRun = ticks >= this.delay;