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
import (
"fmt"
"github.com/kch42/gomcmap/mcmap"
"github.com/mattn/go-gtk/gdk"
)
const cacheSize = 4
type RegionWrapper struct {
region *CachedRegion
tileUpdates chan bool
Maptiles map[XZPos]*gdk.Pixmap
Biotiles map[XZPos]*gdk.Pixmap
bioCache map[XZPos][]mcmap.Biome
redraw chan<- bool
guicbs GUICallbacks
toolsEnabled bool
tool Tool
fixSnowIce bool
bio mcmap.Biome
startX, startZ, endX, endZ int
}
func renderTile(chunk *mcmap.Chunk) (maptile, biotile *gdk.Pixmap, biocache []mcmap.Biome) {
maptile = emptyPixmap(tileSize, tileSize, 24)
mtDrawable := maptile.GetDrawable()
mtGC := gdk.NewGC(mtDrawable)
biotile = emptyPixmap(tileSize, tileSize, 24)
btDrawable := biotile.GetDrawable()
btGC := gdk.NewGC(btDrawable)
biocache = make([]mcmap.Biome, mcmap.ChunkRectXZ)
i := 0
for z := 0; z < mcmap.ChunkSizeXZ; z++ {
scanX:
for x := 0; x < mcmap.ChunkSizeXZ; x++ {
bio := chunk.Biome(x, z)
btGC.SetRgbFgColor(bioColors[bio])
btDrawable.DrawRectangle(btGC, true, x*zoom, z*zoom, zoom, zoom)
biocache[i] = bio
i++
for y := chunk.Height(x, z); y >= 0; y-- {
if col, ok := blockColors[chunk.Block(x, y, z).ID]; ok {
mtGC.SetRgbFgColor(col)
mtDrawable.DrawRectangle(mtGC, true, x*zoom, z*zoom, zoom, zoom)
continue scanX
}
}
mtGC.SetRgbFgColor(gdk.NewColor("#ffffff"))
mtDrawable.DrawRectangle(mtGC, true, x*zoom, z*zoom, zoom, zoom)
}
}
return
}
func (rw *RegionWrapper) tileUpdater() {
for _ = range rw.tileUpdates {
todelete := make(map[XZPos]bool)
for pos := range rw.Maptiles {
if (pos.X < rw.startX) || (pos.Z < rw.startZ) || (pos.X >= rw.endX) || (pos.Z >= rw.endZ) {
todelete[pos] = true
}
}
gdk.ThreadsEnter()
for pos := range todelete {
if tile, ok := rw.Maptiles[pos]; ok {
tile.Unref()
delete(rw.Maptiles, pos)
}
if tile, ok := rw.Biotiles[pos]; ok {
tile.Unref()
delete(rw.Biotiles, pos)
}
if _, ok := rw.bioCache[pos]; ok {
delete(rw.bioCache, pos)
}
}
if rw.region != nil {
for z := rw.startZ; z < rw.endZ; z++ {
scanX:
for x := rw.startX; x < rw.endX; x++ {
pos := XZPos{x, z}
if _, ok := rw.Biotiles[pos]; ok {
continue scanX
}
chunk, err := rw.region.Chunk(x, z)
switch err {
case nil:
case mcmap.NotAvailable:
continue scanX
default:
rw.guicbs.reportFail(fmt.Sprintf("Could not get chunk %d, %d: %s", x, z, err))
return
}
rw.Maptiles[pos], rw.Biotiles[pos], rw.bioCache[pos] = renderTile(chunk)
chunk.MarkUnused()
gdk.ThreadsLeave()
rw.redraw <- true
gdk.ThreadsEnter()
}
}
}
gdk.ThreadsLeave()
}
}
func (rw *RegionWrapper) SetRegion(region *mcmap.Region) {
if rw.RegionLoaded() {
rw.flushTiles()
}
rw.region = NewCachedRegion(region, cacheSize)
rw.tileUpdates <- true
}
func (rw *RegionWrapper) SetChunkBounds(startX, startZ, endX, endZ int) {
rw.startX = startX
rw.startZ = startZ
rw.endX = endX
rw.endZ = endZ
}
func (rw *RegionWrapper) SetTool(t Tool) { rw.tool = t }
func (rw *RegionWrapper) SetBiome(bio mcmap.Biome) { rw.bio = bio }
func (rw *RegionWrapper) SetFixSnowIce(b bool) { rw.fixSnowIce = b }
func (rw *RegionWrapper) RegionLoaded() bool { return rw.region != nil }
func (rw *RegionWrapper) ToolSingleClick() bool { return rw.tool.SingleClick() }
func (rw *RegionWrapper) flushTiles() {
if err := rw.region.Flush(); err != nil {
rw.guicbs.reportFail(fmt.Sprintf("Error while flushing cache: %s", err))
return
}
gdk.ThreadsEnter()
for _, mt := range rw.Maptiles {
mt.Unref()
}
for _, bt := range rw.Biotiles {
bt.Unref()
}
gdk.ThreadsLeave()
rw.Maptiles = make(map[XZPos]*gdk.Pixmap)
rw.Biotiles = make(map[XZPos]*gdk.Pixmap)
rw.bioCache = make(map[XZPos][]mcmap.Biome)
}
func (rw *RegionWrapper) Save() {
gdk.ThreadsLeave()
rw.flushTiles()
gdk.ThreadsEnter()
if err := rw.region.Flush(); err != nil {
rw.guicbs.reportFail(fmt.Sprintf("Error while flushing cache: %s", err))
return
}
if err := rw.region.Region.Save(); err != nil {
rw.guicbs.reportFail(fmt.Sprintf("Error while saving: %s", err))
return
}
}
func (rw *RegionWrapper) UseTool(x, z int) {
gdk.ThreadsLeave()
defer gdk.ThreadsEnter()
if !rw.toolsEnabled {
return
}
if rw.tool.IsSlow() {
rw.toolsEnabled = false
rw.guicbs.setBusy(true)
go func() {
rw.tool.Do(rw.bio, rw, x, z)
rw.guicbs.setBusy(false)
rw.toolsEnabled = true
rw.redraw <- true
}()
} else {
rw.tool.Do(rw.bio, rw, x, z)
}
}
func (rw *RegionWrapper) GetBiomeAt(x, z int) (mcmap.Biome, bool) {
cx, cz, bx, bz := mcmap.BlockToChunk(x, z)
pos := XZPos{cx, cz}
if bc, ok := rw.bioCache[pos]; ok {
return bc[bz*mcmap.ChunkSizeXZ+bx], true
}
if !rw.RegionLoaded() {
return mcmap.BioUncalculated, false
}
chunk, err := rw.region.Chunk(cx, cz)
switch err {
case nil:
case mcmap.NotAvailable:
return mcmap.BioUncalculated, false
default:
rw.guicbs.reportFail(fmt.Sprintf("Error while getting chunk %d, %d: %s", cx, cz, err))
return mcmap.BioUncalculated, false
}
bc := make([]mcmap.Biome, mcmap.ChunkRectXZ)
i := 0
for z := 0; z < mcmap.ChunkSizeXZ; z++ {
for x := 0; x < mcmap.ChunkSizeXZ; x++ {
bc[i] = chunk.Biome(x, z)
i++
}
}
rw.bioCache[pos] = bc
return chunk.Biome(bx, bz), true
}
func fixFreeze(bx, bz int, chunk *mcmap.Chunk) (newcol *gdk.Color) {
for y := chunk.Height(bx, bz); y >= 0; y-- {
if blk := chunk.Block(bx, y, bz); blk.ID != mcmap.BlkAir {
if (blk.ID == mcmap.BlkStationaryWater) || (blk.ID == mcmap.BlkWater) {
blk.ID = mcmap.BlkIce
newcol = blockColors[mcmap.BlkIce]
} else if blockCanSnowIn[blk.ID] {
if yFix := y + 1; yFix < mcmap.ChunkSizeY {
blkFix := chunk.Block(bx, yFix, bz)
blkFix.ID = mcmap.BlkSnow
blkFix.Data = 0x0
newcol = blockColors[mcmap.BlkSnow]
}
}
break
}
}
return
}
func fixMelt(bx, bz int, chunk *mcmap.Chunk) (newcol *gdk.Color) {
for y := chunk.Height(bx, bz); y >= 0; y-- {
if blk := chunk.Block(bx, y, bz); blk.ID != mcmap.BlkAir {
if blk.ID == mcmap.BlkIce {
blk.ID = mcmap.BlkStationaryWater
blk.Data = 0x0
newcol = blockColors[mcmap.BlkStationaryWater]
} else if blk.ID == mcmap.BlkSnow {
blk.ID = mcmap.BlkAir
for y2 := y - 1; y2 >= 0; y2-- {
if col, ok := blockColors[chunk.Block(bx, y2, bz).ID]; ok {
newcol = col
break
}
}
}
break
}
}
return
}
func (rw *RegionWrapper) SetBiomeAt(x, z int, bio mcmap.Biome) {
cx, cz, bx, bz := mcmap.BlockToChunk(x, z)
pos := XZPos{cx, cz}
chunk, err := rw.region.Chunk(cx, cz)
switch err {
case nil:
case mcmap.NotAvailable:
return
default:
rw.guicbs.reportFail(fmt.Sprintf("Error while getting chunk %d, %d: %s", cx, cz, err))
return
}
chunk.SetBiome(bx, bz, bio)
var newcol *gdk.Color
if rw.fixSnowIce {
if coldBiome[bio] {
newcol = fixFreeze(bx, bz, chunk)
} else {
newcol = fixMelt(bx, bz, chunk)
}
}
chunk.MarkModified()
// Update cache
if bc, ok := rw.bioCache[pos]; ok {
bc[bz*mcmap.ChunkSizeXZ+bx] = bio
}
// Update tile
if biotile, ok := rw.Biotiles[pos]; ok {
gdk.ThreadsEnter()
drawable := biotile.GetDrawable()
gc := gdk.NewGC(drawable)
gc.SetRgbFgColor(bioColors[bio])
drawable.DrawRectangle(gc, true, bx*zoom, bz*zoom, zoom, zoom)
if newcol != nil {
drawable = rw.Maptiles[pos].GetDrawable()
gc = gdk.NewGC(drawable)
gc.SetRgbFgColor(newcol)
drawable.DrawRectangle(gc, true, bx*zoom, bz*zoom, zoom, zoom)
}
gdk.ThreadsLeave()
}
}
func (rw *RegionWrapper) UpdateTiles() {
rw.tileUpdates <- true
}
func NewRegionWrapper(redraw chan<- bool, guicbs GUICallbacks) *RegionWrapper {
rw := &RegionWrapper{
redraw: redraw,
tileUpdates: make(chan bool),
toolsEnabled: true,
guicbs: guicbs,
Maptiles: make(map[XZPos]*gdk.Pixmap),
Biotiles: make(map[XZPos]*gdk.Pixmap),
bioCache: make(map[XZPos][]mcmap.Biome),
}
go rw.tileUpdater()
return rw
}
|