Skip to content

Commit

Permalink
Implement time axis
Browse files Browse the repository at this point in the history
  • Loading branch information
KingRainbow44 committed Aug 13, 2023
1 parent e7410a0 commit 6a42133
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 13 deletions.
40 changes: 33 additions & 7 deletions src/main/java/emu/grasscutter/scripts/SceneScriptManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package emu.grasscutter.scripts;

import static emu.grasscutter.scripts.constants.EventType.EVENT_TIMER_EVENT;

import com.github.davidmoten.rtreemulti.RTree;
import com.github.davidmoten.rtreemulti.geometry.Geometry;
import emu.grasscutter.Grasscutter;
Expand All @@ -21,24 +19,30 @@
import emu.grasscutter.utils.*;
import io.netty.util.concurrent.FastThreadLocalThread;
import it.unimi.dsi.fastutil.ints.*;
import kotlin.Pair;
import lombok.val;
import org.luaj.vm2.*;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;

import javax.annotation.*;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import javax.annotation.*;
import kotlin.Pair;
import lombok.val;
import org.luaj.vm2.*;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;

import static emu.grasscutter.scripts.constants.EventType.EVENT_TIMER_EVENT;

public class SceneScriptManager {
private final Scene scene;
private final Map<String, Integer> variables;
private SceneMeta meta;
private boolean isInit;

private final Map<String, SceneTimeAxis> timeAxis
= new ConcurrentHashMap<>();

/** current triggers controlled by RefreshGroup */
private final Map<Integer, Set<SceneTrigger>> currentTriggers;

Expand Down Expand Up @@ -1198,6 +1202,28 @@ public boolean isClearedGroupMonsters(int groupId) {
});
}

/**
* Registers a new time axis for this scene.
* Starts the time axis after.
*
* @param timeAxis The time axis.
*/
public void initTimeAxis(SceneTimeAxis timeAxis) {
this.timeAxis.put(timeAxis.getIdentifier(), timeAxis);
}

/**
* Terminates a time axis.
*
* @param identifier The identifier of the time axis.
*/
public void stopTimeAxis(String identifier) {
var timeAxis = this.timeAxis.get(identifier);
if (timeAxis != null) {
timeAxis.stop();
}
}

public void onDestroy() {
activeGroupTimers.forEach(
(gid, times) ->
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/emu/grasscutter/scripts/SceneTimeAxis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package emu.grasscutter.scripts;

import emu.grasscutter.scripts.constants.EventType;
import emu.grasscutter.scripts.data.ScriptArgs;
import lombok.*;

import java.util.*;

@Getter
@RequiredArgsConstructor
public final class SceneTimeAxis {
private final Timer timer = new Timer();

private final SceneScriptManager handle;
private final int groupId;

private final String identifier;
private final int delay;
private final boolean loop;

/**
* Schedules the task to run.
*/
public void start() {
if (this.loop) {
this.timer.scheduleAtFixedRate(
new Task(), this.delay, this.delay);
} else {
this.timer.schedule(new Task(), this.delay);
}
}

/**
* Terminates a repeating task.
*/
public void stop() {
this.timer.cancel();
}

final class Task extends TimerTask {
@Override
public void run() {
// Invoke script event.
SceneTimeAxis.this.handle.callEvent(new ScriptArgs(
SceneTimeAxis.this.groupId,
EventType.EVENT_TIME_AXIS_PASS
).setEventSource(SceneTimeAxis.this.identifier));
}
}
}
25 changes: 19 additions & 6 deletions src/main/java/emu/grasscutter/scripts/ScriptLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -956,14 +956,27 @@ public int StartSealBattle(int gadgetId, LuaTable var2){
return 0;
}

public int InitTimeAxis(String var1, int[] var2, boolean var3){
logger.warn("[LUA] Call unimplemented InitTimeAxis with {} {} {}", var1, var2, var3);
//TODO implement var1 == name? var2 == delay? var3 == should loop?
public int InitTimeAxis(String identifier, int[] delays, boolean shouldLoop) {
if (this.getCurrentGroup().isEmpty()) {
logger.warn("[LUA] Call InitTimeAxis without a group");
return 0;
}

var scriptManager = this.getSceneScriptManager();
var group = this.getCurrentGroup().get();

// Create a new time axis instance.
scriptManager.initTimeAxis(new SceneTimeAxis(
scriptManager, group.id,
identifier, delays[0], shouldLoop
));

return 0;
}
public int EndTimeAxis(String var1){
logger.warn("[LUA] Call unimplemented EndTimeAxis with {}", var1);
//TODO implement var1 == name?

public int EndTimeAxis(String identifier) {
this.getSceneScriptManager().stopTimeAxis(identifier);

return 0;
}

Expand Down

0 comments on commit 6a42133

Please sign in to comment.