-
Notifications
You must be signed in to change notification settings - Fork 0
/
citymap.go
472 lines (408 loc) · 12.5 KB
/
citymap.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package citygraph
import (
"fmt"
"image"
"image/color"
"image/draw"
"github.com/voidshard/citygraph/internal/encoding"
"github.com/boljen/go-bitmap"
"github.com/fogleman/gg"
"golang.org/x/image/colornames"
)
const (
// bit numbers for our bitmap
bitRoad = 0
bitBridge = 1
bitWall = 2
bitTower = 3
bitGate = 4
)
// CityMap is a graphical representation of a CityGraph
type CityMap interface {
// Save as custom file in a format defined by the library
Save(fpath string) error
// SaveAdv saves as an image with the given color scheme
SaveAdv(fpath string, scheme *ColourScheme) error
// CustomImage returns an image with the given color scheme
CustomImage(scheme *ColourScheme) (image.Image, error)
// only one of these things can be at a given (x,y) co-ord
IsRoad(x, y int) bool
IsBridge(x, y int) bool
IsWall(x, y int) bool
IsTower(x, y int) bool
IsGatehouse(x, y int) bool
BuildingID(x, y int) (int, error)
// internal helper for IsWall || IsTower || IsGatehouse
isFortification(x, y int) bool
// District returns the district type & id at the given x,y
District(x, y int) (DistrictType, int, error)
}
// imageMap is a particular implementation of CityMap using a RGBA64
type imageMap struct {
// Map is an RGBA64 image where each pixel of 64 bits is split via
//
// nb. helpful
// 8 bits 0-255
// 16 bits 0-65,535
// 32 bits 0-4,294,967,295
//
// R [16 bits]
// 16-1: [16 bits] -> district id
// G [16 bits]
// B [16 bits]
// 32-1 [32 bits] -> building id, G holds the significant bits
// A [16 bits]
// 16-9 [8 bits] -> district type
// 8-1 [8 bits] -> bitmap (true if set, false if not)
// bit 0 -> isRoad
// bit 1 -> isBridge
// bit 2 -> isWall
// bit 3 -> isTower
// bit 4 -> isGatehouse
// bit 5-7 -> unused
//
im *image.RGBA64
// temporary map for road / wall / tower / gatehouse network
// We draw the shapes with a drawing lib because it's 100x easier
// than figuring out all the geometry ourselves .. we then transfer
// this information into our main working image in endDraw()
ctx *gg.Context
// mask for our drawing context. We mask out some areas as
// required so we cannot paint over them in later stages
mask *image.Alpha
}
// ColourScheme defines how various features in a city should be coloured.
type ColourScheme struct {
Roads color.Color
Walls color.Color
Towers color.Color
Gates color.Color
Bridges color.Color
Buildings color.Color
Districts map[DistrictType]color.Color
}
// DefaultScheme returns a reasonable default ColourScheme.
func DefaultScheme() *ColourScheme {
return &ColourScheme{
Roads: colornames.Dimgray,
Bridges: colornames.Darkgray,
Walls: colornames.Black,
Towers: colornames.Crimson,
Gates: colornames.Black,
Buildings: colornames.Black,
Districts: map[DistrictType]color.Color{
Park: colornames.Lightgreen,
Temple: colornames.Gold,
Civic: colornames.Indigo,
Graveyard: colornames.Lightgray,
ResidentialUpper: colornames.Royalblue,
ResidentialMiddle: colornames.Steelblue,
ResidentialLower: colornames.Slateblue,
ResidentialSlum: colornames.Darkslateblue,
Abandoned: colornames.Whitesmoke,
Fortress: colornames.Crimson,
Market: colornames.Fuchsia,
Commercial: colornames.Hotpink,
Square: colornames.Yellow,
Industrial: colornames.Firebrick,
Warehouse: colornames.Brown,
Barracks: colornames.Maroon,
Prison: colornames.Grey,
Docks: colornames.Lightblue,
Research: colornames.Mediumturquoise,
Fields: colornames.Wheat,
Empty: colornames.White,
},
}
}
func (c *imageMap) setBuilding(x, y int, b *BuildingConfig) {
if b == nil {
return
}
for dx := b.Area.Min.X; dx < b.Area.Max.X; dx++ {
for dy := b.Area.Min.Y; dy < b.Area.Max.Y; dy++ {
c.setBuildingID(x+dx, y+dy, b.ID)
}
}
}
// Save the CityMap as is to disk
func (c *imageMap) Save(fpath string) error {
return savePNG(fpath, c.im)
}
// CustomImage returns the CityMap coloured with the given Scheme
func (c *imageMap) CustomImage(scheme *ColourScheme) (image.Image, error) {
bnds := c.im.Bounds()
im := image.NewRGBA(bnds)
for dy := bnds.Min.Y; dy < bnds.Max.Y; dy++ {
for dx := bnds.Min.X; dx < bnds.Max.X; dx++ {
bm := c.getBM(dx, dy)
if bm.Get(bitGate) {
im.Set(dx, dy, scheme.Gates)
continue
} else if bm.Get(bitTower) {
im.Set(dx, dy, scheme.Towers)
continue
} else if bm.Get(bitWall) {
im.Set(dx, dy, scheme.Walls)
continue
} else if bm.Get(bitBridge) {
im.Set(dx, dy, scheme.Bridges)
continue
} else if bm.Get(bitRoad) {
im.Set(dx, dy, scheme.Roads)
continue
}
buildingID, err := c.BuildingID(dx, dy)
if err != nil {
return nil, err
}
if buildingID != 0 {
im.Set(dx, dy, scheme.Buildings)
continue
}
dtype, _, err := c.District(dx, dy)
if err != nil {
return nil, err
}
col, ok := scheme.Districts[dtype]
if ok {
im.Set(dx, dy, col)
}
}
}
return im, nil
}
// SaveAdv essentially saves the CityMap using the given scheme to disk.
// Essentially sugar around "CustomImage()" followed by writing out a PNG.
func (c *imageMap) SaveAdv(fpath string, scheme *ColourScheme) error {
im, err := c.CustomImage(scheme)
if err != nil {
return err
}
ctx := gg.NewContextForRGBA(im.(*image.RGBA))
return ctx.SavePNG(fpath)
}
// District returns the district type & ID at x,y
func (c *imageMap) District(x, y int) (DistrictType, int, error) {
if c.isOutOfBounds(x, y) {
return Empty, -1, fmt.Errorf("(%d,%d) is out of bounds", x, y)
}
v := c.im.RGBA64At(x, y)
typeid, _ := encoding.Split16(v.A)
return districtForID(int(typeid)), int(v.R), nil
}
// BuildingID returns the buildingID (see BuildingConfig.ID) at x,y
// A value of 0 indicates that there is no building.
func (c *imageMap) BuildingID(x, y int) (int, error) {
if c.isOutOfBounds(x, y) {
return -1, fmt.Errorf("(%d,%d) is out of bounds", x, y)
}
v := c.im.RGBA64At(x, y)
return int(encoding.Merge16(v.G, v.B)), nil
}
// setBuildingID sets the given ID at x,y
func (c *imageMap) setBuildingID(x, y, id int) {
v := c.im.RGBA64At(x, y)
g, b := encoding.Split32(uint32(id))
v.G = g
v.B = b
c.im.SetRGBA64(x, y, v)
}
// setDistrict sets the given type & id at x,y
func (c *imageMap) setDistrict(x, y int, typ DistrictType, id int) error {
denum := typ.ID()
v := c.im.RGBA64At(x, y)
_, bmbits := encoding.Split16(v.A)
v.R = uint16(id)
v.A = encoding.Merge8(uint8(denum), bmbits)
c.im.SetRGBA64(x, y, v)
return nil
}
// setRoad sets x,y as Road
func (c *imageMap) setRoad(x, y int) {
bm := c.getBM(x, y)
bm.Set(bitRoad, true)
c.setBM(x, y, bm)
}
// setBridge sets x,y as bridge
func (c *imageMap) setBridge(x, y int) {
bm := c.getBM(x, y)
bm.Set(bitBridge, true)
c.setBM(x, y, bm)
}
// setBM sets the 8 bit bitmap at x,y
func (c *imageMap) setBM(x, y int, bm bitmap.Bitmap) {
num := encoding.FromBytes8(bm.Data(true))
current := c.im.RGBA64At(x, y)
dtype, _ := encoding.Split16(current.A)
current.A = encoding.Merge8(dtype, num)
c.im.SetRGBA64(x, y, current)
}
// getBM gets the 8 bit bitmap at x,y
func (c *imageMap) getBM(x, y int) bitmap.Bitmap {
current := c.im.RGBA64At(x, y)
_, bmdata := encoding.Split16(current.A)
data := encoding.ToBytes8(bmdata)
return bitmap.Bitmap(data)
}
// isFortification returns if x,y is any of Tower, Gate, Wall
func (c *imageMap) isFortification(x, y int) bool {
_, _, b, _ := c.ctx.Image().At(x, y).RGBA()
return b > 0
}
// IsWall returns if there is a wall at x,y
func (c *imageMap) IsWall(x, y int) bool {
if c.isOutOfBounds(x, y) {
return false
}
return c.getBM(x, y).Get(bitWall)
}
// IsTower returns if there is a tower at x,y
func (c *imageMap) IsTower(x, y int) bool {
if c.isOutOfBounds(x, y) {
return false
}
return c.getBM(x, y).Get(bitTower)
}
// IsGatehouse returns if there is a gatehouse at x,y
func (c *imageMap) IsGatehouse(x, y int) bool {
if c.isOutOfBounds(x, y) {
return false
}
return c.getBM(x, y).Get(bitGate)
}
// IsRoad returns if there is a road at x,y
func (c *imageMap) IsRoad(x, y int) bool {
if c.isOutOfBounds(x, y) {
return false
}
return c.getBM(x, y).Get(bitRoad)
}
// IsBridge returns if there is a bridge at x,y
func (c *imageMap) IsBridge(x, y int) bool {
if c.isOutOfBounds(x, y) {
return false
}
return c.getBM(x, y).Get(bitBridge)
}
// isOutOfBounds determines if x,y is outside of the image area
func (c *imageMap) isOutOfBounds(x, y int) bool {
bnds := c.im.Bounds()
return x < bnds.Min.X || x > bnds.Max.X || y < bnds.Min.Y || y > bnds.Max.Y
}
// nearbyFortifications returns of any pixels within radius of x,y in `im`
// are wall, gatehouse or tower
func nearbyFortifications(x, y, radius int, im image.Image) bool {
for dy := y - radius; dy < y+radius; dy++ {
for dx := x - radius; dx < x+radius; dx++ {
_, _, b, _ := im.At(dx, dy).RGBA()
if b >= 50 {
return true
}
}
}
return false
}
// endDraw copies our rough road / wall outline sketch to our proper map.
// Basically we paint roads / walls / towers / gatehouses because it's
// much easier to lean on a graphics library & then copy the results
// over to our main image later.
func (c *imageMap) endDraw(wallBorderRoadWidth int, outline Outline) {
temp := c.ctx.Image()
bnds := temp.Bounds()
for dy := bnds.Min.Y; dy < bnds.Max.Y; dy++ {
for dx := bnds.Min.X; dx < bnds.Max.X; dx++ {
r, g, b, _ := temp.At(dx, dy).RGBA()
r = r >> 8
g = g >> 8
b = b >> 8
bm := bitmap.New(8)
if r == 0 && g == 0 && b == 0 {
road := outline.CanBuildOn(dx, dy)
water := outline.CanBridgeOver(dx, dy)
if (road || water) && nearbyFortifications(dx, dy, wallBorderRoadWidth, temp) {
bit := bitRoad
if water {
bit = bitBridge
}
bm.Set(bit, true)
} else {
continue
}
} else {
if g > 0 {
bm.Set(bitBridge, true) // bridge
}
if b >= 150 {
bm.Set(bitGate, true)
} else if b >= 100 {
bm.Set(bitTower, true)
} else if b >= 50 {
bm.Set(bitWall, true)
}
if r > 0 {
bm.Set(bitRoad, true) // road
}
}
c.setBM(dx, dy, bm)
}
}
}
// drawGatehouse (rect) on to our scratch image
func (c *imageMap) drawGatehouse(a image.Point, width, height int) {
c.ctx.SetColor(color.RGBA{0, 0, 150, 255})
c.ctx.DrawRectangle(float64(a.X), float64(a.Y), float64(width), float64(height))
c.ctx.Fill()
// mark area as non-drawable
draw.Draw(c.mask, image.Rect(a.X, a.Y, a.X+width, a.Y+height), image.Transparent, image.ZP, draw.Src)
}
// drawTower (rect) on to our scratch image
func (c *imageMap) drawTower(a image.Point, width, height int) {
c.ctx.SetColor(color.RGBA{0, 0, 100, 255})
c.ctx.DrawRectangle(float64(a.X), float64(a.Y), float64(width), float64(height))
c.ctx.Fill()
// mark area as non-drawable
draw.Draw(c.mask, image.Rect(a.X, a.Y, a.X+width, a.Y+height), image.Transparent, image.ZP, draw.Src)
}
// drawWall (line) on to our scratch image
func (c *imageMap) drawWall(a, b image.Point, width int) {
c.ctx.SetColor(color.RGBA{0, 0, 50, 255})
c.ctx.SetLineWidth(float64(width))
c.ctx.SetLineCapSquare()
c.ctx.DrawLine(float64(a.X), float64(a.Y), float64(b.X), float64(b.Y))
c.ctx.Stroke()
}
// drawBridge (line) on to our scratch image
func (c *imageMap) drawBridge(a, b image.Point, width int) {
c.ctx.SetColor(color.RGBA{0, 255, 0, 255})
c.ctx.SetLineCapSquare()
c.ctx.SetLineWidth(float64(width))
c.ctx.DrawLine(float64(a.X), float64(a.Y), float64(b.X), float64(b.Y))
c.ctx.Stroke()
}
// drawRoad (line) on to our scratch image
func (c *imageMap) drawRoad(a, b image.Point, width int) {
c.ctx.SetColor(color.RGBA{255, 0, 0, 255})
c.ctx.SetLineCapSquare()
c.ctx.SetLineWidth(float64(width))
c.ctx.DrawLine(float64(a.X), float64(a.Y), float64(b.X), float64(b.Y))
c.ctx.Stroke()
}
// newMap returns a new map with the given bounds
func newMap(bounds image.Rectangle) *imageMap {
ctx := gg.NewContextForRGBA(image.NewRGBA(bounds))
ctx.SetRGBA(0, 0, 0, 0)
ctx.Clear()
// we use a mask to forbid drawing over our towers / gatehouses with
// walls / roads. Since otherwise we can't ensure that thick lines
// ending at the tower/gatehouse edge wont eat into our shapes
// and it's waaaay more annoying drawing walls / roads first.
mask := image.NewAlpha(bounds)
draw.Draw(mask, mask.Bounds(), image.Opaque, image.ZP, draw.Src)
ctx.SetMask(mask)
return &imageMap{
ctx: ctx,
im: image.NewRGBA64(bounds),
mask: mask,
}
}