-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.go
257 lines (224 loc) · 5.76 KB
/
actions.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
package warcrumb
import (
"bytes"
"fmt"
"strconv"
"time"
)
type Action struct {
Ability Ability
Time time.Duration
Player *Player
}
func (a Action) MarshalText() (text []byte, err error) {
return []byte(a.String()), nil
}
/*
func (a Action) MarshalJSON() ([]byte, error) {
// FIXME: temporary until data is more structured
// i.e. by default the action string representation won't be encoded
return json.Marshal(struct {
Time time.Duration
Player *Player
Data interface{}
}{a.Time, a.Player, a.Ability})
}
*/
func (a Action) String() string {
return fmt.Sprintf("[%s] %s: %s", a.Time, a.Player, a.Ability)
}
type Ability interface {
fmt.Stringer
APMChange() int
}
type ItemId [4]byte
func (a ItemId) String() string {
//if bytes.Equal(a[:], []byte{0x03, 0x00, 0x0d, 0x00}) {
// return "right clicked"
//}
if full, ok := byteStrings[string(a[:])]; ok {
return full.Tip
}
if bytes.Contains(a[:], []byte{0x00}) {
// alphanumeric id
//return string(a[:])
return fmt.Sprintf("%#02v", a)
} else {
// string
var reversed [4]byte
for i := 0; i < 4; i++ {
reversed[i] = a[3-i]
}
str := string(reversed[:])
if full, ok := WC3Strings[str]; ok {
return full.Tip
}
return str
}
}
type ObjectId uint32
func (o ObjectId) IsGround() bool {
return o == 0xFFFFFFFF
}
func (o ObjectId) String() string {
if o.IsGround() {
return "the ground"
} else {
return strconv.Itoa(int(o))
}
}
type BasicAbility struct {
AbilityFlags uint16
ItemId ItemId
}
func (a BasicAbility) String() string {
return fmt.Sprintf("Ability [mod %#02x] %s", a.AbilityFlags, a.ItemId)
}
func (a BasicAbility) APMChange() int {
return 1
}
type TargetedAbility struct {
BasicAbility
Target PointF
}
func (a TargetedAbility) String() string {
return fmt.Sprintf("%s at %s", a.BasicAbility, a.Target)
}
type ObjectTargetedAbility struct {
TargetedAbility
TargetObjectId1, TargetObjectId2 ObjectId
}
func (a ObjectTargetedAbility) String() string {
return fmt.Sprintf("%s object (%s, %s) at %s", a.BasicAbility, a.TargetObjectId1, a.TargetObjectId2, a.Target)
}
func (a ObjectTargetedAbility) TargetsGround() bool {
return a.TargetObjectId1.IsGround() && a.TargetObjectId2.IsGround()
}
type GiveOrDropItem struct {
ObjectTargetedAbility
ItemObjectId1, ItemObjectId2 ObjectId
}
func (g GiveOrDropItem) String() string {
if g.TargetsGround() {
return fmt.Sprintf("Drop item %s (%s, %s) on ground at %s", g.ItemId, g.ItemObjectId1, g.ItemObjectId2, g.Target)
}
return fmt.Sprintf("Give item %s (%s, %s) to obj (%s, %s) at %s", g.ItemId, g.ItemObjectId1, g.ItemObjectId2, g.TargetObjectId1, g.TargetObjectId2, g.Target)
}
type TwoTargetTwoItemAbility struct {
TargetedAbility
ItemId2 ItemId
Target2 PointF
}
func (t TwoTargetTwoItemAbility) String() string {
return fmt.Sprintf("%s + %s to %s & %s", t.BasicAbility, t.ItemId2, t.Target, t.Target2)
}
func readActionBlock(buffer *bytes.Buffer, replay *Replay) (Ability, error) {
actionId, err := buffer.ReadByte()
if err != nil {
return nil, err
}
switch actionId {
case 0x10, 0x11, 0x12, 0x13, 0x14: // ability (+target) (+object target) (+ target item)
var abilityFlags uint16
if replay.Version < 13 {
abilityFlagsB, err := buffer.ReadByte()
if err != nil {
return nil, err
}
abilityFlags = uint16(abilityFlagsB)
} else {
abilityFlags, err = readWORD(buffer)
if err != nil {
return nil, err
}
}
var itemId [4]byte
if _, err = buffer.Read(itemId[:]); err != nil {
return nil, err
}
if replay.Version >= 7 {
// two unknown values that are 0xFFFFFFFF in old replays (before like, 1.18)
if _, err = readDWORD(buffer); err != nil {
return nil, err
}
if _, err = readDWORD(buffer); err != nil {
return nil, err
}
}
baseAbility := BasicAbility{AbilityFlags: abilityFlags, ItemId: itemId}
if actionId == 0x10 {
return baseAbility, nil
}
target, err := readPointF(buffer)
if err != nil {
return nil, err
}
targetedAbility := TargetedAbility{BasicAbility: baseAbility, Target: target}
if actionId == 0x11 {
return targetedAbility, nil
}
if actionId == 0x14 {
var itemId2 [4]byte
if _, err = buffer.Read(itemId2[:]); err != nil {
return nil, err
}
buffer.Next(9)
target2, err := readPointF(buffer)
if err != nil {
return nil, err
}
return TwoTargetTwoItemAbility{
TargetedAbility: targetedAbility,
ItemId2: itemId2,
Target2: target2,
}, nil
}
targetObjId1, err := readDWORD(buffer)
if err != nil {
return nil, err
}
targetObjId2, err := readDWORD(buffer)
if err != nil {
return nil, err
}
objTargetedAbility := ObjectTargetedAbility{
TargetedAbility: targetedAbility,
TargetObjectId1: ObjectId(targetObjId1),
TargetObjectId2: ObjectId(targetObjId2),
}
if actionId == 0x12 {
return objTargetedAbility, nil
}
itemObjId1, err := readDWORD(buffer)
if err != nil {
return nil, err
}
itemObjId2, err := readDWORD(buffer)
if err != nil {
return nil, err
}
return GiveOrDropItem{objTargetedAbility, ObjectId(itemObjId1), ObjectId(itemObjId2)}, nil
}
return nil, nil
}
// StringsEntity represents a definition from the WC3 *strings.txt files, which tools/gen_strings uses to generate mappings.
// Replay files only contain short codes, e.g. "hpea", from which we want to get "Peasant" (and possibly other data later).
type StringsEntity struct {
// Name is the full name of the entity, e.g. "Peasant"
Name string
// Code is the 4-char string it is stored under, e.g. "hpea"
Code string
// Tip is the imperative form, e.g. "Train Peasant"
Tip string
}
var byteStrings = map[string]StringsEntity{
"\u0003\u0000\r\u0000": {
Name: "Right Click",
Code: "",
Tip: "Right-click",
},
"\u000f\u0000\r\u0000": {
Name: "Attack",
Tip: "Attack",
},
}