This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
move.go
372 lines (337 loc) · 9.26 KB
/
move.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
// This example program shows how to implement movement on a grid either on
// keyboard or mouse input. It implements both single-step movement and
// automatic movement in a direction or path, and provides some simple map
// generation and field of vision.
package main
import (
"bytes"
"context"
"fmt"
"log"
"math/rand"
"strings"
"time"
"github.com/anaseto/gruid"
"github.com/anaseto/gruid/paths"
"github.com/anaseto/gruid/rl"
"github.com/anaseto/gruid/ui"
)
func main() {
// our application's state and grid with default config
gd := gruid.NewGrid(80, 24)
pr := paths.NewPathRange(gd.Bounds())
m := &model{grid: gd, pr: pr}
framebuf := &bytes.Buffer{} // for compressed recording
// define new application
app := gruid.NewApp(gruid.AppConfig{
Driver: driver,
Model: m,
FrameWriter: framebuf,
})
// start application
if err := app.Start(context.Background()); err != nil {
driver.Close()
log.Fatal(err)
}
// launch replay just after the previous session
fd, err := gruid.NewFrameDecoder(framebuf)
if err != nil {
log.Fatal(err)
}
gd = gruid.NewGrid(80, 24)
rep := ui.NewReplay(ui.ReplayConfig{
Grid: gd,
FrameDecoder: fd,
})
app = gruid.NewApp(gruid.AppConfig{
Driver: driver,
Model: rep,
})
if err := app.Start(context.Background()); err != nil {
log.Fatal(err)
} else {
fmt.Println("Successful quit.")
}
}
// Those constants represent the generic colors we use in this example.
const (
ColorPlayer gruid.Color = 1 + iota // skip special zero value gruid.ColorDefault
ColorLOS
ColorDark
)
// Those constants represent styling attributes.
const (
AttrNone gruid.AttrMask = iota
AttrReverse
)
// Those constants represent the different types of terrains in the map grid.
// We use the second bit for marking a cell explored or not.
const (
Wall rl.Cell = iota
Ground
Explored rl.Cell = 0b10
)
// maxLOS is the maximum sight range.
const maxLOS = 10
func cell(c rl.Cell) rl.Cell {
return c &^ Explored
}
func explored(c rl.Cell) bool {
return c&Explored != 0
}
// models represents our main application state.
type model struct {
grid gruid.Grid // drawing grid
playerPos gruid.Point // tracks player position
move autoMove // automatic movement
pr *paths.PathRange // path finding in the grid range
path []gruid.Point // current path (reverse highlighting)
mapgd rl.Grid // map grid
rand *rand.Rand // random number generator
fov *rl.FOV // field of vision
}
// autoMove represents the information for an automatic-movement step.
type autoMove struct {
// delta represents a position variation such as (0,1), that
// will be used in position arithmetic to move from one position to an
// adjacent one in a certain direction.
delta gruid.Point
path bool // whether following a path (instead of a simple direction)
}
// msgAutoMove is used to ask Update to move the player's position by delta.
type msgAutoMove struct {
delta gruid.Point
}
// Update implements gruid.Model.Update. It handles keyboard and mouse input
// messages and updates the model in response to them.
func (m *model) Update(msg gruid.Msg) gruid.Effect {
switch msg := msg.(type) {
case gruid.MsgInit:
m.InitializeMap()
case gruid.MsgKeyDown:
return m.updateMsgKeyDown(msg)
case gruid.MsgMouse:
return m.updateMsgMouse(msg)
case msgAutoMove:
return m.updateMsgAutomove(msg)
}
return nil
}
func (m *model) InitializeMap() {
m.mapgd = rl.NewGrid(80, 24)
m.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
wlk := walker{rand: m.rand}
mgen := rl.MapGen{Rand: m.rand, Grid: m.mapgd}
if m.rand.Float64() > 0.5 {
mgen.RandomWalkCave(wlk, Ground, 0.5, 1)
} else {
rules := []rl.CellularAutomataRule{
{WCutoff1: 5, WCutoff2: 2, Reps: 4, WallsOutOfRange: true},
{WCutoff1: 5, WCutoff2: 25, Reps: 3, WallsOutOfRange: true},
}
mgen.CellularAutomataCave(Wall, Ground, 0.40, rules)
}
max := m.mapgd.Size()
var p gruid.Point
for {
// find an empty starting position for the player
p = gruid.Point{m.rand.Intn(max.X), m.rand.Intn(max.Y)}
if cell(m.mapgd.At(p)) != Wall {
break
}
}
m.fov = rl.NewFOV(gruid.NewRange(-maxLOS, -maxLOS, maxLOS+1, maxLOS+1))
m.MovePlayer(p)
}
func (m *model) MovePlayer(to gruid.Point) {
// We shift the FOV's Range so that it will be centered on the new
// player's position. We could have simply used the whole map for the
// range, though it would have used a little bit more memory (not
// important here, just for showing what can be done).
rg := gruid.NewRange(-maxLOS, -maxLOS, maxLOS+1, maxLOS+1)
m.fov.SetRange(rg.Add(to).Intersect(m.mapgd.Range()))
m.playerPos = to
// We mark cells in field of view as explored.
passable := func(p gruid.Point) bool {
return cell(m.mapgd.At(p)) != Wall
}
for _, p := range m.fov.SSCVisionMap(m.playerPos, maxLOS, passable, false) {
if distance(p, m.playerPos) > maxLOS {
continue
}
c := m.mapgd.At(p)
if !explored(c) {
m.mapgd.Set(p, c|Explored)
}
}
}
func (m *model) updateMsgKeyDown(msg gruid.MsgKeyDown) gruid.Effect {
// cancel automatic movement on any key
if m.autoMove() {
m.stopAuto()
return nil
}
// remove mouse path highlighting
m.path = nil
pdelta := gruid.Point{}
switch msg.Key {
case gruid.KeyArrowDown, "j", "J":
pdelta = pdelta.Shift(0, 1)
case gruid.KeyArrowLeft, "h", "H":
pdelta = pdelta.Shift(-1, 0)
case gruid.KeyArrowRight, "l", "L":
pdelta = pdelta.Shift(1, 0)
case gruid.KeyArrowUp, "k", "K":
pdelta = pdelta.Shift(0, -1)
case "Q", "q", gruid.KeyEscape:
return gruid.End()
}
if pdelta.X != 0 || pdelta.Y != 0 {
np := m.playerPos.Add(pdelta) //
if m.grid.Contains(np) && cell(m.mapgd.At(np)) != Wall {
m.MovePlayer(np)
if msg.Mod&gruid.ModShift != 0 || strings.ToUpper(string(msg.Key)) == string(msg.Key) {
// activate automatic movement in that direction
m.move.delta = pdelta
return automoveCmd(m.move.delta)
}
}
}
return nil
}
func (m *model) updateMsgMouse(msg gruid.MsgMouse) gruid.Effect {
switch msg.Action {
case gruid.MouseMain:
if m.autoMove() {
m.stopAuto()
m.pathSet(msg.P)
break
}
if len(m.path) > 1 {
return m.pathNext()
}
case gruid.MouseMove:
if m.autoMove() {
break
}
m.pathSet(msg.P)
}
return nil
}
func (m *model) updateMsgAutomove(msg msgAutoMove) gruid.Effect {
if m.move.delta != msg.delta {
return nil
}
if m.move.path {
if len(m.path) > 1 {
return m.pathNext()
}
} else {
np := m.playerPos.Add(msg.delta)
if m.grid.Contains(np) && cell(m.mapgd.At(np)) != Wall {
m.path = nil // remove path highlighting if any
m.MovePlayer(np)
// continue automatic movement in the same direction
return automoveCmd(msg.delta)
}
}
m.stopAuto()
return nil
}
// automoveCmd returns a command that signals automatic movement in a given
// direction.
func automoveCmd(pdelta gruid.Point) gruid.Cmd {
d := time.Millisecond * 30 // automatic movement time interval
return func() gruid.Msg {
t := time.NewTimer(d)
<-t.C
return msgAutoMove{pdelta}
}
}
// autoMove checks whether automatic movement is activated.
func (m *model) autoMove() bool {
p := gruid.Point{}
return m.move.delta != p
}
// stopAuto resets automatic movement information.
func (m *model) stopAuto() {
m.move = autoMove{}
m.path = nil
}
// pathSet updates the path from player to a new position.
func (m *model) pathSet(p gruid.Point) {
m.path = m.pr.JPSPath(m.path, m.playerPos, p, m.passable, false)
}
// pathNext moves the player to next position in the path, updates the path
// accordingly, and returns a command that will deliver the message for the
// next automatic movement step along the path.
func (m *model) pathNext() gruid.Cmd {
p := m.path[1]
m.path = m.path[1:]
m.move.path = true
m.move.delta = p.Sub(m.playerPos)
m.MovePlayer(p)
return automoveCmd(m.move.delta)
}
func (m *model) passable(p gruid.Point) bool {
if !m.mapgd.Contains(p) {
return false
}
c := m.mapgd.At(p)
return explored(c) && cell(c) != Wall
}
// walker implements rl.RandomWalker.
type walker struct {
rand *rand.Rand
}
// Neighbor returns a random neighbor position, favoring horizontal directions
// (because the maps we use are longer in that direction).
func (w walker) Neighbor(p gruid.Point) gruid.Point {
switch w.rand.Intn(6) {
case 0, 1:
return p.Shift(1, 0)
case 2, 3:
return p.Shift(-1, 0)
case 4:
return p.Shift(0, 1)
default:
return p.Shift(0, -1)
}
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func distance(p, q gruid.Point) int {
p = p.Sub(q)
return abs(p.X) + abs(p.Y)
}
// Draw implements gruid.Model.Draw. It draws a simple map that spans the whole
// grid.
func (m *model) Draw() gruid.Grid {
m.mapgd.Iter(func(p gruid.Point, c rl.Cell) {
st := gruid.Style{}
if m.fov.Visible(p) && distance(p, m.playerPos) <= maxLOS {
st = st.WithFg(ColorLOS)
} else {
st = st.WithBg(ColorDark)
}
switch {
case p == m.playerPos:
m.grid.Set(p, gruid.Cell{Rune: '@', Style: st.WithFg(ColorPlayer)})
case !explored(c):
m.grid.Set(p, gruid.Cell{Rune: ' ', Style: st})
case cell(c) == Wall:
m.grid.Set(p, gruid.Cell{Rune: '#', Style: st})
case cell(c) == Ground:
m.grid.Set(p, gruid.Cell{Rune: '.', Style: st})
}
})
for _, p := range m.path {
c := m.grid.At(p)
m.grid.Set(p, c.WithStyle(c.Style.WithAttrs(AttrReverse)))
}
return m.grid
}