/
map.go
226 lines (185 loc) · 4.58 KB
/
map.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
// Game map functions
package game
import (
"bufio"
"encoding/gob"
"fmt"
"github.com/nsf/termbox-go"
"github.com/nsf/tulib"
"image"
"log"
"math/rand"
"os"
)
type TerrainType uint32
type Action int
func (a Action) String() string {
return fmt.Sprintf("%s", DirTable[a])
}
const (
MAP_WIDTH = 256
MAP_HEIGHT = 256
T_EMPTY TerrainType = iota
T_WALL // can't pass/see through wall
T_GROUND // passable/visible
T_UNIT
DIR_UP Action = iota // player movement instructions
DIR_DOWN
DIR_LEFT
DIR_RIGHT
ACTION_ITEM_PICKUP
ACTION_ITEM_DROP
ACTION_ITEM_LIST_INVENTORY
)
var (
DirTable = map[Action]image.Point{
DIR_UP: image.Point{0, -1},
DIR_DOWN: image.Point{0, 1},
DIR_LEFT: image.Point{-1, 0},
DIR_RIGHT: image.Point{1, 0},
}
GLYPH_EMPTY = termbox.Cell{Ch: ' '}
GLYPH_WALL = termbox.Cell{Ch: '#', Fg: termbox.ColorDefault, Bg: termbox.ColorBlack | termbox.AttrUnderline | termbox.AttrBold}
GLYPH_GROUND = termbox.Cell{Ch: '.', Fg: termbox.ColorGreen}
GLYPH_FLAG = termbox.Cell{Ch: '%', Fg: termbox.ColorCyan}
GLYPH_ITEM = termbox.Cell{Ch: '?', Fg: termbox.ColorCyan}
GLYPH_HUMAN = termbox.Cell{Ch: '@'}
// convert a rune to a terrain square
glyphTable = map[rune]*Terrain{
' ': &Terrain{GLYPH_EMPTY, T_EMPTY},
'#': &Terrain{GLYPH_WALL, T_WALL},
'.': &Terrain{GLYPH_GROUND, T_GROUND},
'@': &Terrain{GLYPH_HUMAN, T_UNIT},
}
)
func init() {
gob.Register(DIR_UP)
gob.Register(&MapChunk{})
gob.Register(&Terrain{})
gob.Register(T_EMPTY)
}
func (tt *TerrainType) String() string {
switch *tt {
case T_EMPTY:
return "empty"
case T_WALL:
return "wall"
case T_GROUND:
return "ground"
case T_UNIT:
return "unit"
}
return "unknown"
}
func GlyphToTerrain(g rune) (t *Terrain, ok bool) {
t, ok = glyphTable[g]
if !ok {
t = glyphTable[' ']
}
return
}
type Terrain struct {
//*GameObject
Glyph termbox.Cell
Type TerrainType
}
func (t Terrain) String() string {
return fmt.Sprintf("(%c %s)", t.Glyph.Ch, t.Type)
}
func (t *Terrain) Draw(b *tulib.Buffer, pt image.Point) {
b.Set(pt.X, pt.Y, t.Glyph)
}
func (t *Terrain) IsEmpty() bool {
return t.Type == T_EMPTY
}
func (t *Terrain) IsWall() bool {
return t.Type == T_WALL
}
func (t *Terrain) IsGround() bool {
return t.Type == T_GROUND
}
type MapChunk struct {
Size image.Point
Rect image.Rectangle
Locations [][]*Terrain // land features
GameObjects []*GameObject // active game objects
Players []*Player // active players
}
func (mc *MapChunk) String() string {
return fmt.Sprintf("(%s %s objs %d players %d)", mc.Size, mc.Rect, len(mc.GameObjects), len(mc.Players))
}
func NewMapChunk() *MapChunk {
ch := MapChunk{Size: image.Pt(MAP_WIDTH, MAP_HEIGHT)}
ch.Rect = image.Rect(0, 0, MAP_WIDTH, MAP_HEIGHT)
ch.Locations = make([][]*Terrain, MAP_WIDTH)
for row := range ch.Locations {
ch.Locations[row] = make([]*Terrain, MAP_HEIGHT)
}
for x := 0; x < MAP_WIDTH; x++ {
for y := 0; y < MAP_HEIGHT; y++ {
g, _ := GlyphToTerrain('.')
ch.Locations[x][y] = g
}
}
return &ch
}
// return true if the map chunk has a cell with coordinates v.X, v.Y
func (mc *MapChunk) HasCell(pt image.Point) bool {
return pt.In(mc.Rect)
}
// get terrain at v. returns nil, false if it is not present
func (mc *MapChunk) GetTerrain(pt image.Point) (t *Terrain, ok bool) {
if ok = mc.HasCell(pt); !ok {
return
}
return mc.Locations[pt.X][pt.Y], true
}
func (mc *MapChunk) CheckCollision(gob *GameObject, pos image.Point) bool {
t, ok := mc.GetTerrain(pos)
if ok {
return !t.IsWall()
}
return false
}
// Generates an array of (x,y) tuples of open
// spots on the map, called open, and selects
// random(1, len(open))
func (mc *MapChunk) RandCell() image.Point {
var open []image.Point
for x := 0; x < MAP_WIDTH; x++ {
for y := 0; y < MAP_HEIGHT; y++ {
if !mc.Locations[x][y].IsWall() {
open = append(open, image.Pt(x, y))
}
}
}
// choose random location in range or len(open)
i := rand.Intn(len(open))
return open[i]
}
func MapChunkFromFile(mapfile string) *MapChunk {
mfh, err := os.Open(mapfile)
if err != nil {
log.Printf("Error loading map chunk file '%s': %s", mapfile, err)
return nil
}
defer mfh.Close()
r := bufio.NewReader(mfh)
mc := NewMapChunk()
for y := 0; y < MAP_HEIGHT; y++ {
str, err := r.ReadString('\n')
if err != nil {
log.Printf("map read error: %s", err)
return nil
}
for x := 0; x < MAP_WIDTH; x++ {
g, ok := GlyphToTerrain(rune(str[x]))
if !ok {
log.Printf("invalid map tile '%c' at %s:%d:%d", str[x], mapfile, y, x)
return nil
}
mc.Locations[x][y] = g
}
}
return mc
}