Skip to content

Commit

Permalink
Miscellaneous fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Tea committed Jul 11, 2020
1 parent 3cdac5a commit 46d85dd
Show file tree
Hide file tree
Showing 35 changed files with 638 additions and 251 deletions.
2 changes: 2 additions & 0 deletions Src/Certainties/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Active.Core{

public static action operator % (action x, action y) => _void;

public static loop operator - (action x) => loop._forever;

#if AL_OPTIMIZE
public status now => status._done;

Expand Down
2 changes: 2 additions & 0 deletions Src/Certainties/Failure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Active.Core{

public static failure operator % (failure x, failure y) => _flop;

public static loop operator + (failure x) => loop._forever;

#if AL_OPTIMIZE
public status fail => status._fail;

Expand Down
9 changes: 3 additions & 6 deletions Src/Conditional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ public abstract partial class Conditional : AbstractDecorator{
public abstract void OnStatus(status s);

protected Gate done(ValidString reason=null){
#if !AL_OPTIMIZE
logData = new LogData(this, ".", reason);
#endif
return new Gate(this);
return new Gate(this, new LogData(this, ".", reason));
}

protected Gate? fail(ValidString reason=null){
#if !AL_OPTIMIZE
#if !AL_OPTIMIZE
logData = new LogData(this, target, reason);
#endif
#endif
return null;
}

Expand Down
5 changes: 3 additions & 2 deletions Src/Decorators/Cooldown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ public class Cooldown: Conditional, Conditional.OptionalArguments{
static int uid; internal static int id => uid = ID(uid);

public float duration = 1f;
float stamp = System.Single.MinValue;
internal float stamp = System.Single.MinValue;

public Cooldown(){}
public Cooldown(float duration){ this.duration = duration; }

public static implicit operator Cooldown(float val) => new Cooldown(val);
public static implicit operator Cooldown(float val)
=> new Cooldown(val);

public Gate? pass => this[duration];

Expand Down
21 changes: 14 additions & 7 deletions Src/Decorators/Delay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,35 @@

using static Active.Core.status;
using Tag = System.Runtime.CompilerServices.CallerLineNumberAttribute;
using Active.Core.Details;

namespace Active.Core{
[System.Serializable]
public class Delay: Waiter, Waiter.OptionalArguments{

static int uid; internal static int id => uid = ID(uid);

public float duration = 1f;
float elapsed = 0;
internal float elapsed = 0;
int frame = 0;

public Delay(){}
public Delay(float duration){ this.duration = duration; }

public static implicit operator Delay(float val) => new Delay(val);
public static implicit operator Delay(float val)
=> new Delay(val);

public Gate? pass => this[duration];

override public void OnStatus(status s){ if(!s.running) elapsed = 0; }
override public void OnStatus(status s){
if(!s.running) elapsed = 0;
}

public Gate? this[float duration]
=> ((elapsed += UnityEngine.Time.deltaTime) >= duration)
? done(log && $"[0.0]") : cont(log && $"[{duration-elapsed:0.0}]");
public Gate? this[float duration]{ get{
RoR.OnResume(ref frame, Reset);
return ((elapsed += UnityEngine.Time.deltaTime) >= duration)
? done(log && $"[0.0]")
: cont(log && $"[{duration-elapsed:0.0}]");
}}

override public action Reset(){ elapsed = 0; return @void(); }

Expand Down
50 changes: 50 additions & 0 deletions Src/Decorators/FrameDelay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Doc/Reference/Decorators.md
#if UNITY_2018_1_OR_NEWER

using static Active.Core.status;
using Tag = System.Runtime.CompilerServices.CallerLineNumberAttribute;
using Active.Core.Details;

namespace Active.Core{
[System.Serializable]
public class FrameDelay: Waiter, Waiter.OptionalArguments{

static int uid; internal static int id => uid = ID(uid);

public int duration = 1;
int elapsed = 0;
int frame = 0;

public FrameDelay(){}
public FrameDelay(int duration){ this.duration = duration; }

public static implicit operator FrameDelay(int val)
=> new FrameDelay(val);

public Gate? pass => this[duration];

override public void OnStatus(status s){
if(!s.running) elapsed = 0;
}

public Gate? this[int duration]{ get{
RoR.OnResume(ref frame, Reset);
return ( ++elapsed > duration) ? done(log && $"[0]")
: cont(log && $"[{duration-elapsed}]");
}}

override public action Reset(){ elapsed = 0; return @void(); }

}

#if !AL_BEST_PERF
partial class Task{
public Waiter.Gate? AfterFrames(int frames, [Tag] int key = -1)
=> store.Decorator<FrameDelay>(key,
Active.Core.FrameDelay.id)[frames];
}
#endif

} // Active.Core

#endif // UNITY REQUIRED
9 changes: 6 additions & 3 deletions Src/Decorators/InOut.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// Doc/Reference/Decorators.md
using static Active.Core.status;
using Tag = System.Runtime.CompilerServices.CallerLineNumberAttribute;
using Active.Core.Details;

namespace Active.Core{
public class InOut : Conditional{

static int uid; internal static int id => uid = ID(uid);

bool passing;
int frame;

public Gate? this[bool @in, bool @out]
=> (passing = passing ? !@out : @in) ? done() : fail();
public Gate? this[bool @in, bool @out]{ get{
RoR.OnResume(ref frame, Reset);
return (passing = passing ? !@out : @in) ? done() : fail();
}}

override public void OnStatus(status s) => OnStatus(s.running);

Expand Down
20 changes: 15 additions & 5 deletions Src/Decorators/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,33 @@
using static Active.Core.status;
using InvOp = System.InvalidOperationException;
using Tag = System.Runtime.CompilerServices.CallerLineNumberAttribute;
using Active.Core.Details;

namespace Active.Core{
public class Init : AbstractDecorator{

static int uid; internal static int id => uid = ID(uid);

static Init current;
public bool passing = true;
int frame;

// TODO: RoR needs testing
// NOTE: unlike other decorators, it appears that the entry
// point for Init is the `pass` getter. Notice how this[]
// returns Gate vs Gate? (check cPatrol.cs for an example)
public Init pass{ get{
if(current != null)
{ current = null; throw new InvOp("Unclosed init detected"); }
RoR.OnResume(ref frame, Reset);
if(current != null){
current = null;
throw new InvOp("Unclosed init detected");
}
current = this; return passing ? this : null;
}}

public Gate this[object x]
{ get{ passing = false; return new Gate(this); }}
public Gate this[object x]{ get{
passing = false;
return new Gate(this);
}}

override public action Reset(){ passing = true; return @void(); }

Expand Down
43 changes: 23 additions & 20 deletions Src/Decorators/Interval.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Doc/Reference/Decorators.md
using System;
using static System.Math;
using static Active.Core.status;
using Tag = System.Runtime.CompilerServices.CallerLineNumberAttribute;

Expand All @@ -11,47 +12,49 @@ public class Interval : Waiter, Waiter.OptionalArguments{

public bool catchup = false;
public float offset = 0, period = 1;
internal float stamp = System.Single.MinValue;
internal float stamp; // Next time i'val will fire, minus offset

public Interval(){}
public Interval() => stamp = time;

public Interval(bool fireOnStart){ if(!fireOnStart) stamp = 0; }

public Interval(float period, float offset=0f, bool fireOnStart=true){
public Interval(float period, float offset=0f){
this.period = period;
this.offset = offset;
if(!fireOnStart) stamp = 0;
stamp = time;
}

public float due => stamp + period + offset;
public float due => stamp + offset;

public static implicit operator Interval(float val) => new Interval(val);
public static implicit operator Interval(float val)
=> new Interval(val);

public Gate? pass => this[period];

public Gate? this[float s]{get{
if(time >= stamp + s + offset){
if(stamp == System.Single.MinValue || s == 0) stamp = time;
else if(catchup){
stamp += s;
}else{
int n = (int)Math.Floor((time - stamp) / s) + 1;
stamp += s * n;
}
public Gate? this[float s]{ get{
if(s == 0f){
stamp = time;
return done(log && "[○{bypass}]");
}
if(time >= stamp + offset){
stamp += catchup ? s : EvalInc(s, offset, stamp, time);
return done(log && $"[○{stamp:0.00}]");
}else return cont(log && $"[{time:0.00}{stamp+s+offset:0.00}]");
}else
return cont(log && $"[{time:0.00}{stamp+s+offset:0.00}]");
}}

override public action Reset(){ stamp = 0; return @void(); }
override public action Reset(){ stamp = time; return @void(); }

override public void OnStatus(status s){}

// period, offset, stamp, time
static internal float EvalInc(float p, float o, float s, float t)
=> (float)(Floor((t - s - o) / p) + 1) * p;

}

#if !AL_BEST_PERF
partial class Task{
public Waiter.Gate? Every(float delay, float offset = 0f,
[Tag] int key = -1)
[Tag] int key = -1)
=> store.Decorator<Interval>(key, Active.Core.Interval.id)[delay];
}
#endif
Expand Down
8 changes: 6 additions & 2 deletions Src/Decorators/Latch.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// Doc/Reference/Decorators.md
using static Active.Core.status;
using Tag = System.Runtime.CompilerServices.CallerLineNumberAttribute;
using Active.Core.Details;

namespace Active.Core{
public class Latch : Conditional{

static int uid; internal static int id => uid = ID(uid);

public bool passing;
int frame;

public Gate? this[bool cond] => (passing |= cond) ? done() : fail();
public Gate? this[bool cond]{ get{
RoR.OnResume(ref frame, Reset);
return (passing |= cond) ? done() : fail();
}}

override public void OnStatus(status s) => OnStatus(s.running);

Expand Down
8 changes: 6 additions & 2 deletions Src/Decorators/Once.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// Doc/Reference/Decorators.md
using static Active.Core.status;
using Tag = System.Runtime.CompilerServices.CallerLineNumberAttribute;
using Active.Core.Details;

namespace Active.Core{
public class Once : Conditional, Conditional.OptionalArguments{

static int uid; internal static int id => uid = ID(uid);

bool passing = true;
int frame = 0;

public Gate? pass => passing ? done() : fail();
public Gate? pass{ get{
RoR.OnResume(ref frame, Reset); // TODO tests
return passing ? done() : fail();
}}

override public void OnStatus(status s) => passing &= s.running;

Expand Down
32 changes: 22 additions & 10 deletions Src/Decorators/Timeout.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
// Doc/Reference/Decorators.md
using static Active.Core.status;
using Tag = System.Runtime.CompilerServices.CallerLineNumberAttribute;
using Active.Core.Details;

namespace Active.Core{
[System.Serializable]
public class Timeout : Conditional, Conditional.OptionalArguments{

static int uid; internal static int id => uid = ID(uid);

public float duration = 1;
float stamp;
int frame;

bool enabled => stamp != -1;

public Timeout(){}
public Timeout(float duration) => this.duration = duration;
public Timeout() => stamp = time;

public Timeout(float duration)
{ stamp = time; this.duration = duration; }

public float due => stamp + duration;

public static implicit operator Timeout(float val) => new Timeout(val);
public static implicit operator Timeout(float val)
=> new Timeout(val);

public Gate? pass => this[duration];

public Gate? this[float s]
=> enabled ? time < stamp + s ? done(log && $"[{s + stamp - time:0.#}]")
: fail(log && $"timed out ({s}s)")
: done(log && "idle");
// TODO RoR untested
public Gate? this[float s]{ get{
RoR.OnResume(ref frame, Reset);
return enabled ? time < stamp + s
? done(log && $"[{s + stamp - time:0.#}]")
: fail(log && $"timed out ({s}s)")
: done(log && "idle");
}}

override public void OnStatus(status s) => OnStatus(s.running);

Expand All @@ -36,8 +46,10 @@ void OnStatus(bool running)

#if !AL_BEST_PERF
partial class Task{
public Conditional.Gate? Timeout(float duration, [Tag] int key = -1)
=> store.Decorator<Timeout>(key, Active.Core.Timeout.id)[duration];
public Conditional.Gate? Timeout(float duration,
[Tag] int key = -1)
=> store.Decorator<Timeout>(
key, Active.Core.Timeout.id)[duration];
}
#endif

Expand Down

0 comments on commit 46d85dd

Please sign in to comment.