Skip to content

Commit

Permalink
Merge pull request #44 from cyb3rpsych0s1s/chore/switch-to-callbacks
Browse files Browse the repository at this point in the history
Chore/switch to callbacks
  • Loading branch information
Roms1383 authored Feb 22, 2023
2 parents 1c800b6 + 28c477b commit d380641
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 107 deletions.
14 changes: 6 additions & 8 deletions scripts/Addicted/Definitions.reds
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Addicted
import Addicted.Helper
import Addicted.Helpers.{Generic,Translations}

public abstract class HintRequest extends ScriptableSystemRequest {
public abstract class Hint {
// game timestamp where to stop at
protected let until: Float;
protected let times: Int32;
Expand Down Expand Up @@ -39,7 +39,7 @@ public abstract class HintRequest extends ScriptableSystemRequest {
}

// hint for inhalers
public class CoughingRequest extends HintRequest {
public class CoughingHint extends Hint {
public func Onomatopea() -> Onomatopea { return Onomatopea.Cough; }
public func Sound() -> CName {
if EnumInt(this.threshold) == EnumInt(Threshold.Severely) {
Expand All @@ -49,7 +49,7 @@ public class CoughingRequest extends HintRequest {
}
}
// hint for pills
public class VomitingRequest extends HintRequest {
public class VomitingHint extends Hint {
public func Onomatopea() -> Onomatopea { return Onomatopea.Vomit; }
public func Sound() -> CName {
if EnumInt(this.threshold) == EnumInt(Threshold.Severely) {
Expand All @@ -59,7 +59,7 @@ public class VomitingRequest extends HintRequest {
}
}
// hint for injectors
public class AchingRequest extends HintRequest {
public class AchingHint extends Hint {
public func Onomatopea() -> Onomatopea { return Onomatopea.Ache; }
public func Sound() -> CName {
if EnumInt(this.threshold) == EnumInt(Threshold.Severely) {
Expand All @@ -69,7 +69,7 @@ public class AchingRequest extends HintRequest {
}
}
// hint for anabolics
public class BreatheringRequest extends HintRequest {
public class BreatheringHint extends Hint {
public func Onomatopea() -> Onomatopea { return Onomatopea.Breather; }
public func Sound() -> CName {
if EnumInt(this.threshold) == EnumInt(Threshold.Severely) {
Expand All @@ -79,7 +79,7 @@ public class BreatheringRequest extends HintRequest {
}
}
// hint for memory booster
public class HeadAchingRequest extends HintRequest {
public class HeadAchingHint extends Hint {
public func Onomatopea() -> Onomatopea { return Onomatopea.Headache; }
public func Sound() -> CName {
return n"q101_sc_03_heart_loop";
Expand All @@ -93,8 +93,6 @@ public class HeadAchingRequest extends HintRequest {
}
}

public class UpdateWithdrawalSymptomsRequest extends ScriptableSystemRequest {}

public class Consumptions {
private persistent let keys: array<TweakDBID>;
private persistent let values: array<ref<Consumption>>;
Expand Down
28 changes: 14 additions & 14 deletions scripts/Addicted/Helper.reds
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ public class Helper {
];
}
public static func AppropriateHintRequest(id: TweakDBID, threshold: Threshold, now: Float) -> ref<HintRequest> {
public static func AppropriateHint(id: TweakDBID, threshold: Threshold, now: Float) -> ref<Hint> {
if Helper.IsSerious(threshold) {
let request: ref<HintRequest>;
let hint: ref<Hint>;
if Generic.IsInhaler(id) {
request = new CoughingRequest();
hint = new CoughingHint();
}
// anabolic are also pills, but the opposite isn't true
let anabolic = Generic.IsAnabolic(id);
Expand All @@ -173,25 +173,25 @@ public class Helper {
}
if anabolic {
if above {
request = new VomitingRequest();
hint = new VomitingHint();
}
request = new BreatheringRequest();
hint = new BreatheringHint();
} else {
if above {
request = new VomitingRequest();
hint = new VomitingHint();
}
request = new HeadAchingRequest();
hint = new HeadAchingHint();
}
}
if Generic.IsInjector(id) {
request = new AchingRequest();
hint = new AchingHint();
}
request.threshold = threshold;
let randtime = request.RandTime();
request.until = now + request.RandTime();
request.times = request.InitialTimes();
E(s"packing appropriate request: until \(ToString(request.until)) (randtime \(ToString(randtime))), \(ToString(request.times)) time(s), threshold \(ToString(request.threshold)) (\(TDBID.ToStringDEBUG(id)))");
return request;
hint.threshold = threshold;
let randtime = hint.RandTime();
hint.until = now + randtime;
hint.times = hint.InitialTimes();
E(s"packing appropriate hint: until \(ToString(hint.until)) (randtime \(ToString(randtime))), \(ToString(hint.times)) time(s), threshold \(ToString(hint.threshold)) (\(TDBID.ToStringDEBUG(id)))");
return hint;
}
return null;
}
Expand Down
62 changes: 22 additions & 40 deletions scripts/Addicted/System.reds
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import Addicted.*
import Addicted.Helpers.*
import Addicted.Manager.*

public class UpdateWithdrawalSymptomsCallback extends DelayCallback {
public let system: wref<AddictedSystem>;
public func Call() -> Void {
this.system.OnUpdateWithdrawalSymptoms();
}
}

public class AddictedSystem extends ScriptableSystem {
private let player: wref<PlayerPuppet>;
Expand Down Expand Up @@ -41,7 +48,9 @@ public class AddictedSystem extends ScriptableSystem {
this.timeSystem = GameInstance.GetTimeSystem(this.player.GetGame());
this.board = GameInstance.GetBlackboardSystem(this.player.GetGame()).Get(GetAllBlackboardDefs().PlayerStateMachine);
this.updateSymtomsID = this.delaySystem.DelayScriptableSystemRequest(this.GetClassName(), new UpdateWithdrawalSymptomsRequest(), 600., true);
let callback = new UpdateWithdrawalSymptomsCallback();
callback.system = this;
this.updateSymtomsID = this.delaySystem.DelayCallback(callback, 600., true);
this.stimulantManager = new StimulantManager();
this.stimulantManager.Register(this.player);
Expand Down Expand Up @@ -114,8 +123,8 @@ public class AddictedSystem extends ScriptableSystem {
return container.Get(n"Addicted.System.AddictedSystem") as AddictedSystem;
}
public func OnUpdateWithdrawalSymptomsRequest(request: ref<UpdateWithdrawalSymptomsRequest>) -> Void {
E(s"on update withdrawal symptoms request");
public func OnUpdateWithdrawalSymptoms() -> Void {
E(s"on update withdrawal symptoms");
let blackboard: ref<IBlackboard> = this.player.GetPlayerStateMachineBlackboard();
let before = blackboard.GetInt(GetAllBlackboardDefs().PlayerStateMachine.WithdrawalSymptoms);
let now: Int32 = 0;
Expand All @@ -130,9 +139,11 @@ public class AddictedSystem extends ScriptableSystem {
}
if NotEquals(this.updateSymtomsID, GetInvalidDelayID()) {
this.delaySystem.CancelDelay(this.updateSymtomsID);
this.delaySystem.CancelCallback(this.updateSymtomsID);
}
this.updateSymtomsID = this.delaySystem.DelayScriptableSystemRequest(this.GetClassName(), new UpdateWithdrawalSymptomsRequest(), 600., true);
let callback = new UpdateWithdrawalSymptomsCallback();
callback.system = this;
this.updateSymtomsID = this.delaySystem.DelayCallback(callback, 600., true);
}
public func OnConsumeItem(itemID: ItemID) -> Void {
Expand Down Expand Up @@ -233,7 +244,9 @@ public class AddictedSystem extends ScriptableSystem {
}
}
this.delaySystem.DelayScriptableSystemRequestNextFrame(this.GetClassName(), new UpdateWithdrawalSymptomsRequest());
let callback = new UpdateWithdrawalSymptomsCallback();
callback.system = this;
this.delaySystem.DelayCallbackNextFrame(callback);
}
public func OnProcessStatusEffects(actionEffects: array<wref<ObjectActionEffect_Record>>) -> array<wref<ObjectActionEffect_Record>> {
Expand Down Expand Up @@ -272,31 +285,6 @@ public class AddictedSystem extends ScriptableSystem {
this.hasMetabolicEditorEquipped = hasMetabolicEditor;
}
protected final func OnCoughingRequest(request: ref<CoughingRequest>) -> Void {
E(s"on coughing request");
this.ProcessHintRequest(request);
}
protected final func OnVomitingRequest(request: ref<VomitingRequest>) -> Void {
E(s"on vomiting request");
this.ProcessHintRequest(request);
}
protected final func OnAchingRequest(request: ref<AchingRequest>) -> Void {
E(s"on aching request");
this.ProcessHintRequest(request);
}
protected final func OnBreatheringRequest(request: ref<BreatheringRequest>) -> Void {
E(s"on breathering request");
this.ProcessHintRequest(request);
}
protected final func OnHeadAchingRequest(request: ref<HeadAchingRequest>) -> Void {
E(s"on headaching request");
this.ProcessHintRequest(request);
}
private func Consume(id: TweakDBID, amount: Int32) -> Bool {
E(s"consume");
let now = this.timeSystem.GetGameTimeStamp();
Expand Down Expand Up @@ -336,9 +324,9 @@ public class AddictedSystem extends ScriptableSystem {
s"\(ToString(averageThreshold)) addicted (consumable threshold)"
);
let now = this.timeSystem.GetGameTimeStamp();
let request = Helper.AppropriateHintRequest(id, threshold, now);
if IsDefined(request) {
this.ProcessHintRequest(request);
let hint = Helper.AppropriateHint(id, threshold, now);
if IsDefined(hint) {
this.onoManager.Hint(hint);
}
}
Expand Down Expand Up @@ -375,12 +363,6 @@ public class AddictedSystem extends ScriptableSystem {
GameInstance.GetUISystem(this.player.GetGame()).QueueEvent(event);
}
/// play an onomatopea as a hint to the player when reaching notably or severely addicted
/// also randomly reschedule if in timeframe
private func ProcessHintRequest(request: ref<HintRequest>) -> Void {
this.onoManager.Hint(request);
}
public func IsHard() -> Bool { return Equals(EnumInt(this.config.mode), EnumInt(AddictedMode.Hard)); }
public func UnderInfluence(id: TweakDBID) -> Bool {
Expand Down
Loading

0 comments on commit d380641

Please sign in to comment.