forked from wowsims/wotlk
/
library.go
363 lines (333 loc) · 9.31 KB
/
library.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package main
// #include <stdlib.h>
import "C"
import (
"bytes"
"compress/zlib"
"encoding/base64"
"encoding/json"
"log"
"unsafe"
"github.com/Tereneckla/wotlk/sim"
"github.com/Tereneckla/wotlk/sim/core"
"github.com/Tereneckla/wotlk/sim/core/proto"
"google.golang.org/protobuf/encoding/protojson"
goproto "google.golang.org/protobuf/proto"
)
var _default_rsr = proto.RaidSimRequest{
Raid: &proto.Raid{},
Encounter: &proto.Encounter{},
SimOptions: &proto.SimOptions{},
}
var _active_sim = core.NewSim(&_default_rsr)
var _active_seed int64 = 1
var _aura_labels = []string{}
var _target_aura_labels = []string{}
//export runSim
func runSim(json *C.char) *C.char {
input := &proto.RaidSimRequest{}
jsonString := C.GoString(json)
err := protojson.Unmarshal([]byte(jsonString), input)
if err != nil {
log.Fatalf("failed to load input json file: %s", err)
}
sim.RegisterAll()
result := core.RunSim(input, nil)
out, err := protojson.Marshal(result)
if err != nil {
panic(err)
}
return C.CString(string(out))
}
//export computeStats
func computeStats(json *C.char) *C.char {
input := &proto.ComputeStatsRequest{}
jsonString := C.GoString(json)
err := protojson.Unmarshal([]byte(jsonString), input)
if err != nil {
log.Fatalf("failed to load input json file: %s", err)
}
sim.RegisterAll()
result := core.ComputeStats(input)
out, err := protojson.Marshal(result)
if err != nil {
panic(err)
}
return C.CString(string(out))
}
//export encodeSettings
func encodeSettings(json *C.char) *C.char {
input := &proto.RaidSimRequest{}
jsonString := C.GoString(json)
err := protojson.Unmarshal([]byte(jsonString), input)
if err != nil {
log.Fatalf("failed to load input json file: %s", err)
}
settings := &proto.IndividualSimSettings{
Settings: &proto.SimSettings{
Iterations: input.SimOptions.Iterations,
},
RaidBuffs: input.Raid.Buffs,
Debuffs: input.Raid.Debuffs,
Tanks: input.Raid.Tanks,
PartyBuffs: input.Raid.Parties[0].Buffs,
Player: input.Raid.Parties[0].Players[0],
Encounter: input.Encounter,
}
var buffer bytes.Buffer
data, err := goproto.Marshal(settings)
if err != nil {
panic(err)
}
writer := zlib.NewWriter(&buffer)
writer.Write(data)
writer.Close()
out := base64.StdEncoding.EncodeToString(buffer.Bytes())
return C.CString(string(out))
}
//export getDatabase
func getDatabase(itemIds *int32, numItems int32, enchantIds *int32, numEnchants int32, gemIds *int32, numGems int32) *C.char {
ids := unsafe.Slice(itemIds, numItems)
eids := unsafe.Slice(enchantIds, numEnchants)
gids := unsafe.Slice(gemIds, numGems)
simDB := &proto.SimDatabase{
Items: make([]*proto.SimItem, numItems),
Enchants: make([]*proto.SimEnchant, numEnchants),
Gems: make([]*proto.SimGem, numGems),
}
for i, itemId := range ids {
item := core.ItemsByID[itemId]
simDB.Items[i] = &proto.SimItem{
Id: item.ID,
Name: item.Name,
Type: item.Type,
ArmorType: item.ArmorType,
WeaponType: item.WeaponType,
HandType: item.HandType,
RangedWeaponType: item.RangedWeaponType,
Stats: item.Stats[:],
GemSockets: item.GemSockets,
SocketBonus: item.SocketBonus[:],
WeaponDamageMin: item.WeaponDamageMin,
WeaponDamageMax: item.WeaponDamageMax,
WeaponSpeed: item.SwingSpeed,
SetName: item.SetName,
}
}
for i, enchantId := range eids {
enchant := core.EnchantsByEffectID[enchantId]
simDB.Enchants[i] = &proto.SimEnchant{
EffectId: enchant.EffectID,
Stats: enchant.Stats[:],
}
}
for i, gemId := range gids {
gem := core.GemsByID[gemId]
simDB.Gems[i] = &proto.SimGem{
Id: gem.ID,
Name: gem.Name,
Color: gem.Color,
Stats: gem.Stats[:],
}
}
out, err := protojson.Marshal(simDB)
if err != nil {
panic(err)
}
return C.CString(string(out))
}
//export new
func new(json *C.char) {
input := &proto.RaidSimRequest{}
jsonString := C.GoString(json)
err := protojson.Unmarshal([]byte(jsonString), input)
if err != nil {
log.Fatalf("failed to load input json file: %s", err)
}
sim.RegisterAll()
_active_sim = core.NewSim(input)
_active_sim.Init()
_active_sim.Reseed(_active_seed)
_active_seed += 1
_active_sim.Reset()
_active_sim.PrePull()
}
//export trySpell
func trySpell(act int) bool {
player := _active_sim.Raid.Parties[0].Players[0]
spells := player.GetCharacter().Spellbook
if act >= len(spells) || act < 0 {
return false
}
spell := spells[act]
target := player.GetCharacter().CurrentTarget
casted := false
// FIXME : This is a hack to allow Heroic strike to work
if spell.ActionID.SpellID == 47450 {
aura := player.GetCharacter().GetAura("HS Queue Aura")
if aura.IsActive() {
return false
}
aura.Activate(_active_sim)
return true
}
// End of Heroic strike hack
if spell.CanCast(_active_sim, target) {
casted = spell.Cast(_active_sim, target)
if casted && spell.CurCast.GCD > 0 {
_active_sim.NeedsInput = false
}
}
return casted
}
//export doNothing
func doNothing() bool {
player := _active_sim.Raid.Parties[0].Players[0]
player.GetCharacter().DoNothing()
return true
}
//export getRemainingDuration
func getRemainingDuration() float64 {
return _active_sim.GetRemainingDuration().Seconds()
}
//export getEnergy
func getEnergy() float64 {
player := _active_sim.Raid.Parties[0].Players[0]
if !player.GetCharacter().HasEnergyBar() {
return 0.0
}
return player.GetCharacter().CurrentEnergy()
}
//export getComboPoints
func getComboPoints() int {
player := _active_sim.Raid.Parties[0].Players[0]
if !player.GetCharacter().HasEnergyBar() {
return 0
}
return int(player.GetCharacter().ComboPoints())
}
//export getUnitCount
func getUnitCount() int {
return len(_active_sim.AllUnits)
}
//export getSpellCount
func getSpellCount() int {
return len(_active_sim.Raid.Parties[0].Players[0].GetCharacter().Spellbook)
}
//export getSpells
func getSpells(storage *int32, n int32) {
player := _active_sim.Raid.Parties[0].Players[0]
spellbook := player.GetCharacter().Spellbook
spells := unsafe.Slice(storage, n)
for i, spell := range spellbook[:n] {
if spell.Tag != -1 {
spells[i] = spell.ActionID.SpellID
} else {
// These spells are not castable by the player
spells[i] = -1
}
}
}
//export getCooldowns
func getCooldowns(storage *float64, spellbookIndices *int32, n int32) {
player := _active_sim.Raid.Parties[0].Players[0]
spellbook := player.GetCharacter().Spellbook
spells := unsafe.Slice(spellbookIndices, n)
cds := unsafe.Slice(storage, n)
for i := int32(0); i < n; i++ {
spellbookIndex := spells[i]
spell := spellbook[spellbookIndex]
cds[i] = spell.TimeToReady(_active_sim).Seconds()
}
}
//export registerAuras
func registerAuras(strings **C.char) {
_aura_labels = []string{}
labels := unsafe.Slice(strings, 1<<30)
for i := 0; labels[i] != nil; i++ {
_aura_labels = append(_aura_labels, C.GoString(labels[i]))
}
}
//export registerTargetAuras
func registerTargetAuras(strings **C.char) {
_target_aura_labels = []string{}
labels := unsafe.Slice(strings, 1<<30)
for i := 0; labels[i] != nil; i++ {
_target_aura_labels = append(_target_aura_labels, C.GoString(labels[i]))
}
}
//export getAuras
func getAuras(storage *float64, n int32) {
player := _active_sim.Raid.Parties[0].Players[0]
auras := unsafe.Slice(storage, n)
for i, label := range _aura_labels {
aura := player.GetCharacter().GetAura(label)
if aura != nil {
auras[i] = aura.RemainingDuration(_active_sim).Seconds()
} else {
auras[i] = 0.0
}
}
}
//export getTargetAuras
func getTargetAuras(storage *float64, n int32) {
player := _active_sim.Raid.Parties[0].Players[0]
target := player.GetCharacter().CurrentTarget
auras := unsafe.Slice(storage, n)
for i, label := range _target_aura_labels {
aura := target.GetAura(label)
if aura != nil {
auras[i] = aura.RemainingDuration(_active_sim).Seconds()
} else {
auras[i] = 0.0
}
}
}
//export getDamageDone
func getDamageDone() float64 {
player := _active_sim.Raid.Parties[0].Players[0]
spellbook := player.GetCharacter().Spellbook
totalDamage := 0.0
for _, spell := range spellbook {
for _, metrics := range spell.SpellMetrics {
totalDamage += metrics.TotalDamage
}
}
return totalDamage
}
//export getSpellMetrics
func getSpellMetrics() *C.char {
all_metrics := make(map[int32][]core.SpellMetrics)
player := _active_sim.Raid.Parties[0].Players[0]
spellbook := player.GetCharacter().Spellbook
for _, spell := range spellbook {
spell_id := spell.ActionID.SpellID
for _, metrics := range spell.SpellMetrics {
if metrics.Casts > 0 {
all_metrics[spell_id] = append(all_metrics[spell_id], metrics)
}
}
}
out, err := json.Marshal(all_metrics)
if err != nil {
panic(err)
}
return C.CString(string(out))
}
//export step
func step() bool {
return _active_sim.Step(core.NeverExpires)
}
//export needsInput
func needsInput() bool {
return _active_sim.NeedsInput
}
//export cleanup
func cleanup() {
_active_sim.Cleanup()
}
//export FreeCString
func FreeCString(s *C.char) {
C.free(unsafe.Pointer(s))
}
func main() {}