forked from lologarithm/wowsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
329 lines (290 loc) · 8.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"net/http"
"strconv"
"strings"
"time"
"github.com/lologarithm/wowsim/tbc"
)
// /script print(GetSpellBonusDamage(4))
func main() {
// f, err := os.Create("profile2.cpu")
// if err != nil {
// log.Fatal("could not create CPU profile: ", err)
// }
// defer f.Close() // error handling omitted for example
// if err := pprof.StartCPUProfile(f); err != nil {
// log.Fatal("could not start CPU profile: ", err)
// }
// defer pprof.StopCPUProfile()
var isDebug = flag.Bool("debug", false, "Include --debug to spew the entire simulation log.")
var noopt = flag.Bool("noopt", false, "If included it will disable optimization.")
var rotation = flag.String("rotation", "", "Custom comma separated rotation to simulate.\n\tFor Example: --rotation=CL6,LB12")
var duration = flag.Int("duration", 300, "Custom fight duration in seconds.")
var iterations = flag.Int("iter", 10000, "Custom number of iterations for the sim to run.")
var runWebUI = flag.Bool("web", false, "Use to run sim in web interface instead of in terminal")
var configFile = flag.String("config", "", "Specify an input configuration.")
flag.Parse()
if *runWebUI {
log.Printf("Closing: %s", http.ListenAndServe(":3333", nil))
}
// Just some default gear if not provided in the config file.
gear := tbc.NewEquipmentSet(
"Tidefury Helm",
"Charlotte's Ivy",
"Pauldrons of Wild Magic",
"Ogre Slayer's Cover",
"Tidefury Chestpiece",
"World's End Bracers",
"Earth Mantle Handwraps",
"Netherstrike Belt",
"Stormsong Kilt",
"Magma Plume Boots",
"Cobalt Band of Tyrigosa",
"Sparking Arcanite Ring",
"Mazthoril Honor Shield",
"Gavel of Unearthed Secrets",
"Natural Alignment Crystal",
"Icon of the Silver Crescent",
"Totem of the Void",
)
// Auto gem the default gear above.
ruby := tbc.GemLookup["Runed Living Ruby"]
for i := range gear {
gear[i].Gems = make([]tbc.Gem, len(gear[i].GemSlots))
for gs, color := range gear[i].GemSlots {
if color != tbc.GemColorMeta {
gear[i].Gems[gs] = ruby
} else {
gear[i].Gems[gs] = tbc.Gems[0] // CSD
}
}
}
opt := tbc.Options{
NumBloodlust: 0,
NumDrums: 0,
Buffs: tbc.Buffs{
ArcaneInt: false,
GiftOftheWild: false,
BlessingOfKings: false,
ImprovedBlessingOfWisdom: false,
JudgementOfWisdom: false,
Moonkin: false,
SpriestDPS: 0,
WaterShield: true,
// Race: tbc.RaceBonusOrc,
Custom: tbc.Stats{
tbc.StatInt: 290,
tbc.StatSpellDmg: 598 + 55,
tbc.StatSpellHit: 24,
tbc.StatSpellCrit: 120,
},
},
Consumes: tbc.Consumes{
// FlaskOfBlindingLight: true,
// BrilliantWizardOil: false,
// MajorMageblood: false,
// BlackendBasilisk: true,
SuperManaPotion: false,
// DarkRune: false,
},
Talents: tbc.Talents{
LightninOverload: 5,
ElementalPrecision: 3,
NaturesGuidance: 3,
TidalMastery: 5,
ElementalMastery: true,
UnrelentingStorm: 3,
CallOfThunder: 5,
Concussion: 5,
Convection: 5,
},
Totems: tbc.Totems{
TotemOfWrath: 1,
WrathOfAir: false,
ManaStream: true,
},
}
if *configFile != "" {
data, err := ioutil.ReadFile(*configFile)
if err != nil {
log.Fatalf("Failed to open config file(%s): %s", *configFile, err)
}
gear, opt = getGear(data)
}
if *isDebug {
*iterations = 1
opt.Debug = true
}
rotArray := []string{}
if rotation != nil && len(*rotation) > 0 {
rotArray = strings.Split(*rotation, ",")
}
results := runTBCSim(gear, opt, *duration, *iterations, rotArray, *noopt)
for _, res := range results {
fmt.Printf("\n%s\n", res)
}
}
type input struct {
Options tbc.Options
Gear []tbc.Item
}
func getGear(val []byte) (tbc.Equipment, tbc.Options) {
in := &input{}
err := json.Unmarshal(val, in)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %s", err)
}
gearSet := make([]tbc.Item, len(in.Gear))
for i, v := range in.Gear {
itemTemplate := tbc.ItemsByName[v.Name]
if v.Name == "" && v.ID > 0 {
itemTemplate = tbc.ItemsByID[v.ID]
}
ic := itemTemplate
if len(v.Gems) > 0 {
ic.Gems = make([]tbc.Gem, len(ic.GemSlots))
for _, gem := range ic.Gems {
gv, ok := tbc.GemLookup[gem.Name]
if !ok {
continue // wasn't a valid gem
}
ic.Gems[i] = gv
}
}
ic.Enchant = tbc.EnchantLookup[v.Enchant.Name]
gearSet[i] = ic
}
return tbc.Equipment(gearSet), in.Options
}
func runTBCSim(equip tbc.Equipment, opt tbc.Options, seconds int, numSims int, customRotation []string, noopt bool) []string {
fmt.Printf("\nSim Duration: %d sec\nNum Simulations: %d\n", seconds, numSims)
stats := tbc.CalculateTotalStats(opt, equip)
spellOrders := [][]string{
{"CL6", "LB12", "LB12", "LB12"},
{"CL6", "LB12", "LB12", "LB12", "LB12"},
{"CL6", "LB12", "LB12", "LB12", "LB12", "LB12"},
{"pri", "CL6", "LB12"}, // cast CL whenever off CD, otherwise LB
// {"LB12"}, // only LB
}
if len(customRotation) > 0 {
fmt.Printf("Using Custom Rotation: %v\n", customRotation)
spellOrders = [][]string{customRotation}
}
fmt.Printf("\nFinal Stats: %s\n", stats.Print())
statchan := make(chan string, 3)
for spi, spells := range spellOrders {
go doSimMetrics(spells, stats, equip, opt, seconds, numSims, statchan)
if opt.Debug && spi != len(spellOrders)-1 {
time.Sleep(time.Second * 2)
}
}
results := []string{}
for i := 0; i < len(spellOrders); i++ {
results = append(results, <-statchan)
}
opt.UseAI = true
go doSimMetrics([]string{"AI"}, stats, equip, opt, seconds, numSims, statchan)
results = append(results, <-statchan)
if !noopt {
// fmt.Printf("\n------- OPTIMIZING -------\n")
// optResult, optimalRotation := tbc.OptimalRotation(stats, opt, equip, seconds, numSims)
// fmt.Printf("\n------- DONE -------\n")
// fmt.Printf("Ratio: 1CL : %dLB\n", len(optimalRotation)-1)
// tbc.PrintResult(optResult, seconds)
tbc.OptimalGems(opt, equip, seconds, numSims)
weights := tbc.StatWeights(opt, equip, seconds, numSims)
// fmt.Printf("Weights: [ SP: %0.2f, Int: %0.2f, Crit: %0.2f, Hit: %0.2f, Haste: %0.2f, MP5: %0.2f ]\n", weights[0], weights[1], weights[2], weights[3], weights[4], weights[5])
fmt.Printf("Weights: [\n")
for i, v := range weights {
if tbc.Stat(i) == tbc.StatStm {
continue
}
fmt.Printf("%s: %0.2f\t", tbc.Stat(i).StatName(), v)
}
fmt.Printf("\n]\n")
}
return results
}
func doSimMetrics(spo []string, stats tbc.Stats, equip tbc.Equipment, opt tbc.Options, seconds int, numSims int, statchan chan string) {
simDmgs := []float64{}
simOOMs := []int{}
histogram := map[int]int{}
casts := map[int32]int{}
manaSpent := 0.0
manaLeft := 0.0
oomdps := 0.0
ooms := 0
numOoms := 0
rseed := time.Now().Unix()
opt.SpellOrder = spo
opt.RSeed = rseed
sim := tbc.NewSim(stats, equip, opt)
for ns := 0; ns < numSims; ns++ {
metrics := sim.Run(seconds)
simDmgs = append(simDmgs, metrics.TotalDamage)
simOOMs = append(simOOMs, metrics.OOMAt)
manaLeft += float64(metrics.ManaAtEnd)
oomdps += metrics.DamageAtOOM
ooms += metrics.OOMAt
if metrics.OOMAt > 0 {
numOoms++
}
for _, cast := range metrics.Casts {
casts[cast.Spell.ID] += 1
manaSpent += cast.ManaCost
}
rv := int(math.Round(math.Round(metrics.TotalDamage/float64(seconds))/10) * 10)
histogram[rv] += 1
}
oomdps /= float64(numOoms)
// TODO: do this better... for now just dumping histograph data to disk lol.
out := ""
for k, v := range histogram {
out += strconv.Itoa(k) + "," + strconv.Itoa(v) + "\n"
}
// ioutil.WriteFile(strings.Join(spo, ""), []byte(out), 0666)
totalDmg := 0.0
tdSq := totalDmg
max := 0.0
for _, dmg := range simDmgs {
totalDmg += dmg
tdSq += dmg * dmg
if dmg > max {
max = dmg
}
}
meanSq := tdSq / float64(numSims)
mean := totalDmg / float64(numSims)
stdev := math.Sqrt(meanSq - mean*mean)
output := ""
output += fmt.Sprintf("Spell Order: %v\n", spo)
output += fmt.Sprintf("DPS:")
output += fmt.Sprintf("\tMean: %0.1f +/- %0.1f\n", (mean / float64(seconds)), stdev/float64(seconds))
output += fmt.Sprintf("\tMax: %0.1f\n", (max / float64(seconds)))
output += fmt.Sprintf("Total Casts:\n")
for k, v := range casts {
output += fmt.Sprintf("\t%s: %d\n", tbc.AuraName(k), v/numSims)
}
// output += fmt.Sprintf("Avg Mana Spent: %d\n", int(manaSpent)/numSims)
// output += fmt.Sprintf("Avg Mana Left: %d\n", int(manaLeft)/numSims)
// avgleft := (manaLeft) / float64(numSims)
// extraCL := int(avgleft / 414) // 414 is cost of difference casting CL instead of LB
// output += fmt.Sprintf("Add CL: %d\n", extraCL)
avgoomSec := 0
if numOoms > 0 {
avgoomSec = ooms / numOoms
}
output += fmt.Sprintf("Went OOM: %d/%d sims\n", numOoms, numSims)
if numOoms > 0 {
output += fmt.Sprintf("Avg OOM Time: %d seconds\n", avgoomSec)
output += fmt.Sprintf("Avg DPS At OOM: %0.0f\n", oomdps/float64(avgoomSec))
}
statchan <- output
}