-
Notifications
You must be signed in to change notification settings - Fork 6
/
PISKVORK.cpp
executable file
·3004 lines (2834 loc) · 77.9 KB
/
PISKVORK.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
(C) 2000-2015 Petr Lastovicka
(C) 2015 Tianyi Hao
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//-----------------------------------------------------------------
#include "hdr.h"
#pragma hdrstop
#include <shlobj.h>
#include <time.h>
#include "piskvork.h"
#pragma comment(lib,"comctl32.lib")
#pragma comment(lib,"version.lib")
#pragma comment(lib,"wsock32.lib")
#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"ole32.lib")
//-----------------------------------------------------------------
/*
USERC("Piskvork.rc");
USEUNIT("nettur.cpp");
USEUNIT("protocol.cpp");
USEUNIT("game.cpp");
USEUNIT("lang.cpp");
USEUNIT("netgame.cpp");
USEUNIT("taskbarprogress.cpp");
*/
//-----------------------------------------------------------------
//toolbar buttons
const int Ntool=18;
const TBBUTTON tbb[]={
{6, 101, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{0, 106, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{1, 107, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{0, 0, 0, TBSTYLE_SEP, {0}, 0},
{10, 104, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{12, 406, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{14, 219, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{13, 221, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{0, 0, 0, TBSTYLE_SEP, {0}, 0},
{7, 205, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{3, 210, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{5, 201, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{4, 202, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{2, 211, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{11, 208, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{0, 0, 0, TBSTYLE_SEP, {0}, 0},
{8, 301, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{9, 302, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{21, 303, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{18, 304, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{15, 204, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{22, 209, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{16, 108, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{20, 206, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{17, 223, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
{19, 222, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0},
};
char *toolNames[]={
"New game", "Open position", "Save position", "",
"Options", "Players settings", "Tournament", "Log window", "",
"Undo all", "Undo 2x", "Undo", "Redo", "Redo 2x",
"Redo all", "", "Open skin", "Next skin", "Previous skin", "Refresh",
"Reset score", "Continue", "Pause", "Swap players",
"Connect", "Disconnect",
};
char *priorTab[]={"idle", "below normal", "normal", "above normal", "high"};
Psquare
board=0,
boardb, boardk,//pointer to the beginning and end of board
lastMove; //pointer to last move
Tplayer players[2]; //information about players
TturPlayer *turTable=0; //result of a tournament
TturCell *turCells=0;
int
width=20, //number of squares horizontally (without borders)
height=20, //number of squares vertically (without borders)
height2, //height+2
player=1, //who has turn
moves, //moves counter
startMoves,//number of automatic opening moves
widthc, //width of the client area of the window
widthb, //right side of the board
heightb, //bottom side of the board
coordW, //width of coorninates at the left side
coordH, //height of coorninates at the top side
mtop, //toolBarH or 0
toolBarVisible=1,//1=show toolbar
maxMemory=80, //maximum memory for AI (MB)
tolerance=1000, //how longer can AI think after timeout
hardTimeOut=0, //should we check timeout for AI
humanTimeOut=0, //should we check timeout for human
ruleFive=0, //0= five or more stones win, 1=exactly 5 win
continuous=0, //0= until someone wins, 1=until board is full
priority=2, //AI process priority
autoBegin=0, //automatic openings
opening, //index of the current opening
turOpening, //the current tournament opening
turRule=0, //tournament mode
turRepeat=5, //tournament repeat count
turCurRepeat, //tournament repeat counter
turMatchRepeat=2,//number of games per one repeat cycle
turNplayers=0, //number of tournament players
turRecord=0, //save games
turOnlyLosses=0,//save only games which the first player lost
turFormat=1, //1=psq,2=rec
turNet=0, //0=local computer, 1=network
turTieRepeat=1, //maximal repeat count when it's a tie
turTieCounter, //current number of sequential ties
whoConnect=0, //0=tournament, 1=network game
logMessage=1, //write message commands to the log window
logDebug=0, //write debug commands to the log window
logMoves=1, //write moves to the log window
infoEval=0, //send INFO evaluate x,y when mouse moves
turLogMsg=1, //write messages to messages.txt
suspendAI=1, //1=suspend pbrains when it isn't their turn
debugAI=0, //debug pbrains (when suspendAI==1)
statusY, //status bar height
fontH, //character height
invert=0, //swap symbols
bmW, //size of a square in pixels
sameTime=0, //both players have the same timeGame and timeMove
coordVisible=0, //show coordinates
mx, my, //mouse coordinates
coordStart=0, //coordinates origin, 0 or 1
moveStart=0, //move number start, 0 or 1
moveDiv2=0, //move number divide by two
flipHoriz, flipVert, flipDiag, //board inversion
hiliteDelay=600,//last move hilite duration
clearLog=1, //clear log window at the beginning of a game
turDelay=1500, //pause between games in tournament
turGamesTotal, //total number of games in tournament
turGamesCounter,//currently finished games in tournament
gotoMove, //number in goto move dialog box
terminatee, //stopping AI
saveLock,
Naccel,
lastTime[2], //displayed game times (used to avoid flicker)
lastTurnTime[2],//displayed turn times
ignoreErrors=1, //don't show error messages from AI
openingRandomShift1=1, //randomly rotate and transpose openings; single game
openingRandomShiftT=0, //randomly rotate and transpose openings; tournament
affinity=-1, //(change to version 8.4 by Tomas Kubes). -1 means that no affinity is set by user
Nopening,
includeUndoMoves=0, //if undo moves are included to the file
soundNotification=1, //sound notification after computer move
cmdlineGameOutFileFormat=1; //same as turFormat, but for commandLineGame
DWORD openingCRC; //(change to version 8.5 by Tomas Kubes). CRC of opening file in the tournament.
const int toolBarH=28; //toolbar height
DWORD lastTick; //last move time (getTickCount())
char *title; //window caption
char turPlayerStr[50000]; //tournament players EXE file names
Psquare
hilited=0; //hilited square
bool
isWin9X, //1=Win95/98/ME, 0=WinNT/2K/XP
disableScore, //to avoid adding score more than once
levelChanged, //to not show timeout when time limit is decreased
turTimerAvail=false, //waiting between tournament games
cmdLineGame, //command line argument -p
paused=true, //game is paused
finished=true,//game has finished
delreg=false, //registry values were deleted
autoBeginForce=false, //automatic tournament opening even if autoBegin = 0
tmpPsq=false; //generate tmp_....psq file in net tournament on the client (from version 8.4 by Tomas Kubes)
TfileName fnrec, fnskin, fnbrain, fnstate, TahDat, PlochaDat, TimeOutsDat, MsgDat, InfoDat, cmdlineGameOutFile, cmdlineLogMsgOutFile, cmdlineLogPipeOutFile;
TdirName fntur, tempDir, dataDir;
Txywh mainPos={50, 15, 0, 0}, logPos={500, 40, 0, 0}, msgPos={500, -1, 0, 0}, resultPos;
HBITMAP bm;
HDC dc, bmpdc;
HWND hWin, logWnd, logDlg, resultDlg, msgWnd, msgDlg, toolbar;
HINSTANCE inst;
HACCEL haccel;
ACCEL accel[Maccel];
ACCEL dlgKey;
HANDLE thread, pipeThread;
HMODULE psapi;
COLORREF colors[4]; //background, text, winning line
CRITICAL_SECTION timerLock, drawLock;
SIZE szScore, szCoord, szMove, szName[2], szTimeGame[2], szTimeMove[2];
TmemInfo getMemInfo;
Win7TaskbarProgress win7TaskbarProgress;
OPENFILENAME psqOfn={
sizeof(OPENFILENAME), 0, 0, 0, 0, 0, 1,
fnrec, sizeof(fnrec),
0, 0, 0, 0, 0, 0, 0, "PSQ", 0, 0, 0
};
OPENFILENAME skinOfn={
sizeof(OPENFILENAME), 0, 0, 0, 0, 0, 1,
fnskin, sizeof(fnskin),
0, 0, 0, 0, 0, 0, 0, "BMP", 0, 0, 0
};
OPENFILENAME brainOfn={
sizeof(OPENFILENAME), 0, 0, 0, 0, 0, 1,
fnbrain, sizeof(turPlayerStr),
0, 0, 0, 0, 0, 0, 0, "EXE", 0, 0, 0
};
OPENFILENAME stateOfn={
sizeof(OPENFILENAME), 0, 0, 0, 0, 0, 1,
fnstate, sizeof(fnstate),
0, 0, 0, 0, 0, 0, 0, "TUR", 0, 0, 0
};
const char
*subkey="Software\\Petr Lastovicka\\piskvorky";
struct Treg { char *s; int *i; } regVal[]={
{"height", &height}, {"sirka", &width},
{"hiliteDelay", &hiliteDelay},
{"souradnice", &coordVisible},
{"coordinatesOrigin", &coordStart},
{"moveOrigin", &moveStart},
{"X", &mainPos.x}, {"Y", &mainPos.y},
{"hraci0", &players[0].isComp},
{"timeMove0", &players[0].timeMove},
{"timeMove1", &players[1].timeMove},
{"timeGame0", &players[0].timeGame},
{"timeGame1", &players[1].timeGame},
{"sameTime", &sameTime},
{"prohodit", &invert},
{"maxMemory", &maxMemory},
{"tolerance", &tolerance},
{"checkTimeOut", &hardTimeOut},
{"moveDiv2", &moveDiv2},
{"humanTimeOut", &humanTimeOut},
{"tournamentRule", &turRule},
{"tournamentRepeat", &turRepeat},
{"tournamentDelay", &turDelay},
{"tournamentRec", &turRecord},
{"tournamentRecFormat", &turFormat},
{"saveMsg", &turLogMsg},
{"onlyLosses", &turOnlyLosses},
{"flipHoriz", &flipHoriz},
{"flipVert", &flipVert},
{"flipDiag", &flipDiag},
{"saveFormat", (int*)&psqOfn.nFilterIndex},
{"logMessage", &logMessage},
{"logDebug", &logDebug},
{"logMoves", &logMoves},
{"Xlog", &logPos.x},
{"Ylog", &logPos.y},
{"Wlog", &logPos.w},
{"Hlog", &logPos.h},
{"Xresult", &resultPos.x},
{"Yresult", &resultPos.y},
{"Wresult", &resultPos.w},
{"Hresult", &resultPos.h},
{"Xmsg", &msgPos.x},
{"Ymsg", &msgPos.y},
{"Wmsg", &msgPos.w},
{"Hmsg", &msgPos.h},
{"debugAI", &suspendAI},
{"debugAI2", &debugAI},
{"autoBegin", &autoBegin},
{"opening", &opening},
{"toolbar", &toolBarVisible},
{"port", &port},
{"net", &turNet},
{"whoConnect", &whoConnect},
{"priority", &priority},
{"tieRepeat", &turTieRepeat},
{"matchRepeat", &turMatchRepeat},
{"infoEvaluate", &infoEval},
{"logPipe", &debugPipe},
{"ruleFive", &ruleFive},
{"continuous", &continuous},
{"clearLog", &clearLog},
{"ignoreErrors", &ignoreErrors},
{"openingRandomShift1", &openingRandomShift1},
{"openingRandomShiftT", &openingRandomShiftT},
{"includeUndoMoves", &includeUndoMoves},
{"soundNotification", &soundNotification},
};
struct Tregs { char *s; char *i; DWORD n; } regValS[]={
{"soubor", fnrec, sizeof(fnrec)},
{"skin", fnskin, sizeof(fnskin)},
{"language", lang, sizeof(lang)},
{"AI0", players[0].brain, sizeof(players[0].brain)},
{"AI1", players[1].brain, sizeof(players[1].brain)},
{"tournamentPlayers", turPlayerStr, sizeof(turPlayerStr)},
{"tournamentFolder", fntur, sizeof(fntur)},
{"servername", servername, sizeof(servername)},
{"AIfolder", AIfolder, sizeof(AIfolder)},
{"cmdGame", cmdGameEnd, sizeof(cmdGameEnd)},
{"cmdTur", cmdTurEnd, sizeof(cmdTurEnd)},
{"AIdataFolder", dataDir, sizeof(dataDir)},
};
//------------------------------------------------------------------
#define endA(a) (a+(sizeof(a)/sizeof(*a)))
void line(int x1, int y1, int x2, int y2)
{
MoveToEx(dc, x1, y1, NULL);
LineTo(dc, x2, y2);
}
void vprint(int x, int y, SIZE *sz, char *format, va_list va)
{
char buf[128];
RECT rc;
int n;
n = vsprintf(buf, format, va);
EnterCriticalSection(&drawLock);
SetTextAlign(dc, TA_CENTER);
if(sz){
rc.left= x-(sz->cx>>1);
rc.right= x+(sz->cx>>1);
rc.top= y;
rc.bottom= y+sz->cy;
if(RectVisible(dc, &rc) || !sz->cx){
GetTextExtentPoint32(dc, buf, n, sz);
}
}
ExtTextOut(dc, x, y, sz ? ETO_OPAQUE : 0, &rc, buf, n, 0);
GdiFlush();
LeaveCriticalSection(&drawLock);
}
void print(int x, SIZE *sz, char *format, ...)
{
va_list va;
va_start(va, format);
vprint(widthc*x/1000, heightb + (statusY/2-fontH)/2, sz, format, va);
va_end(va);
}
void print2(int x, SIZE *sz, char *format, ...)
{
va_list va;
va_start(va, format);
vprint(widthc*x/1000, heightb + (statusY*3/2-fontH)/2, sz, format, va);
va_end(va);
}
void printMoves()
{
print2(500, &szMove, lng(550, "Move %d"), moveStart + (moveDiv2 ? (moves-finished)/2 : moves));
}
void printScore()
{
print(500, &szScore, lng(553, "Score %d:%d"),
players[0].score, players[1].score);
}
void printMouseCoord()
{
if(mx>=0 && my>=0 && mx<width && my<height && coordVisible){
print2(650, &szCoord, " [%2d,%2d] ", mx+coordStart, my+coordStart);
}
else{
print2(650, &szCoord, "");
}
}
static int brainX[2]={220, 771};
void printLevel1()
{
for(int i=0; i<2; i++){
char buf[17];
players[i].getName(buf, sizeof(buf));
print(brainX[i], &szName[i], buf);
}
}
void checkMenus()
{
HMENU h=GetMenu(hWin);
CheckMenuRadioItem(h, 401, 403, 401 + players[0].isComp + players[1].isComp, MF_BYCOMMAND);
CheckMenuItem(h, 309, toolBarVisible ? MF_BYCOMMAND|MF_CHECKED : MF_BYCOMMAND|MF_UNCHECKED);
}
void printLevel()
{
printLevel1();
checkMenus();
}
int whoStarted()
{
return player^(moves&1);
}
int vmsg(char *caption, char *text, int btn, va_list v)
{
char buf[1024];
if(!text) return IDCANCEL;
_vsnprintf(buf, sizeof(buf), text, v);
buf[sizeof(buf)-1]=0;
return MessageBox(hWin, buf, caption, btn);
}
void msglng(int id, char *text, ...)
{
va_list ap;
va_start(ap, text);
vmsg(title, lng(id, text), MB_OK|MB_ICONERROR, ap);
va_end(ap);
}
int msg3(int btn, char *caption, char *text, ...)
{
va_list ap;
va_start(ap, text);
int result = vmsg(caption, text, btn, ap);
va_end(ap);
return result;
}
void msg2(char *caption, char *text, ...)
{
va_list ap;
va_start(ap, text);
vmsg(caption, text, MB_OK|MB_ICONERROR, ap);
va_end(ap);
}
int msg1(int btn, char *text, ...)
{
va_list ap;
va_start(ap, text);
int result = vmsg(title, text, btn, ap);
va_end(ap);
return result;
}
void msg(char *text, ...)
{
va_list ap;
va_start(ap, text);
vmsg(title, text, MB_OK|MB_ICONERROR, ap);
va_end(ap);
}
void vwrLog(char *text, va_list ap)
{
char *buf;
int len, n;
buf=0;
if(text){
for(len=64;; len*=2){
delete[] buf;
buf=new char[len+3];
n=_vsnprintf(buf, len, text, ap);
if(n>=0) break;
}
buf[n]='\r';
buf[n+1]='\n';
buf[n+2]=0;
}
PostMessage(hWin, WM_COMMAND, 998, (LPARAM)buf);
}
void wrLog(char *text, ...)
{
va_list ap;
va_start(ap, text);
vwrLog(text, ap);
va_end(ap);
}
int getRadioButton(HWND hWnd, int item1, int item2)
{
for(int i=item1; i<=item2; i++){
if(IsDlgButtonChecked(hWnd, i)){
return i-item1;
}
}
return 0;
}
void browseFolder(char *buf, HWND wnd, int item, char *txt)
{
BROWSEINFO bi;
bi.hwndOwner= wnd;
bi.pidlRoot=0;
bi.pszDisplayName= buf;
bi.lpszTitle= txt;
bi.ulFlags=BIF_RETURNONLYFSDIRS;
bi.lpfn=0;
bi.iImage=0;
ITEMIDLIST *iil;
iil=SHBrowseForFolder(&bi);
if(iil){
if(SHGetPathFromIDList(iil, buf)){
SetDlgItemText(wnd, item, buf);
}
IMalloc *ma;
SHGetMalloc(&ma);
ma->Free(iil);
ma->Release();
}
}
void delDir(char *path, bool d)
{
HANDLE h;
WIN32_FIND_DATA fd;
char oldDir[256];
GetCurrentDirectory(sizeof(oldDir), oldDir);
if(!SetCurrentDirectory(path)) return;
h = FindFirstFile("*", &fd);
if(h!=INVALID_HANDLE_VALUE){
do{
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
if(fd.cFileName[0]!='.' || (fd.cFileName[1] && fd.cFileName[1]!='.')){
delDir(fd.cFileName, true);
}
continue;
}
DeleteFile(fd.cFileName);
} while(FindNextFile(h, &fd));
FindClose(h);
}
SetCurrentDirectory(oldDir);
if(d) RemoveDirectory(path);
}
void cleanTemp()
{
HANDLE h, p;
DWORD i;
TfileName buf;
WIN32_FIND_DATA fd;
//delete all temporary folders
GetTempPath(sizeof(buf)-8, buf);
if(!SetCurrentDirectory(buf)) return;
h = FindFirstFile("psqp*", &fd);
if(h!=INVALID_HANDLE_VALUE){
do{
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
if(sscanf(fd.cFileName+4, "%d", &i)==1){
p=OpenProcess(STANDARD_RIGHTS_REQUIRED, FALSE, i);
if(p){
CloseHandle(p);
}
else{
delDir(fd.cFileName, true);
}
}
}
} while(FindNextFile(h, &fd));
FindClose(h);
}
}
int checkDir(HWND hDlg, int item)
{
TdirName dir, buf;
char *p;
GetDlgItemText(hDlg, item, buf, sizeof(buf));
if(!buf[0]) GetCurrentDirectory(sizeof(dir), dir);
else GetFullPathName(buf, sizeof(dir), dir, &p);
SetDlgItemText(hDlg, item, dir);
DWORD attr= GetFileAttributes(dir);
if(attr!=0xFFFFFFFF && (attr&FILE_ATTRIBUTE_DIRECTORY)) return 2;
if(CreateDirectory(dir, 0)) return 1;
msglng(816, "Cannot create folder %s", dir);
SetFocus(GetDlgItem(hDlg, item));
return 0;
}
int close(FILE *f, char *fn)
{
if(!fclose(f)) return 0;
if(msg1(MB_ICONEXCLAMATION|MB_RETRYCANCEL,
lng(734, "Write error, file %s"), fn)
==IDRETRY) return 2;
return 1;
}
int getDlgItemMs(HWND hDlg, int item)
{
char buf[32], *s, c1, c2;
GetDlgItemText(hDlg, item, buf, sizeof(buf));
c1=',', c2='.';
if(strtod("1,5", 0)>1.4) c1='.', c2=',';
for(s=buf; *s; s++){
if(*s==c1) *s=c2;
}
return int(strtod(buf, 0)*1000);
}
void setDlgItemMs(HWND hDlg, int item, int ms)
{
char buf[32];
sprintf(buf, "%g", double(ms)/1000.0);
SetDlgItemText(hDlg, item, buf);
}
enum { R_left=1, R_right=2, R_top=4, R_bottom=8 };
struct Tresize {
int id;
int flags;
};
void resize(Txywh &pos, const Tresize *data, int len, HWND hWnd, UINT mesg, LPARAM lP)
{
int i, dw, dh;
RECT rc;
switch(mesg){
case WM_INITDIALOG:
SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(inst, MAKEINTRESOURCE(1)));
GetClientRect(hWnd, &rc);
pos.oldW= rc.right-rc.left;
pos.oldH= rc.bottom-rc.top;
aminmax(pos.x, -20, GetSystemMetrics(SM_CXSCREEN)-100);
aminmax(pos.y, -5, GetSystemMetrics(SM_CYSCREEN)-80);
break;
case WM_MOVE:
case WM_EXITSIZEMOVE:
if(!IsZoomed(hWnd) && !IsIconic(hWnd)){
GetWindowRect(hWnd, &rc);
pos.x= rc.left;
pos.y= rc.top;
pos.w= rc.right-rc.left;
pos.h= rc.bottom-rc.top;
}
break;
case WM_SIZE:
if(!lP) break;
if(pos.oldW){
HDWP p=BeginDeferWindowPos(len);
for(i=0; i<len; i++){
const Tresize *d= data+i;
HWND w= GetDlgItem(hWnd, d->id);
GetWindowRect(w, &rc);
MapWindowPoints(0, hWnd, (POINT*)&rc, 2);
dw=LOWORD(lP)-pos.oldW;
dh=HIWORD(lP)-pos.oldH;
if(d->flags&R_left) rc.left+=dw;
if(d->flags&R_right) rc.right+=dw;
if(d->flags&R_top) rc.top+=dh;
if(d->flags&R_bottom) rc.bottom+=dh;
DeferWindowPos(p, w, 0, rc.left, rc.top,
rc.right-rc.left, rc.bottom-rc.top, SWP_NOZORDER);
}
EndDeferWindowPos(p);
}
pos.oldW=LOWORD(lP);
pos.oldH=HIWORD(lP);
break;
case WM_GETMINMAXINFO:
((MINMAXINFO FAR*) lP)->ptMinTrackSize.x = 170;
((MINMAXINFO FAR*) lP)->ptMinTrackSize.y = 150;
break;
}
}
//-----------------------------------------------------------------
Psquare Square(int x, int y)
{
return boardb + x*height2+(y+1);
}
int X(Psquare p)
{
int x;
if(flipDiag){
x=p->y;
if(flipHoriz) x=height+1-x;
}
else{
x=p->x;
if(flipHoriz) x=width+1-x;
}
x= (x-1)*bmW;
if(coordVisible) x+=coordW;
return x;
}
int Y(Psquare p)
{
int y;
if(flipDiag){
y=p->x;
if(flipVert) y=width+1-y;
}
else{
y=p->y;
if(flipVert) y=height+1-y;
}
y= (y-1)*bmW;
if(coordVisible) y+=coordH;
if(toolBarVisible) y+=toolBarH;
return y;
}
void getMousePos(WPARAM lP, int &x0, int &y0)
{
int w, x, y;
x=LOWORD(lP);
y=HIWORD(lP);
if(coordVisible) x-=coordW, y-=coordH;
if(toolBarVisible) y-=toolBarH;
x=x/bmW; y=y/bmW;
if(flipHoriz) x=(flipDiag ? height : width)-1-x;
if(flipVert) y=(flipDiag ? width : height)-1-y;
if(flipDiag) w=x, x=y, y=w;
x0=x; y0=y;
}
bool onAIName(int i, LPARAM lP)
{
return HIWORD(lP)>heightb &&
abs(LOWORD(lP)-widthc*brainX[i]/1000)<30;
}
//draw square at position p
//z: 0=empty, 1=first player, 2=second player, 3=outside
void paintSquare(int z, Psquare p)
{
if(z>2) return;
if(z){
if(invert) z^=3;
if(p==hilited) z+=2;
}
BitBlt(dc, X(p), Y(p), bmW, bmW, bmpdc, z*bmW, 0, SRCCOPY);
}
void paintSquare(Psquare p)
{
paintSquare(p->z, p);
}
//-----------------------------------------------------------------
void cancelHilite()
{
Psquare p=hilited;
if(p){
hilited=0;
paintSquare(p);
KillTimer(hWin, 10);
}
}
//hilite last move
void hiliteLast()
{
cancelHilite();
if(moves && hiliteDelay && !lastMove->winLineDir && !lastMove->foul){
SetTimer(hWin, 10, hiliteDelay, NULL);
paintSquare(hilited=lastMove);
}
}
void showLast()
{
int d=hiliteDelay;
if(d<100) hiliteDelay=700;
hiliteLast();
hiliteDelay=d;
}
//-----------------------------------------------------------------
DWORD getTickCount()
{
static LARGE_INTEGER freq;
if(!freq.QuadPart){
QueryPerformanceFrequency(&freq);
if(!freq.QuadPart) return GetTickCount();
}
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
return (DWORD)(c.QuadPart*1000/freq.QuadPart);
}
void printTime(int pl)
{
int t, c, y, x0, x;
HGDIOBJ oldP;
HPEN pen0, pen1;
int W;
static const int X1[2]={60, 940}, X2[2]={45, 955};
EnterCriticalSection(&drawLock);
//prepare pens
pen0= CreatePen(PS_SOLID, 0, colors[0]);
pen1= CreatePen(PS_SOLID, 0, colors[1]);
oldP= GetCurrentObject(dc, OBJ_PEN);
W=widthc/9;
x0=0;
if(pl) x0=widthc-W;
t=players[pl].time;
//turn time
if(pl==player && !paused && (moves>0 || players[pl].isComp)){
c=getTickCount()-lastTick;
t+=c;
lastTurnTime[pl]=c;
players[pl].checkTimeOut(c);
}
print2(X1[pl], &szTimeMove[pl], "%.1f",
double(lastTurnTime[pl])/1000.0);
if(players[pl].timeMove>400 && !isNetGame){
//progress
y= heightb+(statusY*3/2+fontH)/2+1;
x= W-W*lastTurnTime[pl]/players[pl].timeMove;
if(x>0){
amax(x, W);
SelectObject(dc, pen1);
line(x0, y, x0+x, y);
}
else{
x=0;
}
SelectObject(dc, pen0);
if(x<W) line(x0+x, y, x0+W, y);
}
//game time
t/=1000;
if(t!=lastTime[pl]){
lastTime[pl]=t;
print(X2[pl], &szTimeGame[pl], "%d:%02d", t/60, t%60);
if(players[pl].timeGame && !isNetGame){
//progress
y= heightb+(statusY/2+fontH)/2+1;
x= W-W*t/players[pl].timeGame;
if(x>0){
amax(x, W);
SelectObject(dc, pen1);
line(x0, y, x0+x, y);
}
else{
x=0;
}
SelectObject(dc, pen0);
if(x<W) line(x0+x, y, x0+W, y);
}
}
//delete pens
SelectObject(dc, oldP);
DeleteObject(pen1);
DeleteObject(pen0);
GdiFlush();
LeaveCriticalSection(&drawLock);
}
bool getWinLine(Psquare p, Psquare &p2)
{
int s;
Psquare p1;
s=p->winLineDir;
if(!s) return false;
p1=p->winLineStart;
do{
nxtP(p, 1);
} while(p->winLineStart==p1);
prvP(p, 1);
p2=p;
return true;
}
void printWinLine(Psquare p)
{
Psquare p2;
RECT rc;
LONG w;
if(!getWinLine(p, p2)) return;
rc.left=X(p->winLineStart);
rc.top=Y(p->winLineStart);
rc.right=X(p2);
rc.bottom=Y(p2);
if(rc.left>rc.right) w=rc.left, rc.left=rc.right, rc.right=w;
if(rc.top>rc.bottom) w=rc.top, rc.top=rc.bottom, rc.bottom=w;
rc.right+=bmW;
rc.bottom+=bmW;
InvalidateRect(hWin, &rc, FALSE);
}
void printFoul(Psquare p)
{
RECT rc;
if(!p->foul) return;
rc.left=X(p);
rc.top=Y(p);
rc.right=rc.left+bmW;
rc.bottom=rc.top+bmW;
InvalidateRect(hWin, &rc, FALSE);
}
void status()
{
printMoves();
printScore();
printLevel();
printTime(0);
printTime(1);
printMouseCoord();
BitBlt(dc, widthc*brainX[0]/1000-bmW/2, heightb+statusY/2, bmW-1, bmW-1,
bmpdc, (invert+1)*bmW+1, 1, SRCCOPY);
BitBlt(dc, widthc*brainX[1]/1000-bmW/2, heightb+statusY/2, bmW-1, bmW-1,
bmpdc, (2-invert)*bmW+1, 1, SRCCOPY);
if(!turNplayers) print2(brainX[whoStarted()]+45, 0, "*");
}
//paint entire window
void repaint(RECT *clip)
{
HGDIOBJ penOld;
HBRUSH brush;
Psquare p, p2=0;
RECT rc;
int i, d, j;
char buf[4];
brush= CreateSolidBrush(colors[0]);
if(coordVisible){
//left
rc.left=0;
rc.right=coordW;
rc.top=mtop;
rc.bottom= heightb;
FillRect(dc, &rc, brush);
//top
rc.left= coordW;
rc.right=widthb;
rc.bottom= rc.top+coordH;
FillRect(dc, &rc, brush);
}
//right border
rc.left= widthb;
rc.right=widthc;
if(rc.left<rc.right){
rc.top=mtop;
rc.bottom= heightb;
FillRect(dc, &rc, brush);
}
//status bar
if(clip->bottom>heightb){
rc.top=heightb;
rc.bottom=rc.top+statusY;
rc.left=0;
FillRect(dc, &rc, brush);
lastTime[0]=lastTime[1]=-1;
status();
}
DeleteObject(brush);
//coordinates
if(coordVisible){
//top
if(clip->top < mtop+coordH){
SetTextAlign(dc, TA_CENTER|TA_TOP);
d= (flipDiag ? height : width)-1;
for(i=d; i>=0; i--){
j=i;
if(flipHoriz) j=d-j;
_itoa(j+coordStart, buf, 10);
TextOut(dc, i*bmW+(bmW/2)+coordW, mtop, buf, (int)strlen(buf));
}
}
//left
if(clip->left < coordW){
SetTextAlign(dc, TA_RIGHT|TA_BASELINE);
d= (flipDiag ? width : height)-1;
for(i=d; i>=0; i--){
j=i;