-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.go
335 lines (310 loc) · 6.17 KB
/
level.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
// Copyright 2020 Andrew Ekstedt
// This program is licensed under the GNU Affero General
// Public License v3.0. See LICENSE for details.
package main
import (
"errors"
"io"
"log"
"os"
"strconv"
"github.com/magical/littlebyte"
)
type Level struct {
Title string
//Password string
TimeLimit int
Tiles [32][32]Tile
Subtiles [32][32]Tile
}
func SaveLevel(filename string, level *Level) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
if err := saveLevel(f, level); err != nil {
return err
}
return f.Close()
}
const magic = "\xAC\xAA\x02\x00"
func saveLevel(w io.Writer, level *Level) error {
var b = new(littlebyte.Builder)
// file header
b.AddBytes([]byte(magic))
b.AddUint16(1) // 1 level
// level header
// time limit 500, no chips
b.AddUint16LengthPrefixed(func(b *littlebyte.Builder) {
b.AddUint16(1) // level 1
b.AddUint16(uint16(level.TimeLimit)) // time limit
b.AddUint16(0) // no chips
// map start
b.AddUint16(1)
// rle encode top layer
b.AddUint16LengthPrefixed(func(b *littlebyte.Builder) {
rlencode(b, &level.Tiles)
})
// bottom layer
b.AddUint16LengthPrefixed(func(b *littlebyte.Builder) {
rlencode(b, &level.Subtiles)
})
// optional fields
b.AddUint16LengthPrefixed(func(b *littlebyte.Builder) {
// level title
b.AddUint8(3)
b.AddUint8LengthPrefixed(func(b *littlebyte.Builder) {
b.AddBytes([]byte(level.Title))
b.AddUint8(0) // NUL
})
// Password
b.AddUint8(6)
b.AddUint8LengthPrefixed(func(b *littlebyte.Builder) {
b.AddUint8('M' ^ 0x99)
b.AddUint8('A' ^ 0x99)
b.AddUint8('Z' ^ 0x99)
b.AddUint8('E' ^ 0x99)
b.AddUint8(0) // NUL
})
})
})
bytes, err := b.Bytes()
if err != nil {
return err
}
_, err = w.Write(bytes)
return err
}
func rlencode(b *littlebyte.Builder, tiles *[32][32]Tile) {
for y := range tiles {
for x := 0; x < 32; {
t, count := countTiles(tiles[y][x:])
x += count
switch {
case count > 3:
b.AddUint8(0xFF)
b.AddUint8(uint8(count))
b.AddUint8(t.encoding())
default:
te := t.encoding()
for i := 0; i < count; i++ {
b.AddUint8(te)
}
}
}
}
}
func countTiles(s []Tile) (Tile, int) {
// assume s is non-empty
t := s[0]
for i := 1; i < len(s); i++ {
if s[i] != t {
return t, i
}
}
return t, len(s)
}
type Tile uint8
const (
Floor = iota
Wall
Teleport
Block
Exit
Player
Trap
TrapButton
Dirt
Popup
Fire
ToggleWall
ToggleFloor
ToggleButton
)
func (t Tile) encoding() uint8 {
switch t {
case Floor:
return 0x00
case Wall:
return 0x01
case Teleport:
return 0x29
case Player:
return 0x6E
case Exit:
return 0x15
case Block:
return 0x0A
case Trap:
return 0x2B
case TrapButton:
return 0x27
case Dirt:
return 0x0B
case Popup:
return 0x2E
case Fire:
return 0x04
case ToggleButton:
return 0x23
case ToggleWall:
return 0x25
case ToggleFloor:
return 0x26
default:
panic("invalid tile: " + strconv.Itoa(int(t)))
}
}
var errEOF = errors.New("level: unexpected end of file")
func DecodeLevel(b []byte) (*Level, error) {
s := littlebyte.String(b)
var fileMagic []byte
if !s.ReadBytes(&fileMagic, 4) {
return nil, errEOF
}
if string(fileMagic) != magic {
return nil, errors.New("level: invalid magic bytes")
}
var nlevels uint16
if !s.ReadUint16(&nlevels) {
return nil, errEOF
}
if nlevels == 0 {
return nil, errors.New("level: levelset has no levels")
}
if nlevels != 1 {
return nil, errors.New("level: sets with more than one level are unsupported")
}
var levelbytes littlebyte.String
if !s.ReadUint16LengthPrefixed(&levelbytes) {
return nil, errEOF
}
level, err := readLevel(levelbytes)
if err != nil {
return nil, err
}
if !s.Empty() {
return nil, errors.New("level: garbage at end of file")
}
return level, nil
}
func readLevel(s littlebyte.String) (*Level, error) {
// n
// time limit
// chips
// 1
// top layer
// bottom layer
// fields
var (
n uint16
time uint16
chips uint16
x uint16
)
if !s.ReadUint16(&n) {
return nil, errEOF
}
if n != 1 { // TODO: don't hardcode
return nil, errors.New("level: expected level 1")
}
if !s.ReadUint16(&time) {
return nil, errEOF
}
if !s.ReadUint16(&chips) {
return nil, errEOF
}
if !s.ReadUint16(&x) {
return nil, errEOF
}
if x != 1 {
return nil, errors.New("level: malformed level")
}
var level Level
level.TimeLimit = int(time)
//
var (
topdata littlebyte.String
bottomdata littlebyte.String
fields littlebyte.String
)
var printedWarning = false
if !s.ReadUint16LengthPrefixed(&topdata) {
return nil, errEOF
}
// decode top layer RLE data
for i := 0; i < 32*32; {
var t uint8
if !topdata.ReadUint8(&t) {
return nil, errors.New("level: unexpected end of layer data")
}
var count uint8 = 1
if t == 0xFF {
if !topdata.ReadUint8(&count) {
return nil, errors.New("level: unexpected end of layer data")
}
if !topdata.ReadUint8(&t) {
return nil, errors.New("level: unexpected end of layer data")
}
} else {
count = 1
}
var tile Tile = Floor
switch t {
case 0x00:
tile = Floor
case 0x01:
tile = Wall
case 0x0A:
tile = Block
case 0x29:
tile = Teleport
case 0x6C, 0x6D, 0x6E, 0x6F:
tile = Player
case 0x15:
tile = Exit
case 0x2B:
tile = Trap
case 0x27:
tile = TrapButton
case 0x2E:
tile = Popup
case 0x0B:
tile = Dirt
case 0x04:
tile = Fire
case 0x23:
tile = ToggleButton
case 0x25:
tile = ToggleWall
case 0x26:
tile = ToggleFloor
default:
// unknown tile
if !printedWarning {
log.Printf("warning: unknown tile %#x at %d,%d (only one warning will be shown per level)", t, i%32, i/32)
printedWarning = true
}
}
for j := 0; j < int(count); j++ {
x, y := i%32, i/32
level.Tiles[y][x] = tile
i++
}
}
if !topdata.Empty() {
return nil, errors.New("level: garbage at end of top layer data")
}
// ignore bottom layer
if !s.ReadUint16LengthPrefixed(&bottomdata) {
return nil, errEOF
}
if !s.ReadUint16LengthPrefixed(&fields) {
return nil, errEOF
}
if !s.Empty() {
return nil, errors.New("level: garbage at end of level")
}
return &level, nil
}