forked from nielsAD/gowarcraft3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
295 lines (252 loc) · 6.35 KB
/
info.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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
package w3m
import (
"io"
"github.com/nielsAD/gowarcraft3/protocol"
)
// Info for Warcraft III maps as found in the war3map.w3i file
type Info struct {
FileFormat uint32
SaveCount uint32
EditorVersion uint32
Name string
Author string
Description string
SuggestedPlayers string
CamBounds [8]float32
CamBoundsCompl [4]uint32
Width uint32
Height uint32
Flags MapFlags
Tileset Tileset
LsBackground uint32
LsPath string
LsText string
LsTitle string
LsSubTitle string
DataSet uint32
PsPath string
PsText string
PsTitle string
PsSubTitle string
Fog uint32
FogStart float32
FogEnd float32
FogDensity float32
FogColor uint32
WeatherID protocol.DWordString
SoundEnv string
LightEnv Tileset
WaterColor uint32
Players []Player
Forces []Force
CustomUpgradeAvailabilities []CustomUpgradeAvailability
CustomTechAvailabilities []CustomTechAvailability
}
// Player structure in war3map.w3i file
type Player struct {
ID uint32
Type PlayerType
Race Race
Flags PlayerFlags
Name string
StartPosX float32
StartPosY float32
AllyPrioLow protocol.BitSet32
AllyPrioHigh protocol.BitSet32
}
// Force structure in war3map.w3i file
type Force struct {
Flags ForceFlags
PlayerSet protocol.BitSet32
Name string
}
// CustomUpgradeAvailability in war3map.w3i file
type CustomUpgradeAvailability struct {
PlayerSet protocol.BitSet32
UpgradeID protocol.DWordString
Level uint32
Availability UpgradeAvailability
}
// CustomTechAvailability in war3map.w3i file
type CustomTechAvailability struct {
PlayerSet protocol.BitSet32
TechID protocol.DWordString
}
// Info read from war3map.w3i
func (m *Map) Info() (*Info, error) {
w3i, err := m.Archive.Open("war3map.w3i")
if err != nil {
return nil, err
}
defer w3i.Close()
if w3i.Size() < 96 {
return nil, ErrBadFormat
}
var b protocol.Buffer
if _, err := io.Copy(&b, w3i); err != nil {
return nil, err
}
var readTS = func() (string, error) {
s, err := b.ReadCString()
if err != nil {
return "", err
}
return m.ExpandString(s)
}
var i = Info{
FileFormat: b.ReadUInt32(),
}
switch i.FileFormat {
case 18: //.w3m
case 25: //.w3x
default:
return nil, ErrBadFormat
}
i.SaveCount = b.ReadUInt32()
i.EditorVersion = b.ReadUInt32()
i.Name, _ = readTS()
i.Author, _ = readTS()
i.Description, _ = readTS()
i.SuggestedPlayers, err = readTS()
if err != nil {
return nil, err
} else if b.Size() < 80 {
return nil, ErrBadFormat
}
for c := 0; c < len(i.CamBounds); c++ {
i.CamBounds[c] = b.ReadFloat32()
}
for c := 0; c < len(i.CamBoundsCompl); c++ {
i.CamBoundsCompl[c] = b.ReadUInt32()
}
i.Width = b.ReadUInt32()
i.Height = b.ReadUInt32()
i.Flags = MapFlags(b.ReadUInt32())
i.Tileset = Tileset(b.ReadUInt8())
i.LsBackground = b.ReadUInt32()
if i.FileFormat == 25 {
i.LsPath, _ = readTS()
}
i.LsText, _ = readTS()
i.LsTitle, _ = readTS()
i.LsSubTitle, err = readTS()
if err != nil {
return nil, err
} else if b.Size() < 13 {
return nil, ErrBadFormat
}
i.DataSet = b.ReadUInt32()
if i.FileFormat == 25 {
i.PsPath, _ = readTS()
}
i.PsText, _ = readTS()
i.PsTitle, _ = readTS()
i.PsSubTitle, err = readTS()
if err != nil {
return nil, err
}
if i.FileFormat == 25 {
if b.Size() < 54 {
return nil, ErrBadFormat
}
i.Fog = b.ReadUInt32()
i.FogStart = b.ReadFloat32()
i.FogEnd = b.ReadFloat32()
i.FogDensity = b.ReadFloat32()
i.FogColor = b.ReadUInt32()
i.WeatherID = b.ReadLEDString()
i.SoundEnv, err = readTS()
if err != nil {
return nil, err
} else if b.Size() < 12 {
return nil, ErrBadFormat
}
i.LightEnv = Tileset(b.ReadUInt8())
i.WaterColor = b.ReadUInt32()
}
if b.Size() < 8 {
return nil, ErrBadFormat
}
var numPlayers = b.ReadUInt32()
i.Players = make([]Player, numPlayers)
for p := uint32(0); p < numPlayers; p++ {
if b.Size() < 37 {
return nil, ErrBadFormat
}
i.Players[p].ID = b.ReadUInt32()
i.Players[p].Type = PlayerType(b.ReadUInt32())
i.Players[p].Race = Race(b.ReadUInt32())
i.Players[p].Flags = PlayerFlags(b.ReadUInt32())
i.Players[p].Name, err = readTS()
if err != nil {
return nil, err
} else if b.Size() < 20 {
return nil, ErrBadFormat
}
i.Players[p].StartPosX = b.ReadFloat32()
i.Players[p].StartPosY = b.ReadFloat32()
i.Players[p].AllyPrioLow = protocol.BitSet32(b.ReadUInt32())
i.Players[p].AllyPrioHigh = protocol.BitSet32(b.ReadUInt32())
}
var numForces = b.ReadUInt32()
i.Forces = make([]Force, numForces)
for f := uint32(0); f < numForces; f++ {
if b.Size() < 9 {
return nil, ErrBadFormat
}
i.Forces[f].Flags = ForceFlags(b.ReadUInt32())
i.Forces[f].PlayerSet = protocol.BitSet32(b.ReadUInt32())
i.Forces[f].Name, err = readTS()
if err != nil {
return nil, err
}
}
if b.Size() >= 8 {
var numUpgrades = b.ReadUInt32()
i.CustomUpgradeAvailabilities = make([]CustomUpgradeAvailability, numUpgrades)
for u := uint32(0); u < numUpgrades; u++ {
if b.Size() < 24 {
return nil, ErrBadFormat
}
i.CustomUpgradeAvailabilities[u].PlayerSet = protocol.BitSet32(b.ReadUInt32())
i.CustomUpgradeAvailabilities[u].UpgradeID = b.ReadLEDString()
i.CustomUpgradeAvailabilities[u].Level = b.ReadUInt32()
i.CustomUpgradeAvailabilities[u].Availability = UpgradeAvailability(b.ReadUInt32())
}
var numTechs = b.ReadUInt32()
i.CustomTechAvailabilities = make([]CustomTechAvailability, numTechs)
for u := uint32(0); u < numTechs; u++ {
if b.Size() < 12 {
return nil, ErrBadFormat
}
i.CustomTechAvailabilities[u].PlayerSet = protocol.BitSet32(b.ReadUInt32())
i.CustomTechAvailabilities[u].TechID = b.ReadLEDString()
}
// TODO: RandomUnitTable and RandomItemTable
}
return &i, nil
}
// Size returns the map size category
func (m *Info) Size() Size {
var s = m.Width * m.Height
if s < 92*92 {
return SizeTiny
} else if s < 128*128 {
return SizeExtraSmall
} else if s < 160*160 {
return SizeSmall
} else if s < 192*192 {
return SizeNormal
} else if s < 224*224 {
return SizeLarge
} else if s < 288*288 {
return SizeExtraLarge
} else if s < 384*384 {
return SizeHuge
} else {
return SizeEpic
}
}