ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

GAS: Ability Activation Cooldown Manager

C++ (Unreal)

Goal -- WPM

Ready
Exercise Algorithm Area
1class UAbilityCooldownManager
2{
3public:
4// Stores the time when each ability's cooldown will end.
5// Key: Ability Tag, Value: End Cooldown Time (Game Time)
6TMap<FGameplayTag, float> CooldownEndTimes;
7
8// Constructor
9UAbilityCooldownManager()
10{
11// Initialize any default settings if needed.
12}
13
14// Applies a cooldown to an ability.
15// AbilityTag: The tag identifying the ability.
16// CooldownDuration: The duration of the cooldown in seconds.
17// CurrentGameTime: The current game time.
18void ApplyCooldown(const FGameplayTag& AbilityTag, float CooldownDuration, float CurrentGameTime)
19{
20if (CooldownDuration <= 0.0f)
21{
22// No cooldown needed for zero or negative durations.
23RemoveCooldown(AbilityTag);
24return;
25}
26
27float CooldownEndTime = CurrentGameTime + CooldownDuration;
28CooldownEndTimes.Add(AbilityTag, CooldownEndTime);
29}
30
31// Checks if an ability is currently on cooldown.
32// AbilityTag: The tag identifying the ability.
33// CurrentGameTime: The current game time.
34// Returns true if the ability is on cooldown, false otherwise.
35bool IsAbilityOnCooldown(const FGameplayTag& AbilityTag, float CurrentGameTime) const
36{
37float* CooldownEndTime = CooldownEndTimes.Find(AbilityTag);
38
39if (CooldownEndTime)
40{
41// If the cooldown end time is in the past or exactly now, the cooldown has expired.
42return CurrentGameTime < *CooldownEndTime;
43}
44
45// Ability not found in cooldown map, so it's not on cooldown.
46return false;
47}
48
49// Removes a cooldown for a specific ability.
50// AbilityTag: The tag identifying the ability.
51void RemoveCooldown(const FGameplayTag& AbilityTag)
52{
53CooldownEndTimes.Remove(AbilityTag);
54}
55
56// Clears all active cooldowns.
57void ClearAllCooldowns()
58{
59CooldownEndTimes.Empty();
60}
61
62// Gets the remaining cooldown time for an ability.
63// AbilityTag: The tag identifying the ability.
64// CurrentGameTime: The current game time.
65// Returns the remaining cooldown time in seconds, or 0.0f if not on cooldown.
66float GetRemainingCooldownTime(const FGameplayTag& AbilityTag, float CurrentGameTime) const
67{
68const float* CooldownEndTime = CooldownEndTimes.Find(AbilityTag);
69
70if (CooldownEndTime)
71{
72float RemainingTime = *CooldownEndTime - CurrentGameTime;
73// Ensure remaining time is not negative due to floating point inaccuracies or time passing.
74return FMath::Max(0.0f, RemainingTime);
75}
76
77return 0.0f;
78}
79
80// Updates all cooldowns, removing those that have expired.
81// CurrentGameTime: The current game time.
82void TickCooldowns(float CurrentGameTime)
83{
84// Iterate through a copy of keys to avoid iterator invalidation issues.
85TArray<FGameplayTag> CooldownTagsToRemove;
86for (const auto& Pair : CooldownEndTimes)
87{
88if (CurrentGameTime >= Pair.Value)
89{
90CooldownTagsToRemove.Add(Pair.Key);
91}
92}
93
94// Remove expired cooldowns.
95for (const FGameplayTag& Tag : CooldownTagsToRemove)
96{
97CooldownEndTimes.Remove(Tag);
98}
99}
100
101private:
102// Helper function to ensure a tag is valid (e.g., not empty).
103bool IsValidTag(const FGameplayTag& Tag) const
104{
105return Tag.IsValid();
106}
107};
Algorithm description viewbox

GAS: Ability Activation Cooldown Manager

Algorithm description:

This C++ code implements a cooldown manager for Unreal Engine's Gameplay Ability System. It allows developers to track and manage cooldowns for various player abilities. The system uses a map to store when each ability's cooldown will end, enabling checks for active cooldowns and calculation of remaining time. This is crucial for preventing players from spamming powerful abilities and maintaining game balance.

Algorithm explanation:

The `UAbilityCooldownManager` class uses a `TMap` to store the expiration time for each ability's cooldown. The keys are `FGameplayTag`s representing abilities, and the values are `float`s representing the game time at which the cooldown ends. The `ApplyCooldown` function adds or updates an entry in the map, calculating the end time based on the current game time and cooldown duration. `IsAbilityOnCooldown` checks if the current game time is before the stored end time for a given ability. `GetRemainingCooldownTime` calculates the difference between the end time and current time, ensuring it's not negative. `TickCooldowns` iterates through the map and removes entries where the cooldown has expired. Edge cases like zero or negative cooldown durations are handled by immediately removing the cooldown. Floating-point precision is managed by comparing current time against the end time. The time complexity for `ApplyCooldown`, `IsAbilityOnCooldown`, `RemoveCooldown`, and `GetRemainingCooldownTime` is typically O(1) on average due to the hash-based nature of `TMap`. `TickCooldowns` has a time complexity of O(N), where N is the number of active cooldowns, as it may iterate through all entries. Space complexity is O(M), where M is the number of abilities that can have a cooldown simultaneously.

Pseudocode:

Class AbilityCooldownManager:
  Map CooldownEndTimes (Tag -> EndTime)

  Function ApplyCooldown(AbilityTag, Duration, CurrentTime):
    If Duration <= 0:
      RemoveCooldown(AbilityTag)
      Return
    EndTime = CurrentTime + Duration
    Add/Update CooldownEndTimes[AbilityTag] = EndTime

  Function IsAbilityOnCooldown(AbilityTag, CurrentTime):
    If AbilityTag exists in CooldownEndTimes:
      Return CurrentTime < CooldownEndTimes[AbilityTag]
    Else:
      Return False

  Function RemoveCooldown(AbilityTag):
    Remove AbilityTag from CooldownEndTimes

  Function ClearAllCooldowns():
    Clear CooldownEndTimes

  Function GetRemainingCooldownTime(AbilityTag, CurrentTime):
    If AbilityTag exists in CooldownEndTimes:
      Remaining = CooldownEndTimes[AbilityTag] - CurrentTime
      Return Max(0.0, Remaining)
    Else:
      Return 0.0

  Function TickCooldowns(CurrentTime):
    For each Tag, EndTime in CooldownEndTimes:
      If CurrentTime >= EndTime:
        Add Tag to list of tags to remove
    For each Tag in tags to remove:
      Remove Tag from CooldownEndTimes