-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTetrisAgent.cs
308 lines (262 loc) · 10.7 KB
/
TetrisAgent.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using GameBot.Core;
using GameBot.Core.Data;
using GameBot.Core.Exceptions;
using GameBot.Core.Extensions;
using GameBot.Game.Tetris.Commands;
using GameBot.Game.Tetris.Data;
using GameBot.Game.Tetris.Extraction;
using GameBot.Game.Tetris.Extraction.Extractors;
using GameBot.Game.Tetris.Extraction.Samplers;
using GameBot.Game.Tetris.Searching;
using GameBot.Game.Tetris.States;
namespace GameBot.Game.Tetris
{
public class TetrisAgent : IAgent
{
private readonly bool _visualize;
// state informations
private IState _state;
private bool _continue;
// services and data used by states
public IConfig Config { get; }
public IClock Clock { get; private set; }
public IQuantizer Quantizer { get; private set; }
public IExecutor Executor { get; private set; }
public IScreenshot Screenshot { get; private set; }
public IExtractor Extractor { get; private set; }
public IBoardExtractor BoardExtractor { get; private set; }
public IScreenExtractor ScreenExtractor { get; private set; }
public ISearch Search { get; private set; }
// data used by states
public GameState GameState { get; set; }
#region multiplayer only
public ISampler<int> MultiplayerHoleSampler { get; private set; }
#endregion
#region config
public bool IsVisualize => Config.Read("Game.Tetris.Visualize", false);
public int StartLevel => Config.Read("Game.Tetris.StartLevel", 0);
public int NumSamples => Config.Read("Game.Tetris.Extractor.Samples", 1);
public bool IsMultiplayer => Config.Read("Game.Tetris.Multiplayer", false);
public bool CheckEnabled => Config.Read("Game.Tetris.Check.Enabled", false);
public bool IsHeartMode => Config.Read("Game.Tetris.HeartMode", false);
#endregion
#region timing
// timing config
private readonly TimeSpan _hitTime;
private readonly TimeSpan _hitDelayAfter;
public readonly TimeSpan MoreTimeToAnalyze;
public readonly TimeSpan LessFallTimeBeforeDrop;
public readonly TimeSpan LessWaitTimeAfterDrop;
public TimeSpan GetExecutionDuration(int commands)
{
// the drop command is not counted here, because
// it has no delay time (press and release, no hit)
commands = Math.Max(0, commands - 1);
return (_hitTime + _hitDelayAfter).Multiply(commands);
}
#endregion
#region visualization
// for visualization only
public Piece ExtractedPiece { private get; set; }
public Piece TracedPiece { private get; set; }
public Tetrimino? ExtractedNextPiece { private get; set; }
public int SearchHeight { private get; set; }
#endregion
public TetrisAgent(IConfig config, IClock clock, IQuantizer quantizer, IExecutor exceutor, IExtractor extractor, IBoardExtractor boardExtractor, IScreenExtractor screenExtractor, ISearch search)
{
Config = config;
Clock = clock;
Quantizer = quantizer;
Executor = exceutor;
Extractor = extractor;
BoardExtractor = boardExtractor;
ScreenExtractor = screenExtractor;
Search = search;
_visualize = IsVisualize;
// init timing config
_hitTime = TimeSpan.FromMilliseconds(Config.Read<int>("Robot.Actuator.Hit.Time"));
_hitDelayAfter = TimeSpan.FromMilliseconds(Config.Read<int>("Robot.Actuator.Hit.DelayAfter"));
MoreTimeToAnalyze = TimeSpan.FromMilliseconds(Config.Read<int>("Game.Tetris.Timing.MoreTimeToAnalyze"));
LessFallTimeBeforeDrop = TimeSpan.FromMilliseconds(Config.Read<int>("Game.Tetris.Timing.LessFallTimeBeforeDrop"));
LessWaitTimeAfterDrop = TimeSpan.FromMilliseconds(Config.Read<int>("Game.Tetris.Timing.LessWaitTimeAfterDrop"));
Init();
}
private void Init()
{
ResetVisualization();
// init game state and agent state
if (IsMultiplayer)
{
MultiplayerHoleSampler = new MultiplayerPenaltyLinesHolePositionSampler(NumSamples);
// TODO: is it correct that the game in multiplayer mode starts always in level 0?
GameState = new GameState { StartLevel = 0, HeartMode = false };
}
else
{
GameState = new GameState { StartLevel = StartLevel, HeartMode = IsHeartMode };
}
SetState(new ReadyState(this));
}
private void ResetVisualization()
{
ExtractedPiece = null;
ExtractedNextPiece = null;
TracedPiece = null;
SearchHeight = 0;
}
public void SetState(IState newState)
{
if (newState == null)
throw new ArgumentNullException(nameof(newState));
_state = newState;
_continue = false;
}
public void SetStateAndContinue(IState newState)
{
if (newState == null)
throw new ArgumentNullException(nameof(newState));
_state = newState;
_continue = true;
}
public void Extract(IScreenshot screenshot)
{
Screenshot = screenshot;
do
{
// every state can directly execute the next state,
// when it changes this flag to true (with SetStateAndContinue)
_continue = false;
try
{
_state.Extract();
}
catch (GameOverException)
{
// game over detected
SetState(new GameOverState(this));
throw; // let the engine decide what to do
}
} while (_continue);
}
public Mat Visualize(Mat image)
{
if (!_visualize)
{
// no visualization
return image;
}
var visualization = new Mat();
CvInvoke.CvtColor(image, visualization, ColorConversion.Gray2Bgr);
if (GameState?.Board != null)
{
var board = GameState.Board;
for (int x = 0; x < board.Width; x++)
{
for (int y = 0; y < board.Height; y++)
{
if (board.IsOccupied(x, y))
{
var tileCoordinates = Coordinates.BoardToTile(x, y);
DrawBlock(visualization, tileCoordinates, Color.DodgerBlue);
}
}
}
}
if (TracedPiece != null)
{
// current piece
var piece = TracedPiece;
foreach (var block in piece.Shape.Body)
{
var tileCoordinates = Coordinates.PieceToTile(piece.X + block.X, piece.Y + block.Y);
DrawBlock(visualization, tileCoordinates, Color.Orange);
}
}
if (ExtractedPiece != null)
{
// current piece
var piece = ExtractedPiece;
foreach (var block in piece.Shape.Body)
{
var tileCoordinates = Coordinates.PieceToTile(piece.X + block.X, piece.Y + block.Y);
DrawBlock(visualization, tileCoordinates, Color.Red);
}
}
if (ExtractedNextPiece != null)
{
// next piece
foreach (var block in Shape.Get(ExtractedNextPiece.Value).Body)
{
var tileCoordinates = Coordinates.PieceToTilePreview(block);
DrawBlock(visualization, tileCoordinates, Color.LimeGreen);
}
}
if (SearchHeight > 0)
{
DrawLine(visualization, SearchHeight, Color.Red);
}
return visualization;
}
private void DrawBlock(Mat image, Point tileCoordinates, Color color)
{
const int frameSize = 0;
var rectangle = new Rectangle(GameBoyConstants.TileSize * tileCoordinates.X + frameSize, GameBoyConstants.TileSize * tileCoordinates.Y + frameSize, GameBoyConstants.TileSize - 2 * frameSize - 1, GameBoyConstants.TileSize - 2 * frameSize - 1);
CvInvoke.Rectangle(image, rectangle, new Bgr(color).MCvScalar, 1, LineType.FourConnected);
}
private void DrawLine(Mat image, int height, Color color)
{
var pointFrom = new Point(2 * GameBoyConstants.TileSize, GameBoyConstants.TileSize * (height + 3));
var pointTo = new Point(12 * GameBoyConstants.TileSize - 1, GameBoyConstants.TileSize * (height + 3));
CvInvoke.Line(image, pointFrom, pointTo, new Bgr(color).MCvScalar, 1, LineType.FourConnected);
}
public void Play(IExecutor executor)
{
do
{
// every state can directly execute the next state,
// when it changes this flag to true (with SetStateAndContinue)
_continue = false;
try
{
_state.Play();
}
catch (GameOverException)
{
// game over detected
SetState(new GameOverState(this));
throw; // let the engine decide what to do
}
} while (_continue);
}
public void Send(IEnumerable<string> messages)
{
foreach (var message in messages)
{
switch (message)
{
case "reset":
Init();
break;
case "select level":
new SelectLevelCommand(Executor, StartLevel).Execute();
break;
case "highscore":
new HighscoreCommand(Executor, "THEBOT").Execute();
break;
case "menu":
new HeartModeCommand(Executor, IsHeartMode).Execute();
break;
case "start from game over":
new StartFromGameOverCommand(Executor).Execute();
break;
}
}
}
}
}