forked from wowsims/cata
-
Notifications
You must be signed in to change notification settings - Fork 38
[Feature] Generic secondary resources #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
bbf50c3
[Feature] Generic secondary resouces
2e79f66
Merge branch 'master' into feature/generic-resources
1337LutZ 837193f
Remove unintended file
eda54c5
[Feature]Generic Resoruces: Update implementation and replace Holy P…
5d1fe07
[Feature] Generic Resources: Remove line comments from test
98dd829
[Feature] Generic Resource: Remove old proto mappings
ddea63f
[Feature] Generic resources - Part 1 APL implementation
758fb68
[Feature] Generic Resource: Add support for callbacks
c49ff3c
[Feature] Generic Resource: Add missing callback invoke
5d8d680
Add SecondaryResource class & extend APL dynamic string
1337LutZ 1bd49b8
Merge branch 'feature/generic-resources' of https://github.com/wowsim…
1337LutZ d53ff8a
[Feature] Generic Resource: Fix APL
b1592a1
[Feature] Generic Resources: Make bar track Int32
0022a93
[Feature] Generic Resource: Update resource implementation
c4c9ecf
[Feature] Generic Resources: Make resource bar private to core
014be3c
[Feature] Generic Resource: Map secondary resource names correctly in…
e1ed415
Merge remote-tracking branch 'origin/master' into feature/generic-res…
5ac1cb8
[Feature] Generic resource: Remove chi from secondary resources
44bce62
Merge branch 'master' into feature/generic-resources
54c93d8
[Feature] Generic Resouce: Fixup unintended removed code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -872,5 +872,4 @@ enum LogLevel { | |
| Error = 2; | ||
|
|
||
| Undefined = -1; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| // Implements a generic resource bar that can be used to implement secondary resources | ||
| // TODO: Check whether pre-pull OOC resource loss needs to be supported for DemonicFury | ||
| package core | ||
|
|
||
| import ( | ||
| "github.com/wowsims/mop/sim/core/proto" | ||
| ) | ||
|
|
||
| type OnGainCallback func(gain int32, realGain int32) | ||
| type OnSpendCallback func(amount int32) | ||
|
|
||
| type SecondaryResourceBar interface { | ||
| CanSpend(limit int32) bool // Check whether the current resource is available or not | ||
| Spend(amount int32, action ActionID, sim *Simulation) // Spend the specified amount of resource | ||
| SpendUpTo(limit int32, action ActionID, sim *Simulation) int32 // Spends as much resource as possible up to the speciefied limit; Returns the amount of resource spent | ||
| Gain(amount int32, action ActionID, sim *Simulation) // Gain the amount specified from the action | ||
| Reset(sim *Simulation) // Resets the current resource bar | ||
| Value() int32 // Returns the current amount of resource | ||
| RegisterOnGain(callback OnGainCallback) // Registers a callback that will be called. Gain = amount gained, realGain = actual amount gained due to caps | ||
| RegisterOnSpend(callback OnSpendCallback) // Registers a callback that will be called when the resource was spend | ||
| } | ||
|
|
||
| type SecondaryResourceConfig struct { | ||
| Type proto.SecondaryResourceType // The type of resource the bar tracks | ||
| Max int32 // The maximum amount the bar tracks | ||
| Default int32 // The default value this bar should be initialized with | ||
| } | ||
|
|
||
| // Default implementation of SecondaryResourceBar | ||
| // Use RegisterSecondaryResourceBar to intantiate the resource bar | ||
| type DefaultSecondaryResourceBarImpl struct { | ||
| config SecondaryResourceConfig | ||
| value int32 | ||
| unit *Unit | ||
| metrics map[ActionID]*ResourceMetrics | ||
| onGain []OnGainCallback | ||
| onSpend []OnSpendCallback | ||
| } | ||
|
|
||
| // CanSpend implements SecondaryResourceBar. | ||
| func (bar *DefaultSecondaryResourceBarImpl) CanSpend(limit int32) bool { | ||
| return bar.value >= limit | ||
| } | ||
|
|
||
| // Gain implements SecondaryResourceBar. | ||
| func (bar *DefaultSecondaryResourceBarImpl) Gain(amount int32, action ActionID, sim *Simulation) { | ||
| if amount < 0 { | ||
| panic("Can not gain negative amount") | ||
| } | ||
|
|
||
| oldValue := bar.value | ||
| bar.value = min(bar.value+amount, bar.config.Max) | ||
| amountGained := bar.value - oldValue | ||
| metrics := bar.GetMetric(action) | ||
| metrics.AddEvent(float64(amount), float64(amountGained)) | ||
| if sim.Log != nil { | ||
| bar.unit.Log( | ||
| sim, | ||
| "Gained %d %s from %s (%d --> %d) of %d total.", | ||
| amountGained, | ||
| proto.SecondaryResourceType_name[int32(bar.config.Type)], | ||
| action, | ||
| oldValue, | ||
| bar.value, | ||
| bar.config.Max, | ||
| ) | ||
| } | ||
|
|
||
| bar.invokeOnGain(amount, amountGained) | ||
| } | ||
|
|
||
| // Reset implements SecondaryResourceBar. | ||
| func (bar *DefaultSecondaryResourceBarImpl) Reset(sim *Simulation) { | ||
| bar.value = 0 | ||
| if bar.config.Default > 0 { | ||
| bar.Gain(bar.config.Default, ActionID{SpellID: int32(bar.config.Type)}, sim) | ||
| } | ||
| } | ||
|
|
||
| // Spend implements SecondaryResourceBar. | ||
| func (bar *DefaultSecondaryResourceBarImpl) Spend(amount int32, action ActionID, sim *Simulation) { | ||
| if amount > bar.value { | ||
| panic("Trying to spend more resource than is available.") | ||
| } | ||
|
|
||
| if amount < 0 { | ||
| panic("Trying to spend negative amount.") | ||
| } | ||
|
|
||
| metrics := bar.GetMetric(action) | ||
| if sim.Log != nil { | ||
| bar.unit.Log( | ||
| sim, | ||
| "Spent %d %s from %s (%d --> %d) of %d total.", | ||
| amount, | ||
| proto.SecondaryResourceType_name[int32(bar.config.Type)], | ||
| metrics.ActionID, | ||
| bar.value, | ||
| bar.value-amount, | ||
| bar.config.Max, | ||
| ) | ||
| } | ||
|
|
||
| metrics.AddEvent(float64(-amount), float64(-amount)) | ||
| bar.invokeOnSpend(amount) | ||
| bar.value -= amount | ||
| } | ||
|
|
||
| // SpendUpTo implements SecondaryResourceBar. | ||
| func (bar *DefaultSecondaryResourceBarImpl) SpendUpTo(limit int32, action ActionID, sim *Simulation) int32 { | ||
| if bar.value > limit { | ||
| bar.Spend(limit, action, sim) | ||
| return limit | ||
| } | ||
|
|
||
| bar.Spend(bar.value, action, sim) | ||
| return bar.value | ||
| } | ||
|
|
||
| // Value implements SecondaryResourceBar. | ||
| func (bar *DefaultSecondaryResourceBarImpl) Value() int32 { | ||
| return bar.value | ||
| } | ||
|
|
||
| func (bar *DefaultSecondaryResourceBarImpl) GetMetric(action ActionID) *ResourceMetrics { | ||
| metric, ok := bar.metrics[action] | ||
| if !ok { | ||
| metric = bar.unit.NewGenericMetric(action) | ||
| bar.metrics[action] = metric | ||
| } | ||
|
|
||
| return metric | ||
| } | ||
|
|
||
| func (bar *DefaultSecondaryResourceBarImpl) RegisterOnGain(callback OnGainCallback) { | ||
| if callback == nil { | ||
| panic("Can not register nil callback") | ||
| } | ||
|
|
||
| bar.onGain = append(bar.onGain, callback) | ||
| } | ||
|
|
||
| func (bar *DefaultSecondaryResourceBarImpl) RegisterOnSpend(callback OnSpendCallback) { | ||
| if callback == nil { | ||
| panic("Can not register nil callback") | ||
| } | ||
|
|
||
| bar.onSpend = append(bar.onSpend, callback) | ||
| } | ||
|
|
||
| func (bar *DefaultSecondaryResourceBarImpl) invokeOnGain(gain int32, realGain int32) { | ||
| for _, callback := range bar.onGain { | ||
| callback(gain, realGain) | ||
| } | ||
| } | ||
|
|
||
| func (bar *DefaultSecondaryResourceBarImpl) invokeOnSpend(amount int32) { | ||
| for _, callback := range bar.onSpend { | ||
| callback(amount) | ||
| } | ||
| } | ||
|
|
||
| func (unit *Unit) NewDefaultSecondaryResourceBar(config SecondaryResourceConfig) *DefaultSecondaryResourceBarImpl { | ||
| if config.Type <= 0 { | ||
| panic("Invalid SecondaryResourceType given.") | ||
| } | ||
|
|
||
| if config.Max <= 0 { | ||
| panic("Invalid maximum resource value given.") | ||
| } | ||
|
|
||
| if config.Default < 0 || config.Default > config.Max { | ||
| panic("Invalid default value given for resource bar") | ||
| } | ||
|
|
||
| return &DefaultSecondaryResourceBarImpl{ | ||
| config: config, | ||
| unit: unit, | ||
| metrics: make(map[ActionID]*ResourceMetrics), | ||
| onGain: []OnGainCallback{}, | ||
| onSpend: []OnSpendCallback{}, | ||
| } | ||
| } | ||
|
|
||
| func (unit *Unit) RegisterSecondaryResourceBar(config SecondaryResourceBar) { | ||
| if unit.secondaryResourceBar != nil { | ||
| panic("A secondary resource bar has already been registered.") | ||
| } | ||
|
|
||
| unit.secondaryResourceBar = config | ||
| } | ||
|
|
||
| func (unit *Unit) RegisterNewDefaultSecondaryResourceBar(config SecondaryResourceConfig) SecondaryResourceBar { | ||
| bar := unit.NewDefaultSecondaryResourceBar(config) | ||
| unit.RegisterSecondaryResourceBar(bar) | ||
| return bar | ||
| } | ||
|
|
||
| func (unit *Unit) GetSecondaryResourceBar() SecondaryResourceBar { | ||
| return unit.secondaryResourceBar | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.