Skip to content

Commit

Permalink
Merge branch 'master' into signmeup
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 authored Feb 8, 2025
2 parents 5ad4e4f + d5bdf43 commit 9361676
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 14 deletions.
25 changes: 18 additions & 7 deletions DMCompiler/DM/DMCodeTree.Vars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override bool TryDefineVar(DMCompiler compiler, int pass) {
if (!compiler.DMObjectTree.TryGetDMObject(owner, out var dmObject))
return false;

if (AlreadyExists(compiler, dmObject)) {
if (CheckCantDefine(compiler, dmObject)) {
_defined = true;
return true;
}
Expand Down Expand Up @@ -162,12 +162,23 @@ private bool HandleInstanceVar(DMCompiler compiler, DMObject dmObject) {
return true;
}

private bool AlreadyExists(DMCompiler compiler, DMObject dmObject) {
// "type" and "tag" can only be defined in DMStandard
if (VarName is "type" or "tag" && !varDef.Location.InDMStandard) {
compiler.Emit(WarningCode.InvalidVarDefinition, varDef.Location,
$"Cannot redefine built-in var \"{VarName}\"");
return true;
private bool CheckCantDefine(DMCompiler compiler, DMObject dmObject) {
if (!compiler.Settings.NoStandard) {
var inStandard = varDef.Location.InDMStandard;

// "type" and "tag" can only be defined in DMStandard
if (VarName is "type" or "tag" && !inStandard) {
compiler.Emit(WarningCode.InvalidVarDefinition, varDef.Location,
$"Cannot redefine built-in var \"{VarName}\"");
return true;
}

// Vars on /world and /list can only be defined in DMStandard
if ((dmObject.Path == DreamPath.World || dmObject.Path == DreamPath.List) && !inStandard) {
compiler.Emit(WarningCode.InvalidVarDefinition, varDef.Location,
$"Cannot define a var on type {dmObject.Path}");
return true;
}
}

//DMObjects store two bundles of variables; the statics in GlobalVariables and the non-statics in Variables.
Expand Down
2 changes: 1 addition & 1 deletion DMCompiler/DMStandard/Types/Sound.dm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
var/environment as opendream_unimplemented
var/echo as opendream_unimplemented
var/len as opendream_unimplemented
var/offset as opendream_unimplemented
var/offset

var/priority = 0 as opendream_unimplemented
var/status = 0 as opendream_unimplemented
Expand Down
1 change: 0 additions & 1 deletion DMCompiler/DMStandard/_Standard.dm
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ proc/viewers(Depth, Center = usr) as /list
proc/walk(Ref, Dir, Lag = 0, Speed = 0)
proc/walk_rand(Ref,Lag = 0,Speed = 0)
proc/walk_to(Ref, Trg, Min = 0, Lag = 0, Speed = 0)
set opendream_unimplemented = 1
proc/walk_towards(Ref,Trg,Lag=0,Speed=0)
proc/winclone(player, window_name, clone_name)
proc/winexists(player, control_id) as text
Expand Down
6 changes: 3 additions & 3 deletions OpenDreamClient/Audio/DreamSoundEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Initialize() {
_netManager.Disconnect += DisconnectedFromServer;
}

public void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume) {
public void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume, float offset) {
if (_audioSystem == null)
_entitySystemManager.Resolve(ref _audioSystem);

Expand Down Expand Up @@ -59,7 +59,7 @@ public void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sou
}

var db = 20 * MathF.Log10(volume); // convert from DM volume (0-100) to OpenAL volume (db)
var source = _audioSystem.PlayGlobal(stream, AudioParams.Default.WithVolume(db)); // TODO: Positional audio.
var source = _audioSystem.PlayGlobal(stream, AudioParams.Default.WithVolume(db).WithPlayOffset(offset)); // TODO: Positional audio.
if (source == null) {
_sawmill.Error($"Failed to play audio ${sound}");
return;
Expand All @@ -86,7 +86,7 @@ public void StopAllChannels() {
private void RxSound(MsgSound msg) {
if (msg.ResourceId.HasValue) {
_resourceManager.LoadResourceAsync<ResourceSound>(msg.ResourceId.Value,
sound => PlaySound(msg.Channel, msg.Format!.Value, sound, msg.Volume / 100.0f));
sound => PlaySound(msg.Channel, msg.Format!.Value, sound, msg.Volume / 100.0f, msg.Offset));
} else {
StopChannel(msg.Channel);
}
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamClient/Audio/IDreamSoundEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace OpenDreamClient.Audio;

public interface IDreamSoundEngine {
void Initialize();
void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume);
void PlaySound(int channel, MsgSound.FormatType format, ResourceSound sound, float volume, float offset);
void StopChannel(int channel);
void StopAllChannels();
}
4 changes: 3 additions & 1 deletion OpenDreamRuntime/DreamConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,13 @@ public void OutputDreamValue(DreamValue value) {
if (value.TryGetValueAsDreamObject<DreamObjectSound>(out var outputObject)) {
ushort channel = (ushort)outputObject.GetVariable("channel").GetValueAsInteger();
ushort volume = (ushort)outputObject.GetVariable("volume").GetValueAsInteger();
float offset = outputObject.GetVariable("offset").UnsafeGetValueAsFloat();
DreamValue file = outputObject.GetVariable("file");

var msg = new MsgSound() {
Channel = channel,
Volume = volume
Volume = volume,
Offset = offset
};

if (!file.TryGetValueAsDreamResource(out var soundResource)) {
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_walk);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_walk_rand);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_walk_towards);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_walk_to);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_winclone);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_winexists);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_winget);
Expand Down
23 changes: 23 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3229,6 +3229,29 @@ public static DreamValue NativeProc_walk_towards(NativeProc.Bundle bundle, Dream
return DreamValue.Null;
}

[DreamProc("walk_to")]
[DreamProcParameter("Ref", Type = DreamValueTypeFlag.DreamObject)]
[DreamProcParameter("Trg", Type = DreamValueTypeFlag.DreamObject)]
[DreamProcParameter("Min", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("Lag", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("Speed", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
public static DreamValue NativeProc_walk_to(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
if (!bundle.GetArgument(0, "Ref").TryGetValueAsDreamObject<DreamObjectMovable>(out var refAtom))
return DreamValue.Null;

if (!bundle.GetArgument(1, "Trg").TryGetValueAsDreamObject<DreamObjectAtom>(out var trgAtom)) {
bundle.WalkManager.StopWalks(refAtom);
return DreamValue.Null;
}

bundle.GetArgument(2, "Min").TryGetValueAsInteger(out var min);
bundle.GetArgument(3, "Lag").TryGetValueAsInteger(out var lag);
bundle.GetArgument(4, "Speed").TryGetValueAsInteger(out var speed);

bundle.WalkManager.StartWalkTo(refAtom, trgAtom, min, lag, speed);
return DreamValue.Null;
}

[DreamProc("winclone")]
[DreamProcParameter("player", Type = DreamValueTypeFlag.DreamObject)]
[DreamProcParameter("window_name", Type = DreamValueTypeFlag.String)]
Expand Down
36 changes: 36 additions & 0 deletions OpenDreamRuntime/WalkManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using System.Threading;
using OpenDreamRuntime.Map;
using OpenDreamRuntime.Objects.Types;
Expand Down Expand Up @@ -111,4 +112,39 @@ public void StartWalkTowards(DreamObjectMovable movable, DreamObjectAtom target,
return DreamValue.Null;
});
}

/// <summary>
/// Walk towards the target with pathfinding taken into account
/// </summary>
public void StartWalkTo(DreamObjectMovable movable, DreamObjectAtom target, int min, int lag, int speed) { // TODO: Implement speed. Speed=0 uses Ref.step_size
StopWalks(movable);

lag = Math.Max(lag, 1); // Minimum of 1 tick lag

CancellationTokenSource cancelSource = new();
_walkTasks[movable] = cancelSource;

DreamThread.Run($"walk_to {movable}", async state => {
var moveProc = movable.GetProc("Move");

while (true) {
await _scheduler.CreateDelayTicks(lag);
if (cancelSource.IsCancellationRequested)
break;

var currentLoc = _atomManager.GetAtomPosition(movable);
var targetLoc = _atomManager.GetAtomPosition(target);
var steps = _dreamMapManager.CalculateSteps(currentLoc, targetLoc, min);
using var enumerator = steps.GetEnumerator();
if (!enumerator.MoveNext()) // No more steps to take
break;

var dir = enumerator.Current;
var newLoc = DreamProcNativeHelpers.GetStep(_atomManager, _dreamMapManager, movable, dir);
await state.Call(moveProc, movable, null, new(newLoc), new((int)dir));
}

return DreamValue.Null;
});
}
}
3 changes: 3 additions & 0 deletions OpenDreamShared/Network/Messages/MsgSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public enum FormatType : byte {

public ushort Channel;
public ushort Volume;
public float Offset;
public int? ResourceId;
public FormatType? Format; // TODO: This should probably be sent along with the sound resource instead somehow
//TODO: Frequency and friends

public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer serializer) {
Channel = buffer.ReadUInt16();
Volume = buffer.ReadUInt16();
Offset = buffer.ReadFloat();

if (buffer.ReadBoolean()) {
ResourceId = buffer.ReadInt32();
Expand All @@ -31,6 +33,7 @@ public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer
public override void WriteToBuffer(NetOutgoingMessage buffer, IRobustSerializer serializer) {
buffer.Write(Channel);
buffer.Write(Volume);
buffer.Write(Offset);

buffer.Write(ResourceId != null);
if (ResourceId != null) {
Expand Down

0 comments on commit 9361676

Please sign in to comment.