diff --git a/Source/Framework/Dynamic/EventMap.cs b/Source/Framework/Dynamic/EventMap.cs
index 416750258..feff3ebb7 100644
--- a/Source/Framework/Dynamic/EventMap.cs
+++ b/Source/Framework/Dynamic/EventMap.cs
@@ -102,19 +102,25 @@ namespace Framework.Dynamic
}
///
- /// Creates new event entry in map.
+ /// Schedules a new event.
///
/// The id of the new event.
- /// The time in milliseconds as TimeSpan until the event occurs.
+ /// The time in milliseconds until the event occurs.
/// The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
/// The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases.
public void ScheduleEvent(uint eventId, TimeSpan time, uint group = 0, byte phase = 0)
{
- ScheduleEvent(eventId, (uint)time.TotalMilliseconds, group, phase);
+ if (group != 0 && group <= 8)
+ eventId |= (uint)(1 << ((int)group + 15));
+
+ if (phase != 0 && phase <= 8)
+ eventId |= (uint)(1 << (phase + 23));
+
+ _eventMap.Add(_time + (uint)time.TotalMilliseconds, eventId);
}
///
- /// Creates new event entry in map.
+ /// Schedules a new event.
///
/// The id of the new event.
/// The minimum time until the event occurs as TimeSpan type.
@@ -123,26 +129,7 @@ namespace Framework.Dynamic
/// The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases.
public void ScheduleEvent(uint eventId, TimeSpan minTime, TimeSpan maxTime, uint group = 0, byte phase = 0)
{
- ScheduleEvent(eventId, RandomHelper.URand(minTime.TotalMilliseconds, maxTime.TotalMilliseconds), group, phase);
- }
-
-
- ///
- /// Creates new event entry in map.
- ///
- /// The id of the new event.
- /// The time in milliseconds until the event occurs.
- /// The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
- /// The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases.
- public void ScheduleEvent(uint eventId, uint time, uint group = 0, byte phase = 0)
- {
- if (group != 0 && group <= 8)
- eventId |= (uint)(1 << ((int)group + 15));
-
- if (phase != 0 && phase <= 8)
- eventId |= (uint)(1 << (phase + 23));
-
- _eventMap.Add(_time + time, eventId);
+ ScheduleEvent(eventId, RandomHelper.RandTime(minTime, maxTime), group, phase);
}
///
@@ -154,7 +141,8 @@ namespace Framework.Dynamic
/// The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases.
public void RescheduleEvent(uint eventId, TimeSpan time, uint group = 0, byte phase = 0)
{
- RescheduleEvent(eventId, (uint)time.TotalMilliseconds, group, phase);
+ CancelEvent(eventId);
+ RescheduleEvent(eventId, time, group, phase);
}
///
@@ -167,62 +155,30 @@ namespace Framework.Dynamic
/// The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases.
void RescheduleEvent(uint eventId, TimeSpan minTime, TimeSpan maxTime, uint group = 0, byte phase = 0)
{
- RescheduleEvent(eventId, RandomHelper.URand(minTime.TotalMilliseconds, maxTime.TotalMilliseconds), group, phase);
- }
-
- ///
- /// Cancels the given event and reschedules it.
- ///
- /// The id of the event.
- /// The time in milliseconds until the event occurs.
- /// The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group.
- /// The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases.
- public void RescheduleEvent(uint eventId, uint time, uint group = 0, byte phase = 0)
- {
- CancelEvent(eventId);
- ScheduleEvent(eventId, time, group, phase);
+ RescheduleEvent(eventId, RandomHelper.RandTime(minTime, maxTime), group, phase);
}
///
- /// Repeats the mostly recently executed event.
+ /// Repeats the most recently executed event.
///
- /// Time until in ms as TimeSpan the event occurs
+ /// Time until the event occurs as TimeSpan.
public void Repeat(TimeSpan time)
{
- Repeat((uint)time.TotalMilliseconds);
+ _eventMap.Add(_time + (uint)time.TotalMilliseconds, _lastEvent);
}
///
- /// Repeats the mostly recently executed event.
+ /// Repeats the most recently executed event. Equivalent to Repeat(urand(minTime, maxTime)
///
- /// Time until the event occurs
- public void Repeat(uint time)
- {
- _eventMap.Add(_time + time, _lastEvent);
- }
-
- ///
- /// Repeats the mostly recently executed event. Equivalent to Repeat(urand(minTime, maxTime)
- ///
- /// Min Time as TimeSpan until the event occurs.
- /// Max Time as TimeSpan until the event occurs.
+ /// The minimum time until the event occurs as TimeSpan type.
+ /// The maximum time until the event occurs as TimeSpan type.
public void Repeat(TimeSpan minTime, TimeSpan maxTime)
{
- Repeat((uint)minTime.TotalMilliseconds, (uint)maxTime.TotalMilliseconds);
+ Repeat(RandomHelper.RandTime(minTime, maxTime));
}
///
- /// Repeats the mostly recently executed event. Equivalent to Repeat(urand(minTime, maxTime)
- ///
- /// Min Time until the event occurs.
- /// Max Time until the event occurs.
- public void Repeat(uint minTime, uint maxTime)
- {
- Repeat(RandomHelper.URand(minTime, maxTime));
- }
-
- ///
- /// Returns the next event to execute and removes it from map.
+ /// Returns the next event to be executed and removes it from map.
///
/// Id of the event to execute.
///
@@ -256,39 +212,31 @@ namespace Framework.Dynamic
}
///
- /// Delays all events in the map. If delay is greater than or equal internal timer, delay will be 0.
+ /// Delays all events. If delay is greater than or equal internal timer, delay will be 0.
///
- /// Amount of delay in ms as TimeSpan.
+ /// Amount of delay as TimeSpan type.
public void DelayEvents(TimeSpan delay)
{
- DelayEvents((uint)delay.TotalMilliseconds);
- }
-
- ///
- /// Delays all events in the map. If delay is greater than or equal internal timer, delay will be 0.
- ///
- /// Amount of delay.
- public void DelayEvents(uint delay)
- {
- _time = delay < _time ? _time - delay : 0;
+ _time = (uint)(delay.TotalMilliseconds < _time ? _time - delay.TotalMilliseconds : 0);
}
///
/// Delay all events of the same group.
///
- /// Amount of delay.
+ /// Amount of delay as TimeSpan type.
/// Group of the events.
- public void DelayEvents(uint delay, uint group)
+ public void DelayEvents(TimeSpan delay, uint group)
{
if (group == 0 || group > 8 || Empty())
return;
+
MultiMap delayed = new();
foreach (var pair in _eventMap.KeyValueList)
{
if (Convert.ToBoolean(pair.Value & (1 << (int)(group + 15))))
{
- delayed.Add(pair.Key + delay, pair.Value);
+ delayed.Add(pair.Key + (uint)delay.TotalMilliseconds, pair.Value);
_eventMap.Remove(pair.Key, pair.Value);
}
}
@@ -330,7 +278,7 @@ namespace Framework.Dynamic
}
///
- /// Returns closest occurence of specified event.
+ /// Returns closest occurrence of specified event.
///
/// Wanted event id.
/// Time of found event.
diff --git a/Source/Framework/Util/RandomHelper.cs b/Source/Framework/Util/RandomHelper.cs
index 7038b336c..2b3197932 100644
--- a/Source/Framework/Util/RandomHelper.cs
+++ b/Source/Framework/Util/RandomHelper.cs
@@ -103,5 +103,13 @@ public class RandomHelper
return args[randIndex];
}
+
+ public static TimeSpan RandTime(TimeSpan min, TimeSpan max)
+ {
+ double diff = max.TotalMilliseconds - min.TotalMilliseconds;
+ Cypher.Assert(diff >= 0);
+ Cypher.Assert(diff <= 0xFFFFFFFF);
+ return min + TimeSpan.FromMilliseconds(URand(0, (uint)diff));
+ }
}