-
Notifications
You must be signed in to change notification settings - Fork 1
/
images.go
106 lines (91 loc) · 2.8 KB
/
images.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
// Copyright © 2011 Jeffrey Davis <jeff.davis@gmail.com>
// Use of this code is governed by the GPL version 2 or later.
// See the file LICENSE for details.
package maps
import (
"fmt"
"flag"
"log"
"os"
"image"
"image/png"
. "bugnuts/torus"
)
type ImageHelper struct {
m *Map
pixel func(row, col int) image.NRGBAColor
}
func (ih ImageHelper) ColorModel() image.ColorModel {
return image.NRGBAColorModel
}
func (ih ImageHelper) Bounds() image.Rectangle {
// fmt.Printf("Bounds are 0,0,%d,%d\n", ih.m.Cols*4, ih.m.Rows*4)
return image.Rect(0, 0, ih.m.Cols*4, ih.m.Rows*4)
}
func (ih ImageHelper) At(x, y int) image.Color {
return ih.pixel(y/4, x/4)
}
//implement Image for fancy image debugging
func (m *Map) ColorModel() image.ColorModel {
return image.NRGBAColorModel
}
func (m *Map) Bounds() image.Rectangle {
return image.Rect(0, 0, m.Cols*4, m.Rows*4)
}
func (m *Map) At(x, y int) image.NRGBAColor {
loc := m.ToLocation(Point{y, x})
return ItemColor(m.Grid[loc])
}
func (m *Map) WriteDebugImage(Desc string, seq int, At func(row, col int) image.NRGBAColor) {
//use -imgprefix="bot0" to make a series of images (bot0.0.png ... bot0.N.png) which
//illustrate the bot's knowledge of the map at each turn. If you want the images in a
//subdirectory, make sure you create the directory first. (e.g., -imgprefix="images/bot0")
imageOutPrefix := flag.String("imgprefix", "bot", "prefix for helpful debugging images")
if imageOutPrefix == nil {
return
}
fname := fmt.Sprintf("%s.%s.%3.3d.png", *imageOutPrefix, Desc, seq)
fmt.Printf("making image: %s\n", fname)
f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Panicf("Couldn't open %s (%s)", fname, err)
}
defer f.Close()
err = png.Encode(f, ImageHelper{m, At})
if err != nil {
log.Panicf("Couldn't encode png (%s)", err)
}
}
func ItemColor(o Item) image.NRGBAColor {
switch o {
case UNKNOWN:
return image.NRGBAColor{0xb0, 0xb0, 0xb0, 0xff}
case WATER:
return image.NRGBAColor{0x10, 0x10, 0x50, 0xff}
case FOOD:
return image.NRGBAColor{0xe0, 0xe0, 0xc0, 0xff}
case LAND:
return image.NRGBAColor{0x8b, 0x45, 0x13, 0xff}
case MY_ANT:
return image.NRGBAColor{0xf0, 0x00, 0x00, 0xff}
case PLAYER1:
return image.NRGBAColor{0xf0, 0xf0, 0x00, 0xff}
case PLAYER2:
return image.NRGBAColor{0x00, 0xf0, 0x00, 0xff}
case PLAYER3:
return image.NRGBAColor{0x00, 0x00, 0xf0, 0xff}
case PLAYER4:
return image.NRGBAColor{0xf0, 0x00, 0xf0, 0xff}
case PLAYER5:
return image.NRGBAColor{0xf0, 0xf0, 0xf0, 0xff}
case PLAYER6:
return image.NRGBAColor{0x80, 0x80, 0x00, 0xff}
case PLAYER7:
return image.NRGBAColor{0x00, 0x80, 0x80, 0xff}
case PLAYER8:
return image.NRGBAColor{0x80, 0x00, 0x80, 0xff}
case PLAYER9:
return image.NRGBAColor{0x80, 0x00, 0xf0, 0xff}
}
return image.NRGBAColor{0xff, 0xff, 0xff, 0xff}
}