Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for /world/proc/Tick() #1540

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Content.Tests/DMProject/Tests/Tree/Global/World/tick.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

//# issue 361

var/counter = 0
var/counter_updated = FALSE

/world/Tick()
ASSERT(!counter_updated)
var/updated = ++counter
counter_updated = TRUE
sleep(world.tick_lag)
// this should run before the next world.Tick()
ASSERT(!counter_updated)
ASSERT(updated == counter)

/proc/RunTest()
sleep(world.tick_lag) // at time of writing, initial call to DreamThread.Run happens before the first tick and it's fucky
counter_updated = FALSE
for(var/i in 1 to 100)
var/last_read = counter
sleep(-1)
ASSERT(!counter_updated)
ASSERT(last_read == counter)
sleep(0)
ASSERT(!counter_updated)
ASSERT(last_read == counter)
sleep(world.tick_lag)
var/expected = last_read + 1
var/actual = counter
ASSERT(counter_updated)
if(expected != actual)
CRASH("Expected: [expected] Actual: [actual]!")
counter_updated = FALSE
6 changes: 5 additions & 1 deletion DMCompiler/DMStandard/Types/World.dm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/world
/world
var/list/contents = null
var/list/vars

Expand Down Expand Up @@ -114,6 +114,10 @@
set opendream_unimplemented = TRUE
return 0

proc/Tick()
set waitfor = FALSE
return null

proc/ODHotReloadInterface()

proc/ODHotReloadResource(var/file_name)
17 changes: 14 additions & 3 deletions OpenDreamRuntime/DreamManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Text.Json;
Expand Down Expand Up @@ -60,11 +61,15 @@ public sealed partial class DreamManager {
private int _dreamObjectRefIdCounter;

private DreamCompiledJson _compiledJson;

[MemberNotNullWhen(true, nameof(_worldTickProc))]
public bool Initialized { get; private set; }
public GameTick InitializedTick { get; private set; }

private ISawmill _sawmill = default!;

private DreamProc? _worldTickProc;

//TODO This arg is awful and temporary until RT supports cvar overrides in unit tests
public void PreInitialize(string? jsonPath) {
_sawmill = Logger.GetSawmill("opendream");
Expand Down Expand Up @@ -104,8 +109,12 @@ public void Update() {
if (!Initialized)
return;

_procScheduler.Process();
UpdateStat();
// first process is allowed to update delays and run deferred tasks
_procScheduler.Process(true);
DreamThread.Run(_worldTickProc, WorldInstance, null);
_procScheduler.Process(false); // nothing is allowed to run after /world/Tick()

UpdateStat(); // ... except for Stat
_dreamMapManager.UpdateTiles();
DreamObjectSavefile.FlushAllUpdates();
WorldInstance.SetVariableValue("cpu", WorldInstance.GetVariable("tick_usage"));
Expand Down Expand Up @@ -149,6 +158,8 @@ public bool LoadJson(string? jsonPath) {
// Call /world/<init>. This is an IMPLEMENTATION DETAIL and non-DMStandard should NOT be run here.
WorldInstance.InitSpawn(new());

_worldTickProc = WorldInstance.GetProc("Tick");

if (_compiledJson.Globals is GlobalListJson jsonGlobals) {
Globals = new DreamValue[jsonGlobals.GlobalCount];
GlobalNames = jsonGlobals.Names;
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamRuntime/Procs/ProcScheduler.Delays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Task CreateDelayTicks(int ticks) {
}

var tcs = new TaskCompletionSource();

InsertTask(new DelayTicker(tcs) { TicksAt = _gameTiming.CurTick.Value + (uint)ticks }); //safe cast because ticks is always positive here
return tcs.Task;
}
Expand Down
6 changes: 4 additions & 2 deletions OpenDreamRuntime/Procs/ProcScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ async Task Foo() {
return task;
}

public void Process() {
UpdateDelays();
public void Process(bool updateDelays) {
if (updateDelays) {
UpdateDelays();
}

// Update all asynchronous tasks that have finished waiting and are ready to resume.
//
Expand Down
Loading