-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaser_chess.cpp
587 lines (521 loc) · 12.2 KB
/
laser_chess.cpp
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/********************************************************************
* Description: Implementation of Laser Chess Board Game "Khet"
* Author: David Vulakh
*
* Copyright (c) 2017 David Vulakh.
*
********************************************************************/
#include <windows.h>
#include <gdiplus.h>
#include <stdlib.h>
#include <string.h>
#include <objidl.h>
#include <Unknwn.h>
#include <tchar.h>
#include <chrono>
#include <thread>
#include <stack>
#include "logic.h"
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
/*** COLOURS ***/
const int CLICKED_FILL = 0x507000FF;
const int LASER_SHIELD = 0xA00050FF;
const int MOUSED_FILL = 0x30FFFFFF;
const int LASER_BLAST = 0xA0FF0000;
const int LASER_BEAM = 0xFF00FF00;
const int MOVE_FILL = 0x5000FF00;
const int TILE_FILL = 0xFF3E3F44;
const int BKGND = 0xFF202020;
const int SHADE = 0xA0202020;
/*** PROPORTIONS ***/
const double TILE_DIM = 0.05;
const double PADY = 0.025;
const double ARROW = 0.2;
const double PADX = 0.1;
const int ARRHW = 2;
int tile_dim;
int arrow;
int padx;
int pady;
/*** BOARD ***/
const string autos = "autosave.aksg";
bool ready = false;
bool turn;
board brd;
/*** WINDOW INFORMATION ***/
static TCHAR szWindowClass[] = _T("Khet");
static TCHAR szTitle[] = _T("Khet");
int width, height;
HINSTANCE hInst;
Graphics* g;
HWND hWnd;
/*** FUNCTIONS ***/
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
VOID paint_laser_flash(tile*, int);
VOID laser(int, int, int, int);
VOID paint_tile(int, int);
VOID paint_tile(tile*);
VOID OnPaint(HDC hdc);
VOID mouse_clicked();
VOID mouse_moved();
VOID paint_board();
VOID background();
BOOL inside(Rect);
VOID key_input();
VOID paint_rot();
Rect ccRect();
Rect cwRect();
Rect cRect();
/*** WINDOW SETUP ***/
//Start window
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow)
{
WNDCLASSEX wcex;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
///Initialize GDI+
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
///Setup
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
///Notify in case of failure
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
///Store instance handle
hInst = hInstance;
///Create board
brd = board(board::CLASSIC_MODE);
turn = false;
///Create the window
hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_BORDER,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 100,
NULL,
NULL,
hInstance,
NULL
);
SetWindowLong(hWnd, GWL_STYLE, 0);
///Notify in case of failure
if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
szTitle,
NULL);
return 1;
}
///Maximize window
ShowWindow(
hWnd,
SW_MAXIMIZE);
UpdateWindow(hWnd);
///Compute dimensions
RECT windr;
g = new Graphics(hWnd);
GetClientRect(hWnd, &windr);
width = windr.right - windr.left;
height = windr.bottom - windr.top;
tile_dim = width * TILE_DIM;
arrow = height * ARROW;
padx = width * PADX;
pady = width * PADY;
///Paint board
ready = true;
background();
paint_board();
///Mainloop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
key_input();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);
return (int)msg.wParam;
}
//Process messages
LRESULT CALLBACK WndProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_ERASEBKGND:
background();
case WM_PAINT:
OnPaint(BeginPaint(hWnd, &ps));
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONUP:
mouse_clicked();
break;
case WM_MOUSEMOVE:
mouse_moved();
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
/*** UI ***/
//String utilities
wstring to_wstring(string s)
{
wstring ws(s.begin(), s.end());
return ws;
}
string to_string(wchar_t *w)
{
wstring ws(w);
string s(ws.begin(), ws.end());
return s;
}
//Rectangles
///Get rectangle for a given tile
Rect tileRect(int r, int c) { return Rect(padx + c * tile_dim, pady + r * tile_dim, tile_dim, tile_dim); }
///Get rectangle for turn symbol
Rect cRect()
{
int y = (pady + board::NUM_ROW * tile_dim + height) / 2;
int x = padx + board::NUM_COL * tile_dim / 2;
return Rect(
x - height * ARROW / 2,
y - height * ARROW / 2,
height * ARROW,
height * ARROW
);
}
///Get cw arrow rectangle
Rect cwRect() { return Rect(cRect().X - height * ARROW / ARRHW, cRect().Y, height * ARROW / ARRHW, height * ARROW); }
///Get cc arrow rectangle
Rect ccRect() { return Rect(cRect().X + cRect().Width, cRect().Y, height * ARROW / ARRHW, height * ARROW); }
//Mouse input
///Get tile under mouse
tile* get_tile()
{
POINT ms;
GetCursorPos(&ms);
int c = (ms.x - padx) / tile_dim;
int r = (ms.y - pady) / tile_dim;
if (ms.x >= padx && ms.y >= pady && r >= 0 && r < board::NUM_ROW && c >= 0 && c < board::NUM_COL)
return brd.tiles[r][c];
return NULL;
}
///Return whether mouse over a rectangle
BOOL inside(Rect r)
{
POINT ms;
GetCursorPos(&ms);
return
ms.x > r.X &&
ms.x - r.X < r.Width &&
ms.y > r.Y &&
ms.y - r.Y < r.Height;
}
///Mouse moved response: highlight tile
VOID mouse_moved()
{
if (brd.mouse == get_tile())
return;
if (brd.mouse != NULL)
paint_tile(brd.mouse);
brd.mouse = get_tile();
if (brd.mouse != NULL) {
SolidBrush b(MOUSED_FILL);
g->FillRectangle(&b, tileRect(brd.mouse->r, brd.mouse->c));
}
}
///Mouse clicked
VOID mouse_clicked() try
{
///First click -- select a piece
if (brd.click == NULL) {
if (get_tile() != NULL && get_tile()->p != NULL && brd.silver_move == get_tile()->p->silver) {
///Show as selected
brd.click = get_tile();
brd.click->select = CLICKED_FILL;
paint_tile(brd.click);
///Display legal moves
get_moves(brd.move, brd.click);
for (tile* t : brd.move) {
t->select = MOVE_FILL;
paint_tile(t);
}
}
}
///Second click
else {
///Unselect
if (get_tile() != NULL && brd.click == get_tile()) {
///Show as unselected
brd.click->select = 0;
paint_tile(brd.click);
brd.click = NULL;
///Unselect moves
for (tile* t : brd.move) {
t->select = 0;
paint_tile(t);
}
brd.move.clear();
}
///Take legal move
else {
///Legal move
bool moved = false;
///Normal move
if (get_tile() != NULL && legal_move(brd.click, get_tile())) {
brd.click->p->step(get_tile());
paint_tile(get_tile());
moved = true;
}
///Rotations
else if (inside(cwRect())) {
brd.click->p->dir = (brd.click->p->dir + 1) % piece::DIR;
moved = true;
}
else if (inside(ccRect())) {
brd.click->p->dir = (piece::DIR + brd.click->p->dir - 1) % piece::DIR;
moved = true;
}
///Cleanup
if (moved) {
///Unselect moves
brd.click->select = 0;
paint_tile(brd.click);
brd.click = NULL;
for (tile* t : brd.move) {
t->select = 0;
paint_tile(t);
}
brd.move.clear();
///Fire laser
int lx, ly;
list<tile*> l;
brd.repaint->clear();
if (brd.silver_move) {
l = ((sphinx*)(brd.tiles[7][9]->p))->fire();
lx = tileRect(7, 9).X + tileRect(7, 9).Width / 2;
ly = tileRect(7, 9).Y + tileRect(7, 9).Height / 2;
}
else {
l = ((sphinx*)(brd.tiles[0][0]->p))->fire();
lx = tileRect(0, 0).X + tileRect(0, 0).Width / 2;
ly = tileRect(0, 0).Y + tileRect(0, 0).Height / 2;
}
brd.silver_move = !brd.silver_move;
tile* last = NULL;
for (tile* t : l) {
if (t != NULL) {
///Draw laser
int x, y;
x = tileRect(t->r, t->c).X + tileRect(t->r, t->c).Width / 2;
y = tileRect(t->r, t->c).Y + tileRect(t->r, t->c).Height / 2;
laser(lx, ly, x, y);
lx = x;
ly = y;
if (t->p != NULL && (typeid(*(t->p)) != typeid(pyramid)))
paint_tile(t);
if (last != NULL && last->p != NULL && (typeid(*(last->p)) != typeid(pyramid)))
paint_tile(last);
///Sleep
this_thread::sleep_for(chrono::milliseconds(250));
///Kill
if (t == last) {
if (t->p != NULL)
paint_laser_flash(t, LASER_BLAST);
///Pharaoh died
if (typeid(*(t->p)) == typeid(pharaoh))
if (t->p->silver)
MessageBox(NULL,
_T("Game Over: Red Wins"),
szTitle,
NULL);
else
MessageBox(NULL,
_T("Game Over: Silver Wins"),
szTitle,
NULL);
t->p = NULL;
paint_tile(t);
}
last = t;
}
///Shield
else
if (last != NULL && last->p != NULL)
paint_laser_flash(last, LASER_SHIELD);
}
///Cleanup laser
for (tile* t : *brd.repaint) {
if (t != NULL)
paint_tile(t);
}
///Save
brd.save(autos);
}
}
}
///Repaint turn symbol
paint_rot();
}
catch (...)
{
MessageBox(NULL, _T("An error has occurred -- restoring with autosave."), szTitle, NULL);
if (!brd.load(autos))
MessageBox(NULL, _T("Error while loading autosave file."), szTitle, NULL);
paint_board();
}
//Key input
VOID key_input()
{
///Load game
if (GetAsyncKeyState('O') && GetAsyncKeyState(VK_CONTROL)) {
OPENFILENAME ofn = { 0 };
wchar_t fn[MAX_PATH] = L"amonkhet_save";
ofn.lStructSize = sizeof(ofn);
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST;
ofn.hInstance = GetModuleHandle(0);
ofn.nMaxFile = MAX_PATH;
ofn.nFilterIndex = 1;
ofn.lpstrFile = fn;
ofn.lpstrDefExt = _T("aksg");
GetSaveFileName(&ofn);
brd.load(to_string(fn));
paint_board();
}
///Save game
if (GetAsyncKeyState('S') && GetAsyncKeyState(VK_CONTROL)) {
OPENFILENAME ofn = { 0 };
wchar_t fn[MAX_PATH] = L"amonkhet_save";
ofn.lStructSize = sizeof(ofn);
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST;
ofn.hInstance = GetModuleHandle(0);
ofn.nMaxFile = MAX_PATH;
ofn.nFilterIndex = 1;
ofn.lpstrFile = fn;
ofn.lpstrDefExt = _T("aksg");
GetSaveFileName(&ofn);
brd.save(to_string(fn));
}
}
/*** PAINTING ***/
//Paint rotation buttons
VOID paint_rot()
{
///Erase turn symbol
if (turn != brd.silver_move) {
SolidBrush b(BKGND);
g->FillRectangle(&b, cRect());
}
///Paint arrows
Image cc(L"cc_arrow.PNG");
Image cw(L"cw_arrow.PNG");
g->DrawImage(&cc, ccRect());
g->DrawImage(&cw, cwRect());
///Grey out arrows
if (brd.click == NULL) {
SolidBrush b(SHADE);
g->FillRectangle(&b, ccRect());
g->FillRectangle(&b, cwRect());
}
///Paint turn symbol
if (turn != brd.silver_move) {
Image i(brd.silver_move ? L"ankh.PNG" : L"horus.PNG");
g->DrawImage(&i, cRect());
}
turn = brd.silver_move;
}
//Paint bkgnd
VOID background()
{
SolidBrush b(BKGND);
if(g != NULL)
g->FillRectangle(&b, Rect(0, 0, width, height));
}
//Paint a tile
VOID paint_tile(tile* t)
{
SolidBrush b(TILE_FILL);
g->FillRectangle(&b, tileRect(t->r, t->c));
if (t->type == tile::ANKH) {
Image i(L"ankh.png");
g->DrawImage(&i, tileRect(t->r, t->c));
}
if (t->type == tile::HORUS) {
Image i(L"horus.png");
g->DrawImage(&i, tileRect(t->r, t->c));
}
if (t->p != NULL) {
Image i(to_wstring(t->p->image_name()).c_str());
g->DrawImage(&i, tileRect(t->r, t->c));
}
if (t->select) {
SolidBrush b(t->select);
g->FillRectangle(&b, tileRect(t->r, t->c));
}
Pen p(0xFF000000);
g->DrawRectangle(&p, tileRect(t->r, t->c));
}
VOID paint_tile(int r, int c) { paint_tile(brd.tiles[r][c]); }
//Paint a laser flash on a tile
VOID paint_laser_flash(tile* t, int col)
{
SolidBrush b(col);
g->FillRectangle(&b, tileRect(t->r, t->c));
this_thread::sleep_for(chrono::milliseconds(1000));
}
//Paint a laser beam between points
VOID laser(int x1, int y1, int x2, int y2)
{
SolidBrush b(LASER_BEAM);
Pen p(&b, tile_dim / 10);
g->DrawLine(&p, Point(x1, y1), Point(x2, y2));
}
//Paint the board
VOID paint_board()
{
turn = !brd.silver_move;
paint_rot();
for (int i = 0; i < board::NUM_ROW; i++)
for (int j = 0; j < board::NUM_COL; j++)
paint_tile(i, j);
}
VOID OnPaint(HDC hdc)
{
if (ready)
paint_board();
}