From df25fc48fe89cee695a5b739c7d5d4adeb169b2c Mon Sep 17 00:00:00 2001
From: shangfengh <3495281661@qq.com>
Date: Wed, 21 Jun 2023 09:25:49 +0800
Subject: [PATCH 1/9] feat: :sparkles: add a new SafaValues,
IntWithVariableRange
---
logic/Preparation/Utility/SafeValue.cs | 102 ++++++++++++++++++++++++-
1 file changed, 101 insertions(+), 1 deletion(-)
diff --git a/logic/Preparation/Utility/SafeValue.cs b/logic/Preparation/Utility/SafeValue.cs
index 90b1f26b..8be16d22 100644
--- a/logic/Preparation/Utility/SafeValue.cs
+++ b/logic/Preparation/Utility/SafeValue.cs
@@ -49,6 +49,10 @@ public bool TrySet(bool value)
public bool And(bool x) => Interlocked.And(ref v, x ? 1 : 0) != 0;
public bool Or(bool x) => Interlocked.Or(ref v, x ? 1 : 0) != 0;
}
+ ///
+ /// 一个能记录Start后完成多少进度的进度条(int),
+ /// 只允许Start时修改needTime,支持TryStop使未完成的进度条清零,支持Set0使进度条强制清零
+ ///
public struct IntProgressContinuously
{
private long endT = long.MaxValue;
@@ -61,12 +65,22 @@ public IntProgressContinuously(long needTime)
public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2);
public override string ToString() => "EndTime:" + Interlocked.CompareExchange(ref endT, -2, -2).ToString() + " ms, NeedTime:" + Interlocked.CompareExchange(ref needT, -2, -2).ToString() + " ms";
+ public bool IsFinished()
+ {
+ return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
+ }
+ ///
+ /// GetProgress<0则表明未开始
+ ///
public long GetProgress()
{
long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
}
+ ///
+ /// GetProgressDouble<0则表明未开始
+ ///
public double GetProgressDouble()
{
long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
@@ -94,6 +108,92 @@ public void TryStop()
if (Environment.TickCount64 < Interlocked.CompareExchange(ref endT, -2, -2))
Interlocked.Exchange(ref endT, long.MaxValue);
}
- //增加新的写操作可能导致不安全
+ //增加其他新的写操作可能导致不安全
+ }
+ ///
+ /// 一个保证在[0,maxValue]的可变int,支持可变的maxValue(请确保大于0)
+ ///
+ public struct IntWithVariableRange
+ {
+ private int v;
+ private int maxV;
+ private readonly object vLock = new();
+ public IntWithVariableRange(int value, int maxValue)
+ {
+ if (maxValue <= 0) Debugger.Output("Bug:IntWithVariableRange.maxValue (" + maxValue.ToString() + ") is less than 0.");
+ v = value < maxValue ? value : maxValue;
+ this.maxV = maxValue;
+ }
+ public override string ToString()
+ {
+ lock (vLock)
+ {
+ return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString();
+ }
+ }
+ public int GetValue() { lock (vLock) return v; }
+ public int GetMaxV() { lock (vLock) return maxV; }
+
+ public void SetMaxV(int maxValue)
+ {
+ lock (vLock)
+ {
+ if (maxValue <= 0) Debugger.Output("Bug:IntWithVariableRange.maxValue (" + maxValue.ToString() + ") is less than 0.");
+ maxV = maxValue;
+ if (v > maxValue) v = maxValue;
+ }
+ }
+ ///
+ /// 应当保证该value>=0
+ ///
+ public int SetPositiveV(int value)
+ {
+ lock (vLock)
+ {
+ return v = (value > maxV) ? maxV : value;
+ }
+ }
+ public int SetV(int value)
+ {
+ lock (vLock)
+ {
+ if (value <= 0) return v = 0;
+ return v = (value > maxV) ? maxV : value;
+ }
+ }
+ public int AddV(int addV)
+ {
+ lock (vLock)
+ {
+ v += addV;
+ if (v < 0) return v = 0;
+ if (v > maxV) return v = maxV;
+ return v;
+ }
+ }
+ ///
+ /// 应当保证该增加值大于0
+ ///
+ public int AddPositiveV(int addPositiveV)
+ {
+ lock (vLock)
+ {
+ v += addPositiveV;
+ if (v > maxV) return v = maxV;
+ return v;
+ }
+ }
+ ///
+ /// 应当保证该减少值大于0
+ ///
+ public int SubPositiveV(int subPositiveV)
+ {
+ lock (vLock)
+ {
+ v += subPositiveV;
+ if (v < 0) return v = 0;
+ return v;
+ }
+ }
}
}
From 4434522aef1fa4af7ae878810ea06d9cca8b55f7 Mon Sep 17 00:00:00 2001
From: shangfengh <3495281661@qq.com>
Date: Wed, 21 Jun 2023 11:23:46 +0800
Subject: [PATCH 2/9] feat: :sparkles: add a new SafeValues, IntNumUpdateByCD
---
logic/Preparation/Utility/SafeValue.cs | 199 ++++++++++++++++++++++++-
1 file changed, 193 insertions(+), 6 deletions(-)
diff --git a/logic/Preparation/Utility/SafeValue.cs b/logic/Preparation/Utility/SafeValue.cs
index 8be16d22..5a2f62cc 100644
--- a/logic/Preparation/Utility/SafeValue.cs
+++ b/logic/Preparation/Utility/SafeValue.cs
@@ -1,5 +1,7 @@
using System;
+using System.Security.Cryptography.X509Certificates;
using System.Threading;
+using System.Timers;
namespace Preparation.Utility
{
@@ -49,9 +51,12 @@ public bool TrySet(bool value)
public bool And(bool x) => Interlocked.And(ref v, x ? 1 : 0) != 0;
public bool Or(bool x) => Interlocked.Or(ref v, x ? 1 : 0) != 0;
}
+
///
/// 一个能记录Start后完成多少进度的进度条(int),
- /// 只允许Start时修改needTime,支持TryStop使未完成的进度条清零,支持Set0使进度条强制清零
+ /// 只允许Start时修改needTime(请确保大于0);
+ /// 支持TrySet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
+ /// 不支持暂停
///
public struct IntProgressContinuously
{
@@ -60,6 +65,7 @@ public struct IntProgressContinuously
public IntProgressContinuously(long needTime)
{
+ if (needTime <= 0) Debugger.Output("Bug:IntProgressContinuously.needTime (" + needTime.ToString() + ") is less than 0.");
this.needT = needTime;
}
public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
@@ -90,6 +96,11 @@ public double GetProgressDouble()
public bool Start(long needTime)
{
+ if (needTime <= 0)
+ {
+ Debugger.Output("Warning:Start IntProgressContinuously with the needTime (" + needTime.ToString() + ") which is less than 0.");
+ return false;
+ }
//规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
if (needTime <= 2) Debugger.Output("Warning:the field of IntProgressContinuously is " + needTime.ToString() + ",which is too small.");
@@ -103,13 +114,18 @@ public bool Start()
return true;
}
public void Set0() => Interlocked.Exchange(ref endT, long.MaxValue);
- public void TryStop()
+ public bool TrySet0()
{
if (Environment.TickCount64 < Interlocked.CompareExchange(ref endT, -2, -2))
+ {
Interlocked.Exchange(ref endT, long.MaxValue);
+ return true;
+ }
+ return false;
}
//增加其他新的写操作可能导致不安全
}
+
///
/// 一个保证在[0,maxValue]的可变int,支持可变的maxValue(请确保大于0)
///
@@ -120,7 +136,11 @@ public struct IntWithVariableRange
private readonly object vLock = new();
public IntWithVariableRange(int value, int maxValue)
{
- if (maxValue <= 0) Debugger.Output("Bug:IntWithVariableRange.maxValue (" + maxValue.ToString() + ") is less than 0.");
+ if (maxValue < 0)
+ {
+ Debugger.Output("Warning:Try to set IntWithVariableRange.maxValue to " + maxValue.ToString() + ".");
+ maxValue = 0;
+ }
v = value < maxValue ? value : maxValue;
this.maxV = maxValue;
}
@@ -134,11 +154,26 @@ public override string ToString()
public int GetValue() { lock (vLock) return v; }
public int GetMaxV() { lock (vLock) return maxV; }
- public void SetMaxV(int maxValue)
+ ///
+ /// 若maxValue<=0则maxValue设为0并返回False
+ ///
+ public bool SetMaxV(int maxValue)
+ {
+ if (maxValue < 0) maxValue = 0;
+ lock (vLock)
+ {
+ maxV = maxValue;
+ if (v > maxValue) v = maxValue;
+ }
+ return maxValue > 0;
+ }
+ ///
+ /// 应当保证该maxValue>=0
+ ///
+ public void SetPositiveMaxV(int maxValue)
{
lock (vLock)
{
- if (maxValue <= 0) Debugger.Output("Bug:IntWithVariableRange.maxValue (" + maxValue.ToString() + ") is less than 0.");
maxV = maxValue;
if (v > maxValue) v = maxValue;
}
@@ -155,9 +190,9 @@ public int SetPositiveV(int value)
}
public int SetV(int value)
{
+ if (value < 0) value = 0;
lock (vLock)
{
- if (value <= 0) return v = 0;
return v = (value > maxV) ? maxV : value;
}
}
@@ -196,4 +231,156 @@ public int SubPositiveV(int subPositiveV)
}
}
}
+
+ ///
+ /// 一个保证在[0,maxNum],每CDms自动更新的可变int,支持可变的CD、maxNum(请确保大于0)
+ ///
+ public struct IntNumUpdateByCD
+ {
+ private int num;
+ private int maxNum;
+ private int cd;
+ private long updateTime = 0;
+ private object numLock = new();
+ public IntNumUpdateByCD(int num, int maxNum, int cd)
+ {
+ if (num < 0) Debugger.Output("Bug:IntNumUpdateByCD.num (" + num.ToString() + ") is less than 0.");
+ if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
+ if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
+ this.num = num;
+ this.maxNum = maxNum;
+ this.cd = cd;
+ this.updateTime = Environment.TickCount64;
+ }
+ public IntNumUpdateByCD(int maxNum, int cd)
+ {
+ if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
+ if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
+ this.num = this.maxNum = maxNum;
+ this.cd = cd;
+ }
+ public int GetMaxNum() { lock (numLock) return maxNum; }
+ public int GetCD() { lock (numLock) return cd; }
+ public int GetNum(long time)
+ {
+ lock (numLock)
+ {
+ if (num < maxNum && time - updateTime >= cd)
+ {
+ int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
+ updateTime += add * cd;
+ return (num += add);
+ }
+ return num;
+ }
+ }
+
+ ///
+ /// 应当保证该subV>=0
+ ///
+ public int TrySub(int subV)
+ {
+ if (subV < 0) Debugger.Output("Bug:IntNumUpdateByCD Try to sub " + subV.ToString() + ", which is less than 0.");
+ long time = Environment.TickCount64;
+ lock (numLock)
+ {
+ if (num < maxNum && time - updateTime >= cd)
+ {
+ int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
+ updateTime += add * cd;
+ num += add;
+ }
+ if (num == maxNum) updateTime = time;
+ num -= subV = Math.Min(subV, num);
+ }
+ return subV;
+ }
+ ///
+ /// 应当保证该addV>=0
+ ///
+ public void TryAdd(int addV)
+ {
+ if (addV < 0) Debugger.Output("Bug:IntNumUpdateByCD Try to add " + addV.ToString() + ", which is less than 0.");
+ lock (numLock)
+ {
+ num += Math.Min(addV, maxNum - num);
+ }
+ }
+ ///
+ /// 若maxNum<=0则maxNum及Num设为0并返回False
+ ///
+ public bool SetMaxNumAndNum(int maxNum)
+ {
+ if (maxNum < 0) maxNum = 0;
+ lock (numLock)
+ {
+ this.num = this.maxNum = maxNum;
+ }
+ return maxNum > 0;
+ }
+ ///
+ /// 应当保证该maxnum>=0
+ ///
+ public void SetPositiveMaxNumAndNum(int maxNum)
+ {
+ lock (numLock)
+ {
+ this.num = this.maxNum = maxNum;
+ }
+ }
+ ///
+ /// 应当保证该maxnum>=0
+ ///
+ public void SetPositiveMaxNum(int maxNum)
+ {
+ lock (numLock)
+ {
+ if ((this.maxNum = maxNum) < num)
+ num = maxNum;
+ }
+ }
+ ///
+ /// 若maxNum<=0则maxNum及Num设为0并返回False
+ ///
+ public bool SetMaxNum(int maxNum)
+ {
+ if (maxNum < 0) maxNum = 0;
+ lock (numLock)
+ {
+ if ((this.maxNum = maxNum) < num)
+ num = maxNum;
+ }
+ return maxNum > 0;
+ }
+ ///
+ /// 若num<0则num设为0并返回False
+ ///
+ public bool SetNum(int num)
+ {
+ lock (numLock)
+ {
+ if (num < 0) { this.num = 0; return false; }
+ this.num = num;
+ return true;
+ }
+ }
+ ///
+ /// 应当保证该num>=0
+ ///
+ public void SetPositiveNum(int num)
+ {
+ lock (numLock)
+ {
+ this.num = num;
+ }
+ }
+ public void SetCD(int cd)
+ {
+ lock (numLock)
+ {
+ if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateByCD.cd to " + cd.ToString() + ".");
+ this.cd = cd;
+ }
+ }
+ }
}
From 53436fa47e03b54db815908701d54ade52971357 Mon Sep 17 00:00:00 2001
From: shangfengh <3495281661@qq.com>
Date: Wed, 21 Jun 2023 11:34:28 +0800
Subject: [PATCH 3/9] refactor: :art: make BulletNum IntNumUpdateByCD
---
.../GameClass/GameObj/Character/Character.cs | 57 +++----------------
logic/Gaming/AttackManager.cs | 2 +-
logic/Preparation/Interface/ICharacter.cs | 5 +-
logic/Preparation/Utility/SafeValue.cs | 2 -
4 files changed, 10 insertions(+), 56 deletions(-)
diff --git a/logic/GameClass/GameObj/Character/Character.cs b/logic/GameClass/GameObj/Character/Character.cs
index 9ce07bcc..c76d28a5 100644
--- a/logic/GameClass/GameObj/Character/Character.cs
+++ b/logic/GameClass/GameObj/Character/Character.cs
@@ -10,20 +10,7 @@ public partial class Character : Moveable, ICharacter // 负责人LHR摆烂终
{
#region 装弹、攻击相关的基本属性及方法
private readonly object attackLock = new();
- ///
- /// 装弹冷却
- ///
- protected int cd;
- public int CD
- {
- get
- {
- lock (attackLock)
- {
- return cd;
- }
- }
- }
+ public IntNumUpdateByCD BulletNum { get; }
private int orgCD;
public int OrgCD
{
@@ -50,38 +37,11 @@ public BulletType BulletOfPlayer
lock (attackLock)
{
bulletOfPlayer = value;
- cd = orgCD = (BulletFactory.BulletCD(value));
- Debugger.Output(this, string.Format("'s CD has been set to: {0}.", cd));
- maxBulletNum = bulletNum = (BulletFactory.BulletNum(value));
- }
- }
- }
-
- protected int maxBulletNum;
- public int MaxBulletNum
- {
- get
- {
- lock (attackLock)
- {
- return maxBulletNum;
- }
- }
- }
- private int bulletNum;
- private int updateTimeOfBulletNum = 0;
-
- public int UpdateBulletNum(int time)//通过该函数获取真正的bulletNum
- {
- lock (attackLock)
- {
- if (bulletNum < maxBulletNum && time - updateTimeOfBulletNum >= cd)
- {
- int add = Math.Min(maxBulletNum - bulletNum, (time - updateTimeOfBulletNum) / cd);
- updateTimeOfBulletNum += add * cd;
- return (bulletNum += add);
+ orgCD = (BulletFactory.BulletCD(value));
+ BulletNum.SetCD(orgCD);
+ Debugger.Output(this, string.Format("'s CD has been set to: {0}.", orgCD));
+ BulletNum.SetPositiveMaxNumAndNum(BulletFactory.BulletNum(value));
}
- return bulletNum;
}
}
@@ -89,17 +49,14 @@ public int UpdateBulletNum(int time)//通过该函数获取真正的bulletNum
/// 进行一次攻击
///
/// 攻击操作发出的子弹
- public Bullet? Attack(double angle, int time)
+ public Bullet? Attack(double angle)
{
lock (attackLock)
{
if (bulletOfPlayer == BulletType.Null)
return null;
- if (UpdateBulletNum(time) > 0)
+ if (BulletNum.TrySub(1) == 1)
{
- if (bulletNum == maxBulletNum) updateTimeOfBulletNum = time;
- --bulletNum;
-
XY res = Position + new XY // 子弹紧贴人物生成。
(
(int)(Math.Abs((Radius + BulletFactory.BulletRadius(bulletOfPlayer)) * Math.Cos(angle))) * Math.Sign(Math.Cos(angle)),
diff --git a/logic/Gaming/AttackManager.cs b/logic/Gaming/AttackManager.cs
index ac7aea7a..2204cf61 100644
--- a/logic/Gaming/AttackManager.cs
+++ b/logic/Gaming/AttackManager.cs
@@ -203,7 +203,7 @@ public bool Attack(Character player, double angle)
{ // 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
if (!player.Commandable()) return false;
- Bullet? bullet = player.Attack(angle, gameMap.Timer.nowTime());
+ Bullet? bullet = player.Attack(angle);
if (bullet != null)
{
diff --git a/logic/Preparation/Interface/ICharacter.cs b/logic/Preparation/Interface/ICharacter.cs
index 07f0b938..cbe87ab4 100644
--- a/logic/Preparation/Interface/ICharacter.cs
+++ b/logic/Preparation/Interface/ICharacter.cs
@@ -1,5 +1,4 @@
-using System;
-using Preparation.Utility;
+using Preparation.Utility;
namespace Preparation.Interface
{
@@ -15,7 +14,7 @@ public interface ICharacter : IMoveable
public BulletType BulletOfPlayer { get; set; }
public CharacterType CharacterType { get; }
public ActiveSkill FindActiveSkill(ActiveSkillType activeSkillType);
- public int UpdateBulletNum(int time);
+ public IntNumUpdateByCD BulletNum { get; }
public long SetPlayerState(RunningStateType running, PlayerStateType value = PlayerStateType.Null, IGameObj? obj = null);
public bool ResetPlayerState(long state, RunningStateType running = RunningStateType.Null, PlayerStateType value = PlayerStateType.Null, IGameObj? obj = null);
diff --git a/logic/Preparation/Utility/SafeValue.cs b/logic/Preparation/Utility/SafeValue.cs
index 5a2f62cc..ffda18f7 100644
--- a/logic/Preparation/Utility/SafeValue.cs
+++ b/logic/Preparation/Utility/SafeValue.cs
@@ -1,7 +1,5 @@
using System;
-using System.Security.Cryptography.X509Certificates;
using System.Threading;
-using System.Timers;
namespace Preparation.Utility
{
From 82661cc99c5a6206f3f6780f21b626cbcdd946b1 Mon Sep 17 00:00:00 2001
From: shangfengh <3495281661@qq.com>
Date: Wed, 21 Jun 2023 11:50:18 +0800
Subject: [PATCH 4/9] fix: :bug: fix the SafeValues
---
.../GameClass/GameObj/Character/Character.cs | 25 ----------------
logic/Preparation/Utility/SafeValue.cs | 30 +++++++++++--------
2 files changed, 17 insertions(+), 38 deletions(-)
diff --git a/logic/GameClass/GameObj/Character/Character.cs b/logic/GameClass/GameObj/Character/Character.cs
index c76d28a5..ee1b3957 100644
--- a/logic/GameClass/GameObj/Character/Character.cs
+++ b/logic/GameClass/GameObj/Character/Character.cs
@@ -72,31 +72,6 @@ public BulletType BulletOfPlayer
return null;
}
}
-
- /*
- ///
- /// 攻击被反弹,反弹伤害不会再被反弹
- ///
- ///
- ///
- /// 反弹伤害者
- /// 是否因反弹伤害而死
- private bool BeBounced(int subHP, bool hasSpear, Character? bouncer)
- {
- lock (beAttackedLock)
- {
- if (hp <= 0)
- return false;
- if (!(bouncer?.TeamID == this.TeamID))
- {
- if (hasSpear || !HasShield)
- _ = SubHp(subHP);
- if (hp <= 0)
- TryActivatingLIFE();
- }
- return hp <= 0;
- }
- }*/
#endregion
#region 感知相关的基本属性及方法
private readonly object bgmLock = new();
diff --git a/logic/Preparation/Utility/SafeValue.cs b/logic/Preparation/Utility/SafeValue.cs
index ffda18f7..3099e9c1 100644
--- a/logic/Preparation/Utility/SafeValue.cs
+++ b/logic/Preparation/Utility/SafeValue.cs
@@ -194,39 +194,43 @@ public int SetV(int value)
return v = (value > maxV) ? maxV : value;
}
}
+ ///
+ /// 返回实际改变量
+ ///
public int AddV(int addV)
{
lock (vLock)
{
+ int previousV = v;
v += addV;
- if (v < 0) return v = 0;
- if (v > maxV) return v = maxV;
- return v;
+ if (v < 0) v = 0;
+ if (v > maxV) v = maxV;
+ return v - previousV;
}
}
///
- /// 应当保证该增加值大于0
+ /// 应当保证该增加值大于0,返回实际改变量
///
public int AddPositiveV(int addPositiveV)
{
lock (vLock)
{
+ addPositiveV = Math.Min(addPositiveV, maxV - v);
v += addPositiveV;
- if (v > maxV) return v = maxV;
- return v;
}
+ return addPositiveV;
}
///
- /// 应当保证该减少值大于0
+ /// 应当保证该减少值大于0,返回实际改变量
///
public int SubPositiveV(int subPositiveV)
{
lock (vLock)
{
- v += subPositiveV;
- if (v < 0) return v = 0;
- return v;
+ subPositiveV = Math.Min(subPositiveV, v);
+ v -= subPositiveV;
}
+ return subPositiveV;
}
}
@@ -245,7 +249,7 @@ public IntNumUpdateByCD(int num, int maxNum, int cd)
if (num < 0) Debugger.Output("Bug:IntNumUpdateByCD.num (" + num.ToString() + ") is less than 0.");
if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
if (cd <= 0) Debugger.Output("Bug:IntNumUpdateByCD.cd (" + cd.ToString() + ") is less than 0.");
- this.num = num;
+ this.num = (num < maxNum) ? num : maxNum;
this.maxNum = maxNum;
this.cd = cd;
this.updateTime = Environment.TickCount64;
@@ -358,7 +362,7 @@ public bool SetNum(int num)
lock (numLock)
{
if (num < 0) { this.num = 0; return false; }
- this.num = num;
+ this.num = (num < maxNum) ? num : maxNum;
return true;
}
}
@@ -369,7 +373,7 @@ public void SetPositiveNum(int num)
{
lock (numLock)
{
- this.num = num;
+ this.num = (num < maxNum) ? num : maxNum;
}
}
public void SetCD(int cd)
From be8e58b178070bd3c7b1f13e6a84a6d75e9efafe Mon Sep 17 00:00:00 2001
From: shangfengh <3495281661@qq.com>
Date: Wed, 21 Jun 2023 14:08:34 +0800
Subject: [PATCH 5/9] refactor: :art: make HP a LongWithVariableRange
---
.../GameObj/Character/Character.Skill.cs | 2 +-
.../GameClass/GameObj/Character/Character.cs | 174 ++-------------
logic/Gaming/ActionManager.cs | 4 +-
logic/Gaming/CharacterManager.cs | 10 +-
logic/Gaming/PropManager.cs | 4 +-
.../SkillManager/SkillManager.ActiveSkill.cs | 8 +-
logic/Preparation/Interface/ICharacter.cs | 3 +-
logic/Preparation/Utility/SafeValue.cs | 198 +++++++++++++++++-
8 files changed, 224 insertions(+), 179 deletions(-)
diff --git a/logic/GameClass/GameObj/Character/Character.Skill.cs b/logic/GameClass/GameObj/Character/Character.Skill.cs
index 81eea6c2..69c487ac 100644
--- a/logic/GameClass/GameObj/Character/Character.Skill.cs
+++ b/logic/GameClass/GameObj/Character/Character.Skill.cs
@@ -35,7 +35,7 @@ protected Character(XY initPos, int initRadius, CharacterType characterType) :
this.score = 0;
this.buffManager = new BuffManager();
this.occupation = OccupationFactory.FindIOccupation(characterType);
- this.MaxHp = this.hp = Occupation.MaxHp;
+ this.HP = new(Occupation.MaxHp);
this.MoveSpeed.Set(this.orgMoveSpeed = Occupation.MoveSpeed);
this.BulletOfPlayer = this.OriBulletOfPlayer = Occupation.InitBullet;
this.concealment = Occupation.Concealment;
diff --git a/logic/GameClass/GameObj/Character/Character.cs b/logic/GameClass/GameObj/Character/Character.cs
index ee1b3957..5d0394dd 100644
--- a/logic/GameClass/GameObj/Character/Character.cs
+++ b/logic/GameClass/GameObj/Character/Character.cs
@@ -10,7 +10,7 @@ public partial class Character : Moveable, ICharacter // 负责人LHR摆烂终
{
#region 装弹、攻击相关的基本属性及方法
private readonly object attackLock = new();
- public IntNumUpdateByCD BulletNum { get; }
+ public IntNumUpdateByCD BulletNum { get; } = new IntNumUpdateByCD();
private int orgCD;
public int OrgCD
{
@@ -119,114 +119,7 @@ public int SpeedOfOpenChest
}
#endregion
#region 血量相关的基本属性及方法
- private readonly ReaderWriterLockSlim hpReaderWriterLock = new();
- public ReaderWriterLockSlim HPReadWriterLock => hpReaderWriterLock;
-
- private long maxHp;
- public long MaxHp
- {
- get
- {
- HPReadWriterLock.EnterReadLock();
- try
- {
- return maxHp;
- }
- finally
- {
- HPReadWriterLock.ExitReadLock();
- }
- }
- protected set
- {
- HPReadWriterLock.EnterWriteLock();
- try
- {
- maxHp = value;
- if (hp > maxHp) hp = maxHp;
- }
- finally
- {
- HPReadWriterLock.ExitWriteLock();
- }
- }
- }
- // 最大血量
- protected long hp;
- public long HP
- {
- get
- {
- HPReadWriterLock.EnterReadLock();
- try
- {
- return hp;
- }
- finally
- {
- HPReadWriterLock.ExitReadLock();
- }
- }
- }
-
- public long SetHP(long value)
- {
- HPReadWriterLock.EnterWriteLock();
- try
- {
- if (value > 0)
- {
- return hp = value <= maxHp ? value : maxHp;
- }
- else
- return hp = 0;
- }
- finally
- {
- HPReadWriterLock.ExitWriteLock();
- }
- }
-
- ///
- /// 尝试减血
- ///
- /// 减血量
- public long SubHp(long sub)
- {
- HPReadWriterLock.EnterWriteLock();
- try
- {
- long previousHp = hp;
- if (hp <= sub)
- {
- hp = 0;
- return hp;
- }
- else
- {
- hp -= sub;
- return sub;
- }
- }
- finally
- {
- HPReadWriterLock.ExitWriteLock();
- }
- }
-
- public long AddHP(long add)
- {
- HPReadWriterLock.EnterWriteLock();
- try
- {
- long previousHp = hp;
- return (hp = (hp + add > maxHp) ? maxHp : hp + add) - previousHp;
- }
- finally
- {
- HPReadWriterLock.ExitWriteLock();
- }
- }
+ public LongWithVariableRange HP { get; }
private readonly object vampireLock = new();
public object VampireLock => vampire;
@@ -254,59 +147,36 @@ public double Vampire
}
public double OriVampire { get; protected set; }
- private readonly object treatLock = new();
- private int degreeOfTreatment = 0;
+ private AtomicInt degreeOfTreatment = new(0);
public int DegreeOfTreatment
{
- get
- {
- HPReadWriterLock.EnterReadLock();
- try
- {
- return degreeOfTreatment;
- }
- finally
- {
- HPReadWriterLock.ExitReadLock();
- }
- }
+ get => degreeOfTreatment;
}
public void SetDegreeOfTreatment0()
{
- HPReadWriterLock.EnterWriteLock();
- try
- {
- degreeOfTreatment = 0;
- }
- finally
- {
- HPReadWriterLock.ExitWriteLock();
- }
+ degreeOfTreatment.Set(0);
}
public bool AddDegreeOfTreatment(int value, Student whoTreatYou)
{
- HPReadWriterLock.EnterWriteLock();
- try
+ value = degreeOfTreatment.Add(value);
+ long addV = HP.TryAddAll(value);
+ if (addV == 0)
{
- if (value >= maxHp - hp)
- {
- whoTreatYou.AddScore(GameData.StudentScoreTreat(maxHp - hp));
- hp = maxHp;
- degreeOfTreatment = 0;
- return true;
- }
- if (value >= GameData.basicTreatmentDegree)
- {
- whoTreatYou.AddScore(GameData.StudentScoreTreat(GameData.basicTreatmentDegree));
- hp += GameData.basicTreatmentDegree;
- degreeOfTreatment = 0;
- return true;
- }
- degreeOfTreatment = value;
+ SetDegreeOfTreatment0();
+ return false;
}
- finally
+ if (addV > 0)
{
- HPReadWriterLock.ExitWriteLock();
+ whoTreatYou.AddScore(GameData.StudentScoreTreat(addV));
+ SetDegreeOfTreatment0();
+ return true;
+ }
+ if (value >= GameData.basicTreatmentDegree)
+ {
+ whoTreatYou.AddScore(GameData.StudentScoreTreat(GameData.basicTreatmentDegree));
+ HP.AddPositiveV(GameData.basicTreatmentDegree);
+ SetDegreeOfTreatment0();
+ return true;
}
return false;
}
@@ -798,7 +668,7 @@ public void TryActivatingLIFE()
if (buffManager.TryActivatingLIFE())
{
AddScore(GameData.ScorePropRemainHp);
- hp = GameData.RemainHpWhenAddLife;
+ HP.SetPositiveV(GameData.RemainHpWhenAddLife);
}
}
diff --git a/logic/Gaming/ActionManager.cs b/logic/Gaming/ActionManager.cs
index e2961b6d..b8cd6053 100644
--- a/logic/Gaming/ActionManager.cs
+++ b/logic/Gaming/ActionManager.cs
@@ -199,7 +199,7 @@ public bool Treat(Student player, Student? playerTreated = null)
}
else if (!GameData.ApproachToInteract(playerTreated.Position, player.Position)) return false;
- if (playerTreated.HP == playerTreated.MaxHp) return false;
+ if (playerTreated.HP == playerTreated.HP.GetMaxV()) return false;
long stateNumTreated = playerTreated.SetPlayerState(RunningStateType.Waiting, PlayerStateType.Treated);
if (stateNumTreated == -1) return false;
@@ -321,7 +321,7 @@ public bool Rescue(Student player, Student? playerRescued = null)
if (playerRescued.AddTimeOfRescue(GameData.checkInterval))
{
playerRescued.SetPlayerStateNaturally();
- playerRescued.SetHP(playerRescued.MaxHp / 2);
+ playerRescued.HP.SetPositiveV(playerRescued.HP.GetMaxV() / 2);
player.AddScore(GameData.StudentScoreRescue);
return false;
}
diff --git a/logic/Gaming/CharacterManager.cs b/logic/Gaming/CharacterManager.cs
index 86c8fef5..182f42ce 100644
--- a/logic/Gaming/CharacterManager.cs
+++ b/logic/Gaming/CharacterManager.cs
@@ -322,10 +322,10 @@ public void BeAttacked(Student student, Bullet bullet)
{
if (bullet.HasSpear)
{
- long subHp = student.SubHp(bullet.AP);
+ long subHp = student.HP.SubPositiveV(bullet.AP);
Debugger.Output(this, "is being shot! Now his hp is" + student.HP.ToString());
bullet.Parent.AddScore(GameData.TrickerScoreAttackStudent(subHp) + GameData.ScorePropUseSpear);
- bullet.Parent.AddHP((long)bullet.Parent.Vampire * subHp);
+ bullet.Parent.HP.AddPositiveV((long)bullet.Parent.Vampire * subHp);
}
else return;
}
@@ -334,12 +334,12 @@ public void BeAttacked(Student student, Bullet bullet)
long subHp;
if (bullet.HasSpear)
{
- subHp = student.SubHp(bullet.AP + GameData.ApSpearAdd);
+ subHp = student.HP.SubPositiveV(bullet.AP + GameData.ApSpearAdd);
Debugger.Output(this, "is being shot with Spear! Now his hp is" + student.HP.ToString());
}
else
{
- subHp = student.SubHp(bullet.AP);
+ subHp = student.HP.SubPositiveV(bullet.AP);
Debugger.Output(this, "is being shot! Now his hp is" + student.HP.ToString());
}
bullet.Parent.AddScore(GameData.TrickerScoreAttackStudent(subHp));
@@ -348,7 +348,7 @@ public void BeAttacked(Student student, Bullet bullet)
student.AddScore(subHp * GameData.factorOfScoreWhenTeacherAttacked / GameData.basicApOfGhost / FactorTeacher);
}
- bullet.Parent.AddHP((long)(bullet.Parent.Vampire * subHp));
+ bullet.Parent.HP.AddPositiveV((long)(bullet.Parent.Vampire * subHp));
}
if (student.HP <= 0)
student.TryActivatingLIFE(); // 如果有复活甲
diff --git a/logic/Gaming/PropManager.cs b/logic/Gaming/PropManager.cs
index 75426af6..b370710b 100644
--- a/logic/Gaming/PropManager.cs
+++ b/logic/Gaming/PropManager.cs
@@ -44,9 +44,9 @@ public void ConsumeProp(Character player, PropType propType)
break;
case PropType.AddHpOrAp:
if (!player.IsGhost())
- if (player.HP < player.MaxHp)
+ if (player.HP < player.HP.GetMaxV())
{
- player.AddHP(GameData.basicTreatmentDegree / 2);
+ player.HP.AddPositiveV(GameData.basicTreatmentDegree / 2);
player.AddScore(GameData.ScorePropAddHp);
}
else player.AddAp(GameData.PropDuration);
diff --git a/logic/Gaming/SkillManager/SkillManager.ActiveSkill.cs b/logic/Gaming/SkillManager/SkillManager.ActiveSkill.cs
index c4f4aa51..74ce47a5 100644
--- a/logic/Gaming/SkillManager/SkillManager.ActiveSkill.cs
+++ b/logic/Gaming/SkillManager/SkillManager.ActiveSkill.cs
@@ -349,7 +349,7 @@ public bool Punish(Character player)
|| character.PlayerState == PlayerStateType.ClimbingThroughWindows)
&& XY.DistanceFloor3(character.Position, player.Position) <= player.ViewRange / 3)
{
- int stunTime = (GameData.timeOfGhostStunnedWhenPunish + (int)((GameData.factorOfTimeStunnedWhenPunish * (player.MaxHp - player.HP) / GameData.basicApOfGhost))) / characterManager.FactorTeacher;
+ int stunTime = (GameData.timeOfGhostStunnedWhenPunish + (int)((GameData.factorOfTimeStunnedWhenPunish * (player.HP.GetMaxV() - player.HP) / GameData.basicApOfGhost))) / characterManager.FactorTeacher;
if (CharacterManager.BeStunned(character, stunTime) > 0)
player.AddScore(GameData.StudentScoreTrickerBeStunned(stunTime) / characterManager.FactorTeacher);
break;
@@ -421,7 +421,7 @@ public bool Rouse(Character player)
if ((character.PlayerState == PlayerStateType.Addicted) && gameMap.CanSee(player, character))
{
character.SetPlayerStateNaturally();
- character.SetHP(GameData.RemainHpWhenAddLife);
+ character.HP.SetPositiveV(GameData.RemainHpWhenAddLife);
((Student)character).SetTimeOfRescue(0);
player.AddScore(GameData.StudentScoreRescue);
break;
@@ -449,10 +449,10 @@ public bool Encourage(Character player)
{
foreach (Character character in gameMap.GameObjDict[GameObjType.Character])
{
- if ((character.HP < character.MaxHp) && gameMap.CanSee(player, character))
+ if ((character.HP < character.HP.GetMaxV()) && gameMap.CanSee(player, character))
{
player.AddScore(GameData.StudentScoreTreat(GameData.addHpWhenEncourage));
- character.AddHP(GameData.addHpWhenEncourage);
+ character.HP.AddPositiveV(GameData.addHpWhenEncourage);
((Student)character).SetDegreeOfTreatment0();
break;
}
diff --git a/logic/Preparation/Interface/ICharacter.cs b/logic/Preparation/Interface/ICharacter.cs
index cbe87ab4..998b5260 100644
--- a/logic/Preparation/Interface/ICharacter.cs
+++ b/logic/Preparation/Interface/ICharacter.cs
@@ -5,8 +5,7 @@ namespace Preparation.Interface
public interface ICharacter : IMoveable
{
public long TeamID { get; }
- public long HP { get; }
- public long AddHP(long add);
+ public LongWithVariableRange HP { get; }
public long Score { get; }
public void AddScore(long add);
public double Vampire { get; }
diff --git a/logic/Preparation/Utility/SafeValue.cs b/logic/Preparation/Utility/SafeValue.cs
index 3099e9c1..4fb4d640 100644
--- a/logic/Preparation/Utility/SafeValue.cs
+++ b/logic/Preparation/Utility/SafeValue.cs
@@ -28,7 +28,7 @@ public AtomicInt(int x)
}
public struct AtomicBool
{
- private int v;//v==0为false,v!=0(v==1或v==-1)为true
+ private int v;//v==0为false,v==1为true
public AtomicBool(bool x)
{
v = x ? 1 : 0;
@@ -45,25 +45,24 @@ public bool TrySet(bool value)
return (Interlocked.CompareExchange(ref v, value ? 1 : 0, value ? 0 : 1) ^ (value ? 1 : 0)) != 0;
}
- public bool Invert() => Interlocked.Add(ref v, -1) != 0;
public bool And(bool x) => Interlocked.And(ref v, x ? 1 : 0) != 0;
public bool Or(bool x) => Interlocked.Or(ref v, x ? 1 : 0) != 0;
}
///
- /// 一个能记录Start后完成多少进度的进度条(int),
+ /// 一个能记录Start后完成多少进度的进度条(long),
/// 只允许Start时修改needTime(请确保大于0);
/// 支持TrySet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
/// 不支持暂停
///
- public struct IntProgressContinuously
+ public struct LongProgressContinuously
{
private long endT = long.MaxValue;
private long needT;
- public IntProgressContinuously(long needTime)
+ public LongProgressContinuously(long needTime)
{
- if (needTime <= 0) Debugger.Output("Bug:IntProgressContinuously.needTime (" + needTime.ToString() + ") is less than 0.");
+ if (needTime <= 0) Debugger.Output("Bug:LongProgressContinuously.needTime (" + needTime.ToString() + ") is less than 0.");
this.needT = needTime;
}
public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
@@ -82,6 +81,11 @@ public long GetProgress()
if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
}
+ ///
+ /// <0则表明未开始
+ ///
+ public static implicit operator long(LongProgressContinuously pLong) => pLong.GetProgress();
+
///
/// GetProgressDouble<0则表明未开始
///
@@ -96,12 +100,12 @@ public bool Start(long needTime)
{
if (needTime <= 0)
{
- Debugger.Output("Warning:Start IntProgressContinuously with the needTime (" + needTime.ToString() + ") which is less than 0.");
+ Debugger.Output("Warning:Start LongProgressContinuously with the needTime (" + needTime.ToString() + ") which is less than 0.");
return false;
}
//规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
- if (needTime <= 2) Debugger.Output("Warning:the field of IntProgressContinuously is " + needTime.ToString() + ",which is too small.");
+ if (needTime <= 2) Debugger.Output("Warning:the field of LongProgressContinuously is " + needTime.ToString() + ",which is too small.");
Interlocked.Exchange(ref this.needT, needTime);
return true;
}
@@ -142,6 +146,7 @@ public IntWithVariableRange(int value, int maxValue)
v = value < maxValue ? value : maxValue;
this.maxV = maxValue;
}
+
public override string ToString()
{
lock (vLock)
@@ -150,6 +155,7 @@ public override string ToString()
}
}
public int GetValue() { lock (vLock) return v; }
+ public static implicit operator int(IntWithVariableRange aint) => aint.GetValue();
public int GetMaxV() { lock (vLock) return maxV; }
///
@@ -232,6 +238,160 @@ public int SubPositiveV(int subPositiveV)
}
return subPositiveV;
}
+
+ ///
+ /// 试图加到满,如果无法加到maxValue则不加并返回-1
+ ///
+ public int TryAddAll(int addV)
+ {
+ lock (vLock)
+ {
+ if (maxV - v <= addV)
+ {
+ addV = maxV - v;
+ v = maxV;
+ return addV;
+ }
+ return 0;
+ }
+ }
+ }
+
+ ///
+ /// 一个保证在[0,maxValue]的可变long,支持可变的maxValue(请确保大于0)
+ ///
+ public struct LongWithVariableRange
+ {
+ private long v;
+ private long maxV;
+ private readonly object vLock = new();
+ public LongWithVariableRange(long value, long maxValue)
+ {
+ if (maxValue < 0)
+ {
+ Debugger.Output("Warning:Try to set LongWithVariableRange.maxValue to " + maxValue.ToString() + ".");
+ maxValue = 0;
+ }
+ v = value < maxValue ? value : maxValue;
+ this.maxV = maxValue;
+ }
+ public LongWithVariableRange(long maxValue)
+ {
+ if (maxValue < 0)
+ {
+ Debugger.Output("Warning:Try to set LongWithVariableRange.maxValue to " + maxValue.ToString() + ".");
+ maxValue = 0;
+ }
+ v = this.maxV = maxValue;
+ }
+ public override string ToString()
+ {
+ lock (vLock)
+ {
+ return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString();
+ }
+ }
+ public long GetValue() { lock (vLock) return v; }
+ public static implicit operator long(LongWithVariableRange aint) => aint.GetValue();
+ public long GetMaxV() { lock (vLock) return maxV; }
+
+ ///
+ /// 若maxValue<=0则maxValue设为0并返回False
+ ///
+ public bool SetMaxV(long maxValue)
+ {
+ if (maxValue < 0) maxValue = 0;
+ lock (vLock)
+ {
+ maxV = maxValue;
+ if (v > maxValue) v = maxValue;
+ }
+ return maxValue > 0;
+ }
+ ///
+ /// 应当保证该maxValue>=0
+ ///
+ public void SetPositiveMaxV(long maxValue)
+ {
+ lock (vLock)
+ {
+ maxV = maxValue;
+ if (v > maxValue) v = maxValue;
+ }
+ }
+ ///
+ /// 应当保证该value>=0
+ ///
+ public long SetPositiveV(long value)
+ {
+ lock (vLock)
+ {
+ return v = (value > maxV) ? maxV : value;
+ }
+ }
+ public long SetV(long value)
+ {
+ if (value < 0) value = 0;
+ lock (vLock)
+ {
+ return v = (value > maxV) ? maxV : value;
+ }
+ }
+ ///
+ /// 返回实际改变量
+ ///
+ public long AddV(long addV)
+ {
+ lock (vLock)
+ {
+ long previousV = v;
+ v += addV;
+ if (v < 0) v = 0;
+ if (v > maxV) v = maxV;
+ return v - previousV;
+ }
+ }
+ ///
+ /// 应当保证该增加值大于0,返回实际改变量
+ ///
+ public long AddPositiveV(long addPositiveV)
+ {
+ lock (vLock)
+ {
+ addPositiveV = Math.Min(addPositiveV, maxV - v);
+ v += addPositiveV;
+ }
+ return addPositiveV;
+ }
+ ///
+ /// 应当保证该减少值大于0,返回实际改变量
+ ///
+ public long SubPositiveV(long subPositiveV)
+ {
+ lock (vLock)
+ {
+ subPositiveV = Math.Min(subPositiveV, v);
+ v -= subPositiveV;
+ }
+ return subPositiveV;
+ }
+
+ ///
+ /// 试图加到满,如果无法加到maxValue则不加并返回-1
+ ///
+ public long TryAddAll(long addV)
+ {
+ lock (vLock)
+ {
+ if (maxV - v <= addV)
+ {
+ addV = maxV - v;
+ v = maxV;
+ return addV;
+ }
+ return -1;
+ }
+ }
}
///
@@ -276,6 +436,7 @@ public int GetNum(long time)
return num;
}
}
+ public static implicit operator int(IntNumUpdateByCD aint) => aint.GetNum(Environment.TickCount64);
///
/// 应当保证该subV>=0
@@ -361,8 +522,18 @@ public bool SetNum(int num)
{
lock (numLock)
{
- if (num < 0) { this.num = 0; return false; }
- this.num = (num < maxNum) ? num : maxNum;
+ if (num < 0)
+ {
+ this.num = 0;
+ updateTime = Environment.TickCount64;
+ return false;
+ }
+ if (num < maxNum)
+ {
+ if (this.num == maxNum) updateTime = Environment.TickCount64;
+ this.num = num;
+ }
+ else this.num = maxNum;
return true;
}
}
@@ -373,7 +544,12 @@ public void SetPositiveNum(int num)
{
lock (numLock)
{
- this.num = (num < maxNum) ? num : maxNum;
+ if (num < maxNum)
+ {
+ if (this.num == maxNum) updateTime = Environment.TickCount64;
+ this.num = num;
+ }
+ else this.num = maxNum;
}
}
public void SetCD(int cd)
From 81302bddce242076f7e1a6ec9a761a8265e97b72 Mon Sep 17 00:00:00 2001
From: shangfengh <3495281661@qq.com>
Date: Wed, 21 Jun 2023 21:48:38 +0800
Subject: [PATCH 6/9] feat: :art: add a new SafeValues, AtomicLong
---
.../GameClass/GameObj/Character/Character.cs | 66 +++++++++----------
logic/Gaming/CharacterManager.cs | 4 +-
logic/Preparation/Utility/SafeValue.cs | 21 ++++++
3 files changed, 56 insertions(+), 35 deletions(-)
diff --git a/logic/GameClass/GameObj/Character/Character.cs b/logic/GameClass/GameObj/Character/Character.cs
index 5d0394dd..f5469860 100644
--- a/logic/GameClass/GameObj/Character/Character.cs
+++ b/logic/GameClass/GameObj/Character/Character.cs
@@ -147,38 +147,48 @@ public double Vampire
}
public double OriVampire { get; protected set; }
- private AtomicInt degreeOfTreatment = new(0);
+ private readonly object treatLock = new();
+ private int degreeOfTreatment = 0;
public int DegreeOfTreatment
{
- get => degreeOfTreatment;
+ get
+ {
+ lock (treatLock)
+ return degreeOfTreatment;
+ }
}
public void SetDegreeOfTreatment0()
{
- degreeOfTreatment.Set(0);
+ lock (treatLock)
+ degreeOfTreatment = 0;
}
+
public bool AddDegreeOfTreatment(int value, Student whoTreatYou)
{
- value = degreeOfTreatment.Add(value);
- long addV = HP.TryAddAll(value);
- if (addV == 0)
+ lock (treatLock)
{
- SetDegreeOfTreatment0();
+ degreeOfTreatment += value;
+ long addV = HP.TryAddAll(degreeOfTreatment);
+ if (addV == 0)
+ {
+ degreeOfTreatment = 0;
+ return false;
+ }
+ if (addV > 0)
+ {
+ whoTreatYou.AddScore(GameData.StudentScoreTreat(addV));
+ degreeOfTreatment = 0;
+ return true;
+ }
+ if (degreeOfTreatment >= GameData.basicTreatmentDegree)
+ {
+ whoTreatYou.AddScore(GameData.StudentScoreTreat(GameData.basicTreatmentDegree));
+ HP.AddPositiveV(GameData.basicTreatmentDegree);
+ degreeOfTreatment = 0;
+ return true;
+ }
return false;
}
- if (addV > 0)
- {
- whoTreatYou.AddScore(GameData.StudentScoreTreat(addV));
- SetDegreeOfTreatment0();
- return true;
- }
- if (value >= GameData.basicTreatmentDegree)
- {
- whoTreatYou.AddScore(GameData.StudentScoreTreat(GameData.basicTreatmentDegree));
- HP.AddPositiveV(GameData.basicTreatmentDegree);
- SetDegreeOfTreatment0();
- return true;
- }
- return false;
}
#endregion
#region 查询状态相关的基本属性与方法
@@ -475,18 +485,8 @@ public virtual void AddScore(long add)
///
/// 角色所属队伍ID
///
- private long teamID = long.MaxValue;
- public long TeamID
- {
- get => Interlocked.Read(ref teamID);
- set => Interlocked.Exchange(ref teamID, value);
- }
- private long playerID = long.MaxValue;
- public long PlayerID
- {
- get => Interlocked.Read(ref playerID);
- set => Interlocked.Exchange(ref playerID, value);
- }
+ public AtomicLong TeamID { get; } = new AtomicLong(long.MaxValue);
+ public AtomicLong PlayerID { get; } = new AtomicLong(long.MaxValue);
#region 道具和buff相关属性、方法
private readonly object inventoryLock = new();
diff --git a/logic/Gaming/CharacterManager.cs b/logic/Gaming/CharacterManager.cs
index 182f42ce..6fdeedba 100644
--- a/logic/Gaming/CharacterManager.cs
+++ b/logic/Gaming/CharacterManager.cs
@@ -57,8 +57,8 @@ public void DoubleFactorTeacher()
}
gameMap.Add(newPlayer);
- newPlayer.TeamID = teamID;
- newPlayer.PlayerID = playerID;
+ newPlayer.TeamID.Set(teamID);
+ newPlayer.PlayerID.Set(playerID);
/* #region 人物装弹
new Thread
(
diff --git a/logic/Preparation/Utility/SafeValue.cs b/logic/Preparation/Utility/SafeValue.cs
index 4fb4d640..d2be37d8 100644
--- a/logic/Preparation/Utility/SafeValue.cs
+++ b/logic/Preparation/Utility/SafeValue.cs
@@ -26,6 +26,27 @@ public AtomicInt(int x)
/// 返回操作前的值
public int CompareExReturnOri(int newV, int compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}
+ public struct AtomicLong
+ {
+ private long v;
+ public AtomicLong(long x)
+ {
+ v = x;
+ }
+ public override string ToString() => Interlocked.Read(ref v).ToString();
+ public long Get() => Interlocked.Read(ref v);
+ public static implicit operator long(AtomicLong aint) => Interlocked.Read(ref aint.v);
+ public long Set(long value) => Interlocked.Exchange(ref v, value);
+
+ public long Add(long x) => Interlocked.Add(ref v, x);
+ public long Sub(long x) => Interlocked.Add(ref v, -x);
+ public long Inc() => Interlocked.Increment(ref v);
+ public long Dec() => Interlocked.Decrement(ref v);
+
+ public void CompareExchange(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
+ /// 返回操作前的值
+ public long CompareExReturnOri(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
+ }
public struct AtomicBool
{
private int v;//v==0为false,v==1为true
From b4c4eab2d7a675d46441c9ee8ae603069a611ae9 Mon Sep 17 00:00:00 2001
From: shangfengh <3495281661@qq.com>
Date: Sat, 24 Jun 2023 00:23:56 +0800
Subject: [PATCH 7/9] refactor: :art: make the chest OpenProgress
---
.../GameClass/GameObj/Character/Character.cs | 4 +-
logic/GameClass/GameObj/Map/Chest.cs | 36 +-------------
logic/GameClass/GameObj/Map/Door.cs | 1 -
logic/Gaming/ActionManager.cs | 2 +-
logic/Preparation/Interface/ICharacter.cs | 2 +-
logic/Preparation/Interface/IChest.cs | 9 ----
logic/Preparation/Utility/SafeValue.cs | 48 ++++++++++++++++++-
logic/Server/CopyInfo.cs | 3 +-
8 files changed, 54 insertions(+), 51 deletions(-)
delete mode 100644 logic/Preparation/Interface/IChest.cs
diff --git a/logic/GameClass/GameObj/Character/Character.cs b/logic/GameClass/GameObj/Character/Character.cs
index f5469860..14e05958 100644
--- a/logic/GameClass/GameObj/Character/Character.cs
+++ b/logic/GameClass/GameObj/Character/Character.cs
@@ -354,7 +354,7 @@ public long SetPlayerState(RunningStateType runningState, PlayerStateType value
case PlayerStateType.OpeningTheChest:
if (value == PlayerStateType.Rescued) return -1;
- ((Chest)lastObj!).StopOpen();
+ ((Chest)lastObj!).OpenProgress.Set0();
return ChangePlayerState(runningState, value, gameObj);
case PlayerStateType.OpeningTheDoorway:
if (value == PlayerStateType.Rescued) return -1;
@@ -382,7 +382,7 @@ public long SetPlayerState(RunningStateType runningState, PlayerStateType value
else
{
if (value != PlayerStateType.UsingSkill)
- ((UseRobot)FindActiveSkill(ActiveSkillType.UseRobot)).NowPlayerID = (int)playerID;
+ ((UseRobot)FindActiveSkill(ActiveSkillType.UseRobot)).NowPlayerID = (int)PlayerID;
return ChangePlayerState(runningState, value, gameObj);
}
}
diff --git a/logic/GameClass/GameObj/Map/Chest.cs b/logic/GameClass/GameObj/Map/Chest.cs
index de67e895..ff589da2 100644
--- a/logic/GameClass/GameObj/Map/Chest.cs
+++ b/logic/GameClass/GameObj/Map/Chest.cs
@@ -7,7 +7,7 @@ namespace GameClass.GameObj
///
/// 箱子
///
- public class Chest : Immovable, IChest
+ public class Chest : Immovable
{
public Chest(XY initPos) :
base(initPos, GameData.numOfPosGridPerCell / 2, GameObjType.Chest)
@@ -19,38 +19,6 @@ public Chest(XY initPos) :
private readonly Gadget[] propInChest = new Gadget[GameData.maxNumOfPropInChest] { new NullProp(), new NullProp() };
public Gadget[] PropInChest => propInChest;
- private long openStartTime = 0;
- public long OpenStartTime
- {
- get
- {
- lock (gameObjLock) return openStartTime;
- }
- }
- private Character? whoOpen = null;
- public Character? WhoOpen
- {
- get
- {
- lock (gameObjLock) return whoOpen;
- }
- }
- public bool Open(Character character)
- {
- lock (gameObjLock)
- {
- if (whoOpen != null) return false;
- openStartTime = Environment.TickCount64;
- whoOpen = character;
- }
- return true;
- }
- public void StopOpen()
- {
- lock (gameObjLock)
- {
- whoOpen = null;
- }
- }
+ public LongProgressContinuously OpenProgress { get; } = new LongProgressContinuously();
}
}
diff --git a/logic/GameClass/GameObj/Map/Door.cs b/logic/GameClass/GameObj/Map/Door.cs
index 8970e6c5..80b66456 100644
--- a/logic/GameClass/GameObj/Map/Door.cs
+++ b/logic/GameClass/GameObj/Map/Door.cs
@@ -1,7 +1,6 @@
using Preparation.Interface;
using Preparation.Utility;
using System;
-using System.Threading;
namespace GameClass.GameObj
{
diff --git a/logic/Gaming/ActionManager.cs b/logic/Gaming/ActionManager.cs
index b8cd6053..5fd1cc3b 100644
--- a/logic/Gaming/ActionManager.cs
+++ b/logic/Gaming/ActionManager.cs
@@ -369,7 +369,7 @@ public bool OpenChest(Character player)
return;
}
else
- if (!chestToOpen.Open(player))
+ if (!chestToOpen.OpenProgress.Start((long)(GameData.degreeOfOpenedChest / player.SpeedOfOpenChest)))
{
player.ThreadNum.Release();
player.SetPlayerStateNaturally();
diff --git a/logic/Preparation/Interface/ICharacter.cs b/logic/Preparation/Interface/ICharacter.cs
index 998b5260..dbddda81 100644
--- a/logic/Preparation/Interface/ICharacter.cs
+++ b/logic/Preparation/Interface/ICharacter.cs
@@ -4,7 +4,7 @@ namespace Preparation.Interface
{
public interface ICharacter : IMoveable
{
- public long TeamID { get; }
+ public AtomicLong TeamID { get; }
public LongWithVariableRange HP { get; }
public long Score { get; }
public void AddScore(long add);
diff --git a/logic/Preparation/Interface/IChest.cs b/logic/Preparation/Interface/IChest.cs
deleted file mode 100644
index d787cde9..00000000
--- a/logic/Preparation/Interface/IChest.cs
+++ /dev/null
@@ -1,9 +0,0 @@
-using Preparation.Utility;
-
-namespace Preparation.Interface
-{
- public interface IChest : IGameObj
- {
- public void StopOpen();
- }
-}
\ No newline at end of file
diff --git a/logic/Preparation/Utility/SafeValue.cs b/logic/Preparation/Utility/SafeValue.cs
index d2be37d8..45a4b660 100644
--- a/logic/Preparation/Utility/SafeValue.cs
+++ b/logic/Preparation/Utility/SafeValue.cs
@@ -86,6 +86,10 @@ public LongProgressContinuously(long needTime)
if (needTime <= 0) Debugger.Output("Bug:LongProgressContinuously.needTime (" + needTime.ToString() + ") is less than 0.");
this.needT = needTime;
}
+ public LongProgressContinuously()
+ {
+ this.needT = long.MaxValue;
+ }
public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2);
public override string ToString() => "EndTime:" + Interlocked.CompareExchange(ref endT, -2, -2).ToString() + " ms, NeedTime:" + Interlocked.CompareExchange(ref needT, -2, -2).ToString() + " ms";
@@ -103,6 +107,15 @@ public long GetProgress()
return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
}
///
+ /// GetProgress<0则表明未开始
+ ///
+ public long GetProgress(long time)
+ {
+ long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
+ return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
+ }
+ ///
/// <0则表明未开始
///
public static implicit operator long(LongProgressContinuously pLong) => pLong.GetProgress();
@@ -167,6 +180,22 @@ public IntWithVariableRange(int value, int maxValue)
v = value < maxValue ? value : maxValue;
this.maxV = maxValue;
}
+ ///
+ /// 默认使Value=maxValue
+ ///
+ public IntWithVariableRange(int maxValue)
+ {
+ if (maxValue < 0)
+ {
+ Debugger.Output("Warning:Try to set IntWithVariableRange.maxValue to " + maxValue.ToString() + ".");
+ maxValue = 0;
+ }
+ v = this.maxV = maxValue;
+ }
+ public IntWithVariableRange()
+ {
+ v = this.maxV = int.MaxValue;
+ }
public override string ToString()
{
@@ -296,6 +325,9 @@ public LongWithVariableRange(long value, long maxValue)
v = value < maxValue ? value : maxValue;
this.maxV = maxValue;
}
+ ///
+ /// 默认使Value=maxValue
+ ///
public LongWithVariableRange(long maxValue)
{
if (maxValue < 0)
@@ -305,6 +337,11 @@ public LongWithVariableRange(long maxValue)
}
v = this.maxV = maxValue;
}
+ public LongWithVariableRange()
+ {
+ v = this.maxV = long.MaxValue;
+ }
+
public override string ToString()
{
lock (vLock)
@@ -424,7 +461,7 @@ public struct IntNumUpdateByCD
private int maxNum;
private int cd;
private long updateTime = 0;
- private object numLock = new();
+ private readonly object numLock = new();
public IntNumUpdateByCD(int num, int maxNum, int cd)
{
if (num < 0) Debugger.Output("Bug:IntNumUpdateByCD.num (" + num.ToString() + ") is less than 0.");
@@ -435,6 +472,9 @@ public IntNumUpdateByCD(int num, int maxNum, int cd)
this.cd = cd;
this.updateTime = Environment.TickCount64;
}
+ ///
+ /// 默认使num=maxNum
+ ///
public IntNumUpdateByCD(int maxNum, int cd)
{
if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateByCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
@@ -442,6 +482,12 @@ public IntNumUpdateByCD(int maxNum, int cd)
this.num = this.maxNum = maxNum;
this.cd = cd;
}
+ public IntNumUpdateByCD()
+ {
+ this.num = this.maxNum = 0;
+ this.cd = int.MaxValue;
+ }
+
public int GetMaxNum() { lock (numLock) return maxNum; }
public int GetCD() { lock (numLock) return cd; }
public int GetNum(long time)
diff --git a/logic/Server/CopyInfo.cs b/logic/Server/CopyInfo.cs
index d9d341a6..067ecccc 100644
--- a/logic/Server/CopyInfo.cs
+++ b/logic/Server/CopyInfo.cs
@@ -277,8 +277,7 @@ private static MessageOfObj Chest(Chest chest, long time)
Y = chest.Position.y
}
};
- int progress = (chest.WhoOpen != null) ? (((int)(time - chest.OpenStartTime)) * chest.WhoOpen.SpeedOfOpenChest) : 0;
- msg.ChestMessage.Progress = (progress > GameData.degreeOfOpenedChest) ? GameData.degreeOfOpenedChest : progress;
+ msg.ChestMessage.Progress = (int)chest.OpenProgress.GetProgress(time);
return msg;
}
}
From e4bea8433e7687da844e48ade1e9d8a3d6b6b02a Mon Sep 17 00:00:00 2001
From: shangfengh <3495281661@qq.com>
Date: Thu, 6 Jul 2023 14:02:38 +0800
Subject: [PATCH 8/9] fix: :bug: make SafeValues classs
---
logic/Client/MainWindow.xaml.cs | 2 +-
.../GameClass/GameObj/Bullet/Bullet.Ghost.cs | 10 +--
logic/GameClass/GameObj/Bullet/Bullet.cs | 7 +-
.../GameObj/Character/Character.Skill.cs | 4 +-
.../GameClass/GameObj/Character/Character.cs | 4 +-
logic/GameClass/GameObj/GameObj.cs | 3 +-
logic/GameClass/GameObj/Map/Chest.cs | 3 +-
logic/GameClass/GameObj/Map/Door.cs | 5 +-
logic/GameClass/GameObj/Map/Map.cs | 5 +-
logic/GameClass/GameObj/Moveable.cs | 6 +-
logic/GameClass/GameObj/Prop/Gadget.cs | 4 +-
logic/GameClass/GameObj/Prop/Item.cs | 4 +-
logic/GameEngine/MoveEngine.cs | 8 +-
logic/Gaming/ActionManager.cs | 4 +-
logic/Gaming/AttackManager.cs | 4 +-
logic/Gaming/CharacterManager.cs | 39 +-------
logic/Gaming/Game.cs | 4 +-
.../SkillManager/SkillManager.ActiveSkill.cs | 4 +-
.../SkillManager/SkillManager.PassiveSkill.cs | 2 +-
logic/Preparation/Interface/ISkill.cs | 3 +-
logic/Preparation/Utility/SafeValue.cs | 90 ++++++++++++-------
21 files changed, 107 insertions(+), 108 deletions(-)
diff --git a/logic/Client/MainWindow.xaml.cs b/logic/Client/MainWindow.xaml.cs
index 1ac24a75..3874f308 100644
--- a/logic/Client/MainWindow.xaml.cs
+++ b/logic/Client/MainWindow.xaml.cs
@@ -844,7 +844,7 @@ private void Refresh(object? sender, EventArgs e) //log未更新
}
foreach (var obj in listOfButcher)
{
- if (!isDataFixed[obj.PlayerId])
+ if (obj.PlayerId < GameData.numOfStudent && !isDataFixed[obj.PlayerId])
{
IGhostType occupation1 = (IGhostType)OccupationFactory.FindIOccupation(Transformation.ToTrickerType(obj.TrickerType));
int j = 0;
diff --git a/logic/GameClass/GameObj/Bullet/Bullet.Ghost.cs b/logic/GameClass/GameObj/Bullet/Bullet.Ghost.cs
index 072a993b..4ddd23ed 100644
--- a/logic/GameClass/GameObj/Bullet/Bullet.Ghost.cs
+++ b/logic/GameClass/GameObj/Bullet/Bullet.Ghost.cs
@@ -8,7 +8,7 @@ internal sealed class CommonAttackOfGhost : Bullet
public CommonAttackOfGhost(Character player, XY pos, int radius = GameData.bulletRadius) :
base(player, radius, pos)
{
- AP.Set(GameData.basicApOfGhost);
+ AP.SetReturnOri(GameData.basicApOfGhost);
}
public override double BulletBombRange => 0;
public override double AttackDistance => GameData.basicAttackShortRange;
@@ -45,7 +45,7 @@ internal sealed class Strike : Bullet
public Strike(Character player, XY pos, int radius = GameData.bulletRadius) :
base(player, radius, pos)
{
- AP.Set(GameData.basicApOfGhost * 16 / 15);
+ AP.SetReturnOri(GameData.basicApOfGhost * 16 / 15);
}
public override double BulletBombRange => 0;
public override double AttackDistance => GameData.basicAttackShortRange * 20 / 22;
@@ -83,7 +83,7 @@ internal sealed class FlyingKnife : Bullet
public FlyingKnife(Character player, XY pos, int radius = GameData.bulletRadius) :
base(player, radius, pos)
{
- AP.Set(GameData.basicApOfGhost * 4 / 5);
+ AP.SetReturnOri(GameData.basicApOfGhost * 4 / 5);
}
public override double BulletBombRange => 0;
public override double AttackDistance => GameData.basicRemoteAttackRange * 13;
@@ -123,7 +123,7 @@ internal sealed class BombBomb : Bullet
{
public BombBomb(Character player, XY pos, int radius = GameData.bulletRadius) : base(player, radius, pos)
{
- AP.Set((int)(GameData.basicApOfGhost * 6.0 / 5));
+ AP.SetReturnOri((int)(GameData.basicApOfGhost * 6.0 / 5));
}
public override double BulletBombRange => GameData.basicBulletBombRange;
public override double AttackDistance => GameData.basicAttackShortRange;
@@ -163,7 +163,7 @@ internal sealed class JumpyDumpty : Bullet
{
public JumpyDumpty(Character player, XY pos, int radius = GameData.bulletRadius) : base(player, radius, pos)
{
- AP.Set((int)(GameData.basicApOfGhost * 0.6));
+ AP.SetReturnOri((int)(GameData.basicApOfGhost * 0.6));
}
public override double BulletBombRange => GameData.basicBulletBombRange / 2;
public override double AttackDistance => GameData.basicAttackShortRange * 18 / 22;
diff --git a/logic/GameClass/GameObj/Bullet/Bullet.cs b/logic/GameClass/GameObj/Bullet/Bullet.cs
index c47f746c..28954297 100644
--- a/logic/GameClass/GameObj/Bullet/Bullet.cs
+++ b/logic/GameClass/GameObj/Bullet/Bullet.cs
@@ -11,7 +11,8 @@ public abstract class Bullet : ObjOfCharacter
///
public abstract double BulletBombRange { get; }
public abstract double AttackDistance { get; }
- public AtomicInt AP { get; }
+ private AtomicInt ap = new(0);
+ public AtomicInt AP { get => ap; }
public abstract int Speed { get; }
public abstract bool IsRemoteAttack { get; }
public abstract int CastTime { get; }
@@ -43,8 +44,8 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj)
public Bullet(Character player, int radius, XY Position) :
base(Position, radius, GameObjType.Bullet)
{
- this.CanMove.Set(true);
- this.MoveSpeed.Set(this.Speed);
+ this.CanMove.SetReturnOri(true);
+ this.MoveSpeed.SetReturnOri(this.Speed);
this.hasSpear = player.TryUseSpear();
this.Parent = player;
}
diff --git a/logic/GameClass/GameObj/Character/Character.Skill.cs b/logic/GameClass/GameObj/Character/Character.Skill.cs
index 69c487ac..21f4a48d 100644
--- a/logic/GameClass/GameObj/Character/Character.Skill.cs
+++ b/logic/GameClass/GameObj/Character/Character.Skill.cs
@@ -31,12 +31,12 @@ public bool IsGhost()
protected Character(XY initPos, int initRadius, CharacterType characterType) :
base(initPos, initRadius, GameObjType.Character)
{
- this.CanMove.Set(true);
+ this.CanMove.SetReturnOri(true);
this.score = 0;
this.buffManager = new BuffManager();
this.occupation = OccupationFactory.FindIOccupation(characterType);
this.HP = new(Occupation.MaxHp);
- this.MoveSpeed.Set(this.orgMoveSpeed = Occupation.MoveSpeed);
+ this.MoveSpeed.SetReturnOri(this.orgMoveSpeed = Occupation.MoveSpeed);
this.BulletOfPlayer = this.OriBulletOfPlayer = Occupation.InitBullet;
this.concealment = Occupation.Concealment;
this.alertnessRadius = Occupation.AlertnessRadius;
diff --git a/logic/GameClass/GameObj/Character/Character.cs b/logic/GameClass/GameObj/Character/Character.cs
index 14e05958..1461b7a8 100644
--- a/logic/GameClass/GameObj/Character/Character.cs
+++ b/logic/GameClass/GameObj/Character/Character.cs
@@ -459,7 +459,7 @@ public bool TryToRemoveFromGame(PlayerStateType playerStateType)
{
if (SetPlayerState(RunningStateType.RunningForcibly, playerStateType) == -1) return false;
TryToRemove();
- CanMove.Set(false);
+ CanMove.SetReturnOri(false);
position = GameData.PosWhoDie;
}
return true;
@@ -604,7 +604,7 @@ public int IndexingOfAddProp()
}
public void AddMoveSpeed(int buffTime, double add = 1.0) => buffManager.AddMoveSpeed(add, buffTime, newVal =>
- { MoveSpeed.Set(newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed); },
+ { MoveSpeed.SetReturnOri(newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed); },
OrgMoveSpeed);
public bool HasFasterSpeed => buffManager.HasFasterSpeed;
diff --git a/logic/GameClass/GameObj/GameObj.cs b/logic/GameClass/GameObj/GameObj.cs
index 1934d642..3933b7a3 100644
--- a/logic/GameClass/GameObj/GameObj.cs
+++ b/logic/GameClass/GameObj/GameObj.cs
@@ -30,7 +30,8 @@ public abstract class GameObj : IGameObj
public abstract ShapeType Shape { get; }
- public AtomicBool IsRemoved { get; } = new AtomicBool(false);
+ private AtomicBool isRemoved = new(false);
+ public AtomicBool IsRemoved { get => isRemoved; }
public virtual bool TryToRemove()
{
return IsRemoved.TrySet(true);
diff --git a/logic/GameClass/GameObj/Map/Chest.cs b/logic/GameClass/GameObj/Map/Chest.cs
index ff589da2..8e450e53 100644
--- a/logic/GameClass/GameObj/Map/Chest.cs
+++ b/logic/GameClass/GameObj/Map/Chest.cs
@@ -19,6 +19,7 @@ public Chest(XY initPos) :
private readonly Gadget[] propInChest = new Gadget[GameData.maxNumOfPropInChest] { new NullProp(), new NullProp() };
public Gadget[] PropInChest => propInChest;
- public LongProgressContinuously OpenProgress { get; } = new LongProgressContinuously();
+ private LongProgressByTime openProgress = new LongProgressByTime();
+ public LongProgressByTime OpenProgress { get => openProgress; }
}
}
diff --git a/logic/GameClass/GameObj/Map/Door.cs b/logic/GameClass/GameObj/Map/Door.cs
index 80b66456..ca6ba5f3 100644
--- a/logic/GameClass/GameObj/Map/Door.cs
+++ b/logic/GameClass/GameObj/Map/Door.cs
@@ -46,7 +46,8 @@ public bool IsOpen
}
}
- public AtomicInt LockDegree { get; } = new AtomicInt(0);
+ private AtomicInt lockDegree = new AtomicInt(0);
+ public AtomicInt LockDegree { get => lockDegree; }
private long openStartTime = 0;
public long OpenStartTime
@@ -97,7 +98,7 @@ public bool TryLock(Character character)
{
if (!isOpen) return false;
if (whoLockOrOpen != null) return false;
- LockDegree.Set(0);
+ LockDegree.SetReturnOri(0);
whoLockOrOpen = character;
return true;
}
diff --git a/logic/GameClass/GameObj/Map/Map.cs b/logic/GameClass/GameObj/Map/Map.cs
index af3d9b1b..75dea6b8 100644
--- a/logic/GameClass/GameObj/Map/Map.cs
+++ b/logic/GameClass/GameObj/Map/Map.cs
@@ -26,7 +26,7 @@ public void AddNumOfRepairedGenerators()
{
Random r = new Random(Environment.TickCount);
EmergencyExit emergencyExit = (EmergencyExit)(GameObjDict[GameObjType.EmergencyExit][r.Next(0, GameObjDict[GameObjType.EmergencyExit].Count)]);
- emergencyExit.CanOpen.Set(true);
+ emergencyExit.CanOpen.SetReturnOri(true);
Preparation.Utility.Debugger.Output(emergencyExit, emergencyExit.Position.ToString());
}
finally
@@ -41,7 +41,7 @@ public void AddNumOfRepairedGenerators()
try
{
foreach (Doorway doorway in GameObjDict[GameObjType.Doorway])
- doorway.PowerSupply.Set(true);
+ doorway.PowerSupply.SetReturnOri(true);
}
finally
{
@@ -228,7 +228,6 @@ public IOutOfBound GetOutOfBound(XY pos)
{
if (person.CharacterType == CharacterType.TechOtaku)
{
- Debugger.Output(person, person.PlayerID.ToString());
foreach (Character character in gameObjDict[GameObjType.Character])
{
if (((UseRobot)person.FindActiveSkill(ActiveSkillType.UseRobot)).NowPlayerID == character.PlayerID)
diff --git a/logic/GameClass/GameObj/Moveable.cs b/logic/GameClass/GameObj/Moveable.cs
index 1ff26f7c..093f17a6 100644
--- a/logic/GameClass/GameObj/Moveable.cs
+++ b/logic/GameClass/GameObj/Moveable.cs
@@ -94,14 +94,16 @@ public void ReSetPos(XY position)
}
}
- public AtomicBool CanMove { get; }
+ private AtomicBool canMove = new(false);
+ public AtomicBool CanMove { get => canMove; }
public bool IsAvailableForMove => !IsMoving && CanMove && !IsRemoved; // 是否能接收移动指令
///
/// 移动速度
///
- public AtomicInt MoveSpeed { get; }
+ private AtomicInt moveSpeed = new(0);
+ public AtomicInt MoveSpeed { get => moveSpeed; }
///
/// 原初移动速度
///
diff --git a/logic/GameClass/GameObj/Prop/Gadget.cs b/logic/GameClass/GameObj/Prop/Gadget.cs
index 85565cce..74f545cc 100644
--- a/logic/GameClass/GameObj/Prop/Gadget.cs
+++ b/logic/GameClass/GameObj/Prop/Gadget.cs
@@ -24,8 +24,8 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj)
public Gadget(XY initPos, int radius = GameData.propRadius) :
base(initPos, radius, GameObjType.Gadget)
{
- this.CanMove.Set(false);
- this.MoveSpeed.Set(GameData.propMoveSpeed);
+ this.CanMove.SetReturnOri(false);
+ this.MoveSpeed.SetReturnOri(GameData.propMoveSpeed);
}
}
public abstract class Tool : Gadget
diff --git a/logic/GameClass/GameObj/Prop/Item.cs b/logic/GameClass/GameObj/Prop/Item.cs
index 2b83ceaf..881e38fa 100644
--- a/logic/GameClass/GameObj/Prop/Item.cs
+++ b/logic/GameClass/GameObj/Prop/Item.cs
@@ -17,8 +17,8 @@ public abstract class Item : ObjOfCharacter
public Item(XY initPos, int radius = GameData.propRadius) :
base(initPos, radius, GameObjType.Item)
{
- this.CanMove.Set(false);
- this.MoveSpeed.Set(0);
+ this.CanMove.SetReturnOri(false);
+ this.MoveSpeed.SetReturnOri(0);
}
}
diff --git a/logic/GameEngine/MoveEngine.cs b/logic/GameEngine/MoveEngine.cs
index a2236b79..d4c76f78 100644
--- a/logic/GameEngine/MoveEngine.cs
+++ b/logic/GameEngine/MoveEngine.cs
@@ -102,7 +102,7 @@ public void MoveObj(IMoveable obj, int moveTime, double direction, long stateNum
lock (obj.ActionLock)
{
if (!obj.IsAvailableForMove) { EndMove(obj); return; }
- obj.IsMoving.Set(true);
+ obj.IsMoving.SetReturnOri(true);
}
new Thread
@@ -139,7 +139,7 @@ public void MoveObj(IMoveable obj, int moveTime, double direction, long stateNum
if (isEnded)
{
- obj.IsMoving.Set(false);
+ obj.IsMoving.SetReturnOri(false);
EndMove(obj);
return;
}
@@ -184,7 +184,7 @@ public void MoveObj(IMoveable obj, int moveTime, double direction, long stateNum
}
if (isEnded)
{
- obj.IsMoving.Set(false);
+ obj.IsMoving.SetReturnOri(false);
EndMove(obj);
return;
}
@@ -224,7 +224,7 @@ public void MoveObj(IMoveable obj, int moveTime, double direction, long stateNum
}
} while (flag);
}
- obj.IsMoving.Set(false); // 结束移动
+ obj.IsMoving.SetReturnOri(false); // 结束移动
EndMove(obj);
}
}
diff --git a/logic/Gaming/ActionManager.cs b/logic/Gaming/ActionManager.cs
index 5fd1cc3b..6d83ff1f 100644
--- a/logic/Gaming/ActionManager.cs
+++ b/logic/Gaming/ActionManager.cs
@@ -450,12 +450,12 @@ public bool ClimbingThroughWindow(Character player)
player.ReSetPos(windowToPlayer + windowForClimb.Position);
}
- player.MoveSpeed.Set(player.SpeedOfClimbingThroughWindows);
+ player.MoveSpeed.SetReturnOri(player.SpeedOfClimbingThroughWindows);
moveEngine.MoveObj(player, (int)(GameData.numOfPosGridPerCell * 3 * 1000 / player.MoveSpeed / 2), (-1 * windowToPlayer).Angle(), stateNum);
Thread.Sleep((int)(GameData.numOfPosGridPerCell * 3 * 1000 / player.MoveSpeed / 2));
- player.MoveSpeed.Set(player.ReCalculateBuff(BuffType.AddSpeed, player.OrgMoveSpeed, GameData.MaxSpeed, GameData.MinSpeed));
+ player.MoveSpeed.SetReturnOri(player.ReCalculateBuff(BuffType.AddSpeed, player.OrgMoveSpeed, GameData.MaxSpeed, GameData.MinSpeed));
lock (player.ActionLock)
{
diff --git a/logic/Gaming/AttackManager.cs b/logic/Gaming/AttackManager.cs
index 2204cf61..70d61555 100644
--- a/logic/Gaming/AttackManager.cs
+++ b/logic/Gaming/AttackManager.cs
@@ -33,7 +33,7 @@ public AttackManager(Map gameMap, CharacterManager characterManager)
Debugger.Output(obj, " end move at " + obj.Position.ToString() + " At time: " + Environment.TickCount64);
if (obj.CanMove && ((Bullet)obj).TypeOfBullet != BulletType.JumpyDumpty)
BulletBomb((Bullet)obj, null);
- obj.CanMove.Set(false);
+ obj.CanMove.SetReturnOri(false);
}
);
this.characterManager = characterManager;
@@ -89,7 +89,7 @@ public bool TryRemoveBullet(Bullet bullet)
{
if (gameMap.Remove(bullet))
{
- bullet.CanMove.Set(false);
+ bullet.CanMove.SetReturnOri(false);
if (bullet.BulletBombRange > 0)
{
BombedBullet bombedBullet = new(bullet);
diff --git a/logic/Gaming/CharacterManager.cs b/logic/Gaming/CharacterManager.cs
index 6fdeedba..71c403ff 100644
--- a/logic/Gaming/CharacterManager.cs
+++ b/logic/Gaming/CharacterManager.cs
@@ -57,41 +57,8 @@ public void DoubleFactorTeacher()
}
gameMap.Add(newPlayer);
- newPlayer.TeamID.Set(teamID);
- newPlayer.PlayerID.Set(playerID);
- /* #region 人物装弹
- new Thread
- (
- () =>
- {
- while (!gameMap.Timer.IsGaming)
- Thread.Sleep(Math.Max(newPlayer.CD, GameData.checkInterval));
- long lastTime = Environment.TickCount64;
- new FrameRateTaskExecutor(
- loopCondition: () => gameMap.Timer.IsGaming && !newPlayer.IsRemoved,
- loopToDo: () =>
- {
- long nowTime = Environment.TickCount64;
- if (newPlayer.BulletNum == newPlayer.MaxBulletNum)
- lastTime = nowTime;
- else if (nowTime - lastTime >= newPlayer.CD)
- {
- _ = newPlayer.TryAddBulletNum();
- lastTime = nowTime;
- }
- },
- timeInterval: GameData.checkInterval,
- finallyReturn: () => 0
- )
- {
- AllowTimeExceed = true,
- }
- .Start();
- }
- )
- { IsBackground = true }.Start();
- #endregion
- */
+ newPlayer.TeamID.SetReturnOri(teamID);
+ newPlayer.PlayerID.SetReturnOri(playerID);
#region BGM,牵制得分更新
new Thread
(
@@ -311,7 +278,7 @@ public void BeAttacked(Student student, Bullet bullet)
if (student.CharacterType == CharacterType.StraightAStudent)
{
- ((WriteAnswers)student.FindActiveSkill(ActiveSkillType.WriteAnswers)).DegreeOfMeditation.Set(0);
+ ((WriteAnswers)student.FindActiveSkill(ActiveSkillType.WriteAnswers)).DegreeOfMeditation.SetReturnOri(0);
}
student.SetDegreeOfTreatment0();
diff --git a/logic/Gaming/Game.cs b/logic/Gaming/Game.cs
index 96db6753..02d783cb 100644
--- a/logic/Gaming/Game.cs
+++ b/logic/Gaming/Game.cs
@@ -84,7 +84,7 @@ public bool MovePlayer(long playerID, int moveTimeInMilliseconds, double angle)
else
{
#if DEBUG
- Console.WriteLine($"PlayerID:{playerID} player does not exists!");
+ Console.WriteLine($"playerID:{playerID} player does not exists!");
#endif
return false;
@@ -326,7 +326,7 @@ public void ClearAllLists()
{
foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
{
- player.CanMove.Set(false);
+ player.CanMove.SetReturnOri(false);
}
}
gameMap.GameObjDict[keyValuePair.Key].Clear();
diff --git a/logic/Gaming/SkillManager/SkillManager.ActiveSkill.cs b/logic/Gaming/SkillManager/SkillManager.ActiveSkill.cs
index 74ce47a5..88398ae7 100644
--- a/logic/Gaming/SkillManager/SkillManager.ActiveSkill.cs
+++ b/logic/Gaming/SkillManager/SkillManager.ActiveSkill.cs
@@ -196,7 +196,7 @@ public bool SparksNSplash(Character player, int AttackID)
}
if (homingMissile != null)
{
- homingMissile.CanMove.Set(true);
+ homingMissile.CanMove.SetReturnOri(true);
attackManager.moveEngine.MoveObj(homingMissile, GameData.checkIntervalWhenSparksNSplash - 1, (whoAttacked.Position - homingMissile.Position).Angle(), ++homingMissile.StateNum);
}
},
@@ -227,7 +227,7 @@ public bool WriteAnswers(Character player)
if (generator.Repair(((WriteAnswers)activeSkill).DegreeOfMeditation, player))
gameMap.AddNumOfRepairedGenerators();
Debugger.Output(player, "uses WriteAnswers in" + generator.ToString() + "with " + (((WriteAnswers)activeSkill).DegreeOfMeditation).ToString());
- ((WriteAnswers)activeSkill).DegreeOfMeditation.Set(0);
+ ((WriteAnswers)activeSkill).DegreeOfMeditation.SetReturnOri(0);
}
},
() =>
diff --git a/logic/Gaming/SkillManager/SkillManager.PassiveSkill.cs b/logic/Gaming/SkillManager/SkillManager.PassiveSkill.cs
index b22254ef..3cc89c78 100644
--- a/logic/Gaming/SkillManager/SkillManager.PassiveSkill.cs
+++ b/logic/Gaming/SkillManager/SkillManager.PassiveSkill.cs
@@ -26,7 +26,7 @@ public void Meditate(Character player)
() =>
{
if (player.Commandable() && player.PlayerState != PlayerStateType.Fixing) activeSkill.DegreeOfMeditation.Add(learningDegree * GameData.frameDuration);
- else activeSkill.DegreeOfMeditation.Set(0);
+ else activeSkill.DegreeOfMeditation.SetReturnOri(0);
//Debugger.Output(player, "with " + (((WriteAnswers)activeSkill).DegreeOfMeditation).ToString());
},
timeInterval: GameData.frameDuration,
diff --git a/logic/Preparation/Interface/ISkill.cs b/logic/Preparation/Interface/ISkill.cs
index bc3f2d60..65b62708 100644
--- a/logic/Preparation/Interface/ISkill.cs
+++ b/logic/Preparation/Interface/ISkill.cs
@@ -174,7 +174,8 @@ public class WriteAnswers : ActiveSkill
public override int SkillCD => GameData.commonSkillCD;
public override int DurationTime => 0;
- public AtomicInt DegreeOfMeditation { get; } = new(0);
+ private AtomicInt degreeOfMeditation = new(0);
+ public AtomicInt DegreeOfMeditation { get => degreeOfMeditation; }
}
public class SummonGolem : ActiveSkill
diff --git a/logic/Preparation/Utility/SafeValue.cs b/logic/Preparation/Utility/SafeValue.cs
index 45a4b660..dcbf5029 100644
--- a/logic/Preparation/Utility/SafeValue.cs
+++ b/logic/Preparation/Utility/SafeValue.cs
@@ -5,7 +5,7 @@ namespace Preparation.Utility
{
//理论上结构体最好不可变,这里采用了可变结构。
//其对应属性不应当有set访问器,避免不安全的=赋值
- public struct AtomicInt
+ public class AtomicInt
{
private int v;
public AtomicInt(int x)
@@ -15,18 +15,17 @@ public AtomicInt(int x)
public override string ToString() => Interlocked.CompareExchange(ref v, -1, -1).ToString();
public int Get() => Interlocked.CompareExchange(ref v, -1, -1);
public static implicit operator int(AtomicInt aint) => Interlocked.CompareExchange(ref aint.v, -1, -1);
- public int Set(int value) => Interlocked.Exchange(ref v, value);
-
+ /// 返回操作前的值
+ public int SetReturnOri(int value) => Interlocked.Exchange(ref v, value);
public int Add(int x) => Interlocked.Add(ref v, x);
public int Sub(int x) => Interlocked.Add(ref v, -x);
public int Inc() => Interlocked.Increment(ref v);
public int Dec() => Interlocked.Decrement(ref v);
-
- public void CompareExchange(int newV, int compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
/// 返回操作前的值
public int CompareExReturnOri(int newV, int compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}
- public struct AtomicLong
+
+ public class AtomicLong
{
private long v;
public AtomicLong(long x)
@@ -36,18 +35,17 @@ public AtomicLong(long x)
public override string ToString() => Interlocked.Read(ref v).ToString();
public long Get() => Interlocked.Read(ref v);
public static implicit operator long(AtomicLong aint) => Interlocked.Read(ref aint.v);
- public long Set(long value) => Interlocked.Exchange(ref v, value);
-
+ /// 返回操作前的值
+ public long SetReturnOri(long value) => Interlocked.Exchange(ref v, value);
public long Add(long x) => Interlocked.Add(ref v, x);
public long Sub(long x) => Interlocked.Add(ref v, -x);
public long Inc() => Interlocked.Increment(ref v);
public long Dec() => Interlocked.Decrement(ref v);
-
- public void CompareExchange(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
/// 返回操作前的值
public long CompareExReturnOri(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}
- public struct AtomicBool
+
+ public class AtomicBool
{
private int v;//v==0为false,v==1为true
public AtomicBool(bool x)
@@ -57,36 +55,34 @@ public AtomicBool(bool x)
public override string ToString() => (Interlocked.CompareExchange(ref v, -2, -2) == 0) ? "false" : "true";
public bool Get() => (Interlocked.CompareExchange(ref v, -1, -1) != 0);
public static implicit operator bool(AtomicBool abool) => (Interlocked.CompareExchange(ref abool.v, -1, -1) != 0);
-
- public bool Set(bool value) => (Interlocked.Exchange(ref v, value ? 1 : 0) != 0);
-
+ /// 返回操作前的值
+ public bool SetReturnOri(bool value) => (Interlocked.Exchange(ref v, value ? 1 : 0) != 0);
/// 赋值前的值是否与将赋予的值不相同
public bool TrySet(bool value)
{
return (Interlocked.CompareExchange(ref v, value ? 1 : 0, value ? 0 : 1) ^ (value ? 1 : 0)) != 0;
}
-
public bool And(bool x) => Interlocked.And(ref v, x ? 1 : 0) != 0;
public bool Or(bool x) => Interlocked.Or(ref v, x ? 1 : 0) != 0;
}
///
- /// 一个能记录Start后完成多少进度的进度条(long),
+ /// 根据时间推算Start后完成多少进度的进度条(long)。
/// 只允许Start时修改needTime(请确保大于0);
/// 支持TrySet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
- /// 不支持暂停
+ /// 通过原子操作实现。
///
- public struct LongProgressContinuously
+ public class LongProgressByTime
{
private long endT = long.MaxValue;
private long needT;
- public LongProgressContinuously(long needTime)
+ public LongProgressByTime(long needTime)
{
- if (needTime <= 0) Debugger.Output("Bug:LongProgressContinuously.needTime (" + needTime.ToString() + ") is less than 0.");
+ if (needTime <= 0) Debugger.Output("Bug:LongProgressByTime.needTime (" + needTime.ToString() + ") is less than 0.");
this.needT = needTime;
}
- public LongProgressContinuously()
+ public LongProgressByTime()
{
this.needT = long.MaxValue;
}
@@ -118,7 +114,7 @@ public long GetProgress(long time)
///
/// <0则表明未开始
///
- public static implicit operator long(LongProgressContinuously pLong) => pLong.GetProgress();
+ public static implicit operator long(LongProgressByTime pLong) => pLong.GetProgress();
///
/// GetProgressDouble<0则表明未开始
@@ -134,13 +130,13 @@ public bool Start(long needTime)
{
if (needTime <= 0)
{
- Debugger.Output("Warning:Start LongProgressContinuously with the needTime (" + needTime.ToString() + ") which is less than 0.");
+ Debugger.Output("Warning:Start LongProgressByTime with the needTime (" + needTime.ToString() + ") which is less than 0.");
return false;
}
//规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
- if (needTime <= 2) Debugger.Output("Warning:the field of LongProgressContinuously is " + needTime.ToString() + ",which is too small.");
- Interlocked.Exchange(ref this.needT, needTime);
+ if (needTime <= 2) Debugger.Output("Warning:the field of LongProgressByTime is " + needTime.ToString() + ",which is too small.");
+ Interlocked.Exchange(ref needT, needTime);
return true;
}
public bool Start()
@@ -149,7 +145,13 @@ public bool Start()
if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
return true;
}
+ ///
+ /// 使进度条强制终止清零
+ ///
public void Set0() => Interlocked.Exchange(ref endT, long.MaxValue);
+ ///
+ /// 使未完成的进度条终止清零
+ ///
public bool TrySet0()
{
if (Environment.TickCount64 < Interlocked.CompareExchange(ref endT, -2, -2))
@@ -162,10 +164,34 @@ public bool TrySet0()
//增加其他新的写操作可能导致不安全
}
+ /*
+ ///
+ /// 记录(不是根据时间)完成多少进度的进度条(long)。
+ ///
+ public struct IntProgressByAdding
+ {
+ private int completedProgress = -1;
+ private int requiredProgress;
+ public IntProgressByAdding(int completedProgress, int requiredProgress)
+ {
+ this.completedProgress = completedProgress;
+ this.requiredProgress = requiredProgress;
+ }
+ public IntProgressByAdding(int requiredProgress)
+ {
+ this.requiredProgress = requiredProgress;
+ }
+ public IntProgressByAdding()
+ {
+ this.requiredProgress=int.MaxValue;
+ }
+ }
+ */
+
///
/// 一个保证在[0,maxValue]的可变int,支持可变的maxValue(请确保大于0)
///
- public struct IntWithVariableRange
+ public class IntWithVariableRange
{
private int v;
private int maxV;
@@ -310,7 +336,7 @@ public int TryAddAll(int addV)
///
/// 一个保证在[0,maxValue]的可变long,支持可变的maxValue(请确保大于0)
///
- public struct LongWithVariableRange
+ public class LongWithVariableRange
{
private long v;
private long maxV;
@@ -319,7 +345,7 @@ public LongWithVariableRange(long value, long maxValue)
{
if (maxValue < 0)
{
- Debugger.Output("Warning:Try to set LongWithVariableRange.maxValue to " + maxValue.ToString() + ".");
+ Debugger.Output("Warning:Try to set SafaValues.LongWithVariableRange.maxValue to " + maxValue.ToString() + ".");
maxValue = 0;
}
v = value < maxValue ? value : maxValue;
@@ -332,7 +358,7 @@ public LongWithVariableRange(long maxValue)
{
if (maxValue < 0)
{
- Debugger.Output("Warning:Try to set LongWithVariableRange.maxValue to " + maxValue.ToString() + ".");
+ Debugger.Output("Warning:Try to set SafaValues.LongWithVariableRange.maxValue to " + maxValue.ToString() + ".");
maxValue = 0;
}
v = this.maxV = maxValue;
@@ -455,7 +481,7 @@ public long TryAddAll(long addV)
///
/// 一个保证在[0,maxNum],每CDms自动更新的可变int,支持可变的CD、maxNum(请确保大于0)
///
- public struct IntNumUpdateByCD
+ public class IntNumUpdateByCD
{
private int num;
private int maxNum;
@@ -623,9 +649,9 @@ public void SetCD(int cd)
{
lock (numLock)
{
- if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateByCD.cd to " + cd.ToString() + ".");
+ if (cd <= 0) Debugger.Output("Bug:SetReturnOri IntNumUpdateByCD.cd to " + cd.ToString() + ".");
this.cd = cd;
}
}
}
-}
+}
\ No newline at end of file
From 66d2b277b96554dafcede27326dcf4e369c621c7 Mon Sep 17 00:00:00 2001
From: shangfengh <3495281661@qq.com>
Date: Mon, 10 Jul 2023 01:59:24 +0800
Subject: [PATCH 9/9] fix: :bug: fix the chest progress
---
logic/GameClass/GameObj/Map/Chest.cs | 1 -
logic/Preparation/Utility/SafeValue.cs | 29 ++++++++++++++++++++++++--
logic/Server/CopyInfo.cs | 7 +++----
3 files changed, 30 insertions(+), 7 deletions(-)
diff --git a/logic/GameClass/GameObj/Map/Chest.cs b/logic/GameClass/GameObj/Map/Chest.cs
index 8e450e53..0fc69794 100644
--- a/logic/GameClass/GameObj/Map/Chest.cs
+++ b/logic/GameClass/GameObj/Map/Chest.cs
@@ -1,6 +1,5 @@
using Preparation.Interface;
using Preparation.Utility;
-using System;
namespace GameClass.GameObj
{
diff --git a/logic/Preparation/Utility/SafeValue.cs b/logic/Preparation/Utility/SafeValue.cs
index dcbf5029..934fbe22 100644
--- a/logic/Preparation/Utility/SafeValue.cs
+++ b/logic/Preparation/Utility/SafeValue.cs
@@ -84,7 +84,7 @@ public LongProgressByTime(long needTime)
}
public LongProgressByTime()
{
- this.needT = long.MaxValue;
+ this.needT = 0;
}
public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2);
@@ -93,6 +93,7 @@ public bool IsFinished()
{
return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
}
+ public bool IsOpened() => Interlocked.Read(ref endT) != long.MaxValue;
///
/// GetProgress<0则表明未开始
///
@@ -102,6 +103,13 @@ public long GetProgress()
if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
}
+ public long GetNonNegativeProgress()
+ {
+ long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
+ long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
+ return progress < 0 ? 0 : progress;
+ }
///
/// GetProgress<0则表明未开始
///
@@ -111,6 +119,13 @@ public long GetProgress(long time)
if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
}
+ public long GetNonNegativeProgress(long time)
+ {
+ long cutime = Interlocked.Read(ref endT) - time;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
+ long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
+ return progress < 0 ? 0 : progress;
+ }
///
/// <0则表明未开始
///
@@ -123,7 +138,17 @@ public double GetProgressDouble()
{
long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
if (cutime <= 0) return 1;
- return 1.0 - ((double)cutime / Interlocked.CompareExchange(ref needT, -2, -2));
+ long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
+ if (needTime == 0) return 0;
+ return 1.0 - ((double)cutime / needTime);
+ }
+ public double GetNonNegativeProgressDouble(long time)
+ {
+ long cutime = Interlocked.Read(ref endT) - time;
+ if (cutime <= 0) return 1;
+ long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
+ if (needTime <= cutime) return 0;
+ return 1.0 - ((double)cutime / needTime);
}
public bool Start(long needTime)
diff --git a/logic/Server/CopyInfo.cs b/logic/Server/CopyInfo.cs
index 067ecccc..7532cd64 100644
--- a/logic/Server/CopyInfo.cs
+++ b/logic/Server/CopyInfo.cs
@@ -269,16 +269,15 @@ private static MessageOfObj Door(Door door)
}
private static MessageOfObj Chest(Chest chest, long time)
{
- MessageOfObj msg = new()
+ return new()
{
ChestMessage = new()
{
X = chest.Position.x,
- Y = chest.Position.y
+ Y = chest.Position.y,
+ Progress = (int)(chest.OpenProgress.GetNonNegativeProgressDouble(time) * GameData.degreeOfOpenedChest)
}
};
- msg.ChestMessage.Progress = (int)chest.OpenProgress.GetProgress(time);
- return msg;
}
}
}