This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
engine.go
311 lines (266 loc) · 5.94 KB
/
engine.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
package main
import (
"time"
"github.com/gdamore/tcell"
)
// EventEngineStopRun stop the run of the engine
type EventEngineStopRun struct {
EventGame
}
// NewEngine creates new engine
func NewEngine() {
engine = &Engine{
chanStop: make(chan struct{}),
chanEventKey: make(chan *tcell.EventKey, 8),
mode: engineModeGameOver,
tickTime: time.Hour,
ranking: NewRanking(),
ai: NewAi(),
}
board.Clear()
go engine.Run()
}
// Run runs the engine
func (engine *Engine) Run() {
logger.Println("Engine Run start")
var event tcell.Event
loop:
for {
event = screen.PollEvent()
switch eventType := event.(type) {
case *tcell.EventKey:
select {
case engine.chanEventKey <- eventType:
default:
}
case *EventEngineStopRun:
break loop
case *tcell.EventResize:
view.RefreshScreen()
default:
logger.Printf("event type %T", eventType)
}
}
logger.Println("Engine Run end")
}
// Start the game
func (engine *Engine) Start() {
logger.Println("Engine Start start")
engine.timer = time.NewTimer(engine.tickTime)
engine.timer.Stop()
engine.aiTimer = time.NewTimer(engine.tickTime)
engine.aiTimer.Stop()
view.RefreshScreen()
var eventKey *tcell.EventKey
loop:
for {
select {
case <-engine.chanStop:
break loop
default:
select {
case eventKey = <-engine.chanEventKey:
engine.ProcessEventKey(eventKey)
view.RefreshScreen()
case <-engine.timer.C:
engine.tick()
case <-engine.aiTimer.C:
engine.ai.ProcessQueue()
engine.aiTimer.Reset(engine.tickTime / aiTickDivider)
case <-engine.chanStop:
break loop
}
}
}
screen.PostEventWait(&EventEngineStopRun{})
logger.Println("Engine Start end")
}
// Stop the game
func (engine *Engine) Stop() {
logger.Println("Engine Stop start")
if !engine.stopped {
engine.stopped = true
engine.mode = engineModeStopped
close(engine.chanStop)
}
engine.timer.Stop()
engine.aiTimer.Stop()
logger.Println("Engine Stop end")
}
// Pause the game
func (engine *Engine) Pause() {
if !engine.timer.Stop() {
select {
case <-engine.timer.C:
default:
}
}
if !engine.aiTimer.Stop() {
select {
case <-engine.aiTimer.C:
default:
}
}
engine.mode = engineModePaused
}
// UnPause the game
func (engine *Engine) UnPause() {
engine.timer.Reset(engine.tickTime)
if engine.aiEnabled {
engine.aiTimer.Reset(engine.tickTime / aiTickDivider)
engine.mode = engineModeRunWithAI
} else {
engine.mode = engineModeRun
}
}
// PreviewBoard sets previewBoard to true
func (engine *Engine) PreviewBoard() {
engine.mode = engineModePreview
}
// NewGame resets board and starts a new game
func (engine *Engine) NewGame() {
logger.Println("Engine NewGame start")
board.Clear()
engine.tickTime = 480 * time.Millisecond
engine.score = 0
engine.level = 1
engine.deleteLines = 0
loop:
for {
select {
case <-engine.chanEventKey:
default:
break loop
}
}
if engine.aiEnabled {
engine.ai.GetBestQueue()
engine.mode = engineModeRunWithAI
} else {
engine.mode = engineModeRun
}
engine.UnPause()
logger.Println("Engine NewGame end")
}
// ResetTimer resets the time for lock delay or tick time
func (engine *Engine) ResetTimer(duration time.Duration) {
if !engine.timer.Stop() {
select {
case <-engine.timer.C:
default:
}
}
if duration == 0 {
// duration 0 means tick time
engine.timer.Reset(engine.tickTime)
} else {
// duration is lock delay
engine.timer.Reset(duration)
}
}
// AiGetBestQueue calls AI to get best queue
func (engine *Engine) AiGetBestQueue() {
if !engine.aiEnabled {
return
}
go engine.ai.GetBestQueue()
}
// tick move mino down and refreshes screen
func (engine *Engine) tick() {
board.MinoMoveDown()
view.RefreshScreen()
}
// AddDeleteLines adds deleted lines to score
func (engine *Engine) AddDeleteLines(lines int) {
engine.deleteLines += lines
if engine.deleteLines > 999999 {
engine.deleteLines = 999999
}
switch lines {
case 1:
engine.AddScore(40 * (engine.level + 1))
case 2:
engine.AddScore(100 * (engine.level + 1))
case 3:
engine.AddScore(300 * (engine.level + 1))
case 4:
engine.AddScore(1200 * (engine.level + 1))
}
if engine.level < engine.deleteLines/10 {
engine.LevelUp()
}
}
// AddScore adds to score
func (engine *Engine) AddScore(add int) {
engine.score += add
if engine.score > 9999999 {
engine.score = 9999999
}
}
// LevelUp goes up a level
func (engine *Engine) LevelUp() {
if engine.level >= 30 {
return
}
engine.level++
switch {
case engine.level > 29:
engine.tickTime = 10 * time.Millisecond
case engine.level > 25:
engine.tickTime = 20 * time.Millisecond
case engine.level > 19:
// 50 to 30
engine.tickTime = time.Duration(10*(15-engine.level/2)) * time.Millisecond
case engine.level > 9:
// 150 to 60
engine.tickTime = time.Duration(10*(25-engine.level)) * time.Millisecond
default:
// 480 to 160
engine.tickTime = time.Duration(10*(52-4*engine.level)) * time.Millisecond
}
}
// GameOver pauses engine and sets to game over
func (engine *Engine) GameOver() {
logger.Println("Engine GameOver start")
engine.Pause()
engine.mode = engineModeGameOver
view.ShowGameOverAnimation()
loop:
for {
select {
case <-engine.chanEventKey:
default:
break loop
}
}
engine.ranking.InsertScore(uint64(engine.score))
engine.ranking.Save()
logger.Println("Engine GameOver end")
}
// EnabledAi enables the AI
func (engine *Engine) EnabledAi() {
engine.aiEnabled = true
go engine.ai.GetBestQueue()
engine.aiTimer.Reset(engine.tickTime / aiTickDivider)
}
// DisableAi disables the AI
func (engine *Engine) DisableAi() {
engine.aiEnabled = false
engine.mode = engineModeRun
if !engine.aiTimer.Stop() {
select {
case <-engine.aiTimer.C:
default:
}
}
}
// EnabledEditMode enables edit mode
func (engine *Engine) EnabledEditMode() {
edit.EnabledEditMode()
engine.mode = engineModeEdit
}
// DisableEditMode disables edit mode
func (engine *Engine) DisableEditMode() {
edit.DisableEditMode()
engine.mode = engineModePreview
}