-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpiskvork.h
executable file
·306 lines (276 loc) · 9.53 KB
/
piskvork.h
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
/*
(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 "lang.h"
#include "taskbarprogress.h"
#define myAI "pbrain-pela"
#define fnHtmlTable "_table.html"
#define fnResult "_result.txt"
#define fnState "state.tur"
#define fnStateTmp "turstate.tmp"
#define fnMsg "messages.txt"
#define fnOpenings "openings.txt"
#define tmpOpenings "tmpOpening.txt"
#define Mplayer 127
#define NET_PLAYER 0
typedef int Tsymbol;
struct Tsquare;
typedef Tsquare *Psquare;
typedef char TfileName[256];
typedef char TdirName[224];
enum{ INFO_TIMEMOVE=1,INFO_TIMEGAME=2,INFO_MEMORY=4,INFO_RULE=8,
INFO_GAMETYPE=16,INFO_TIMELEFT=32,INFO_DIR=64, INFO_ALL=0xffff };
struct Tsquare
{
Tsymbol z; //0=nothing, 1=X, 2=O, 3=outside
Psquare nxt,pre;//linked list for undo, redo
int x,y; //coordinates <1..width, 1..height>
int time; //total thinking time
Psquare winLineStart;//0 or beginning of a winning line
int winLineDir; //direction offset of a winning line
int foul;
};
struct Txyp {
int x,y;
Psquare p;
};
struct ThreadItem
{
ThreadItem *nxt,*prv;
HANDLE h;
DWORD id;
ThreadItem(){ nxt=prv=this; }
void init(){ nxt=prv=this; }
~ThreadItem(){ prv->nxt= nxt; nxt->prv= prv; }
void append(ThreadItem *item);
int isEmpty(){ return nxt==this; }
};
struct Tplayer
{
int isComp; //0=human, 1=computer
int timeMove; //time for a move (ms)
int timeGame; //time for a game (s)
int score; //number of wins
int time; //total time (ms)
int timeInit; //time of initialization, -1 during initialization, 0 after the first move
int memory; //used memory (bytes)
int turPlayerId; //number of player in tournament
int Nmoves; //moves counter
HANDLE process; //process handle
DWORD processId; //process identifier
HANDLE pipeToAI; //pipe handle
HANDLE pipeFromAI;//pipe handle
HANDLE dbgThread; //debugger thread (only pipe protocol)
ThreadItem threads; //list of threads (only pipe protocol)
Txyp *lastBoard;
int DlastBoard;
bool mustSendBoard; //whole board must be send to AI
bool isTimeOut; //time out
bool mustSendInfo; //INFO timeout_turn must be send to AI
TfileName brain; //name of the EXE or ZIP file
TfileName brain2; //name of the EXE file
TdirName temp; //temporary folder
int level();
void getName(char *buf, int n);
char *getFilePart();
void rdBrain(FILE *f);
int timeLeft();
void checkTimeOut(int currentTime);
int move();
void getMem();
int createProcess(bool pipe, STARTUPINFO &si);
void stopAI();
void startPipeAI();
bool notRunning();
void sendCommand(char *text,...);
int sendInfo(int mask=INFO_ALL);
void sendInfoCmd(char *key, int value);
void sendInfoCmd(char *key, char *value);
int storeBoard();
int readCommand(char *buf, int n);
int brainChanged();
void wrMsg(char *s);
void wrDebugLog(char *s);
void openThreads();
FILE *infoDatFile;
};
struct TturPlayer
{
int wins,losses,timeouts,errors,wins1,winsE,losses1;
int time,memory,maxTurnTime;
int Nmoves,Ngames;
DWORD crc;
int points;
float ratio;
};
struct TturCell
{
short start,notStart,error;
int sum(){ return start+notStart+error; }
};
struct Tclient {
SOCKET socket;
HANDLE thread;
int player[2];
int repeatCount;
int gameCount;
int opening;
u_long IP;
void gameFinished();
};
struct PROCESS_MEMORY_COUNTERS {
DWORD cb,PageFaultCount,PeakWorkingSetSize,WorkingSetSize,
QuotaPeakPagedPoolUsage,QuotaPagedPoolUsage,
QuotaPeakNonPagedPoolUsage,QuotaNonPagedPoolUsage,
PagefileUsage,PeakPagefileUsage;
};
typedef BOOL (__stdcall *TmemInfo)(HANDLE,PROCESS_MEMORY_COUNTERS*,DWORD);
//-----------------------------------------------------------------
extern int player,moves,width,height,tolerance,maxMemory,turNplayers,logDebug,logMessage,suspendAI,debugAI,port,turCurRepeat,autoBegin,turRepeat,turRecord,turDelay,priority,terminatee,turGamesCounter,turTieRepeat,turTieCounter,startMoves,errDelay,turOnlyLosses,turFormat,turGamesTotal,turOpening,invert,turNet,turRule,mx,my,height2,lastTurnTime[2],hardTimeOut,humanTimeOut,opening,logMoves,moveStart,coordStart,sameTime,turMatchRepeat,turLogMsg,infoEval,saveLock,debugPipe,ruleFive,continuous,undoRequest,netGameVersion,ignoreErrors,openingRandomShift1,openingRandomShiftT,affinity, Nopening, includeUndoMoves, soundNotification,cmdlineGameOutFileFormat;
extern DWORD openingCRC;
extern bool paused,finished,disableScore,isWin9X,isClient,isServer,isNetGame,isListening,turTimerAvail,levelChanged,cmdLineGame,autoBeginForce,tmpPsq;
extern Psquare lastMove,board,boardb,boardk,hilited;
extern DWORD lastTick;
extern TturPlayer *turTable;
extern TturCell *turCells;
extern Tplayer players[2];
extern HANDLE thread,pipeThread,endGameEvent;
extern char *title;
extern TfileName fnstate,TahDat,PlochaDat,TimeOutsDat,MsgDat,InfoDat,cmdlineGameOutFile,cmdlineLogMsgOutFile,cmdlineLogPipeOutFile;
extern TdirName fntur,tempDir,AIfolder,dataDir;
extern char servername[128],turPlayerStr[50000],cmdGameEnd[512],cmdTurEnd[512];
extern CRITICAL_SECTION threadsLock,netLock,timerLock,infoLock;
extern SYSTEMTIME turDateTime;
extern HWND hWin,msgWnd,logDlg,resultDlg;
extern SOCKET sock,sock_l;
extern HMODULE psapi;
extern TmemInfo getMemInfo;
extern Tclient clients[Mplayer];
//-----------------------------------------------------------------
int vmsg(char *caption, char *text, int btn, va_list v);
int msg1(int btn, char *text, ...);
void wrLog(char *text, ...);
void vwrLog(char *text, va_list ap);
void writeini(bool saveToolbar=true);
char* parseCommandLine();
int close(FILE *f,char *fn);
void show(HWND wnd);
void paintSquare(Psquare p);
void printLevel();
void printLevel1();
void printScore();
void printMoves();
void printTime(int pl);
bool getWinLine(Psquare p, Psquare &p2);
void printWinLine(Psquare p);
void printFoul(Psquare p);
void invalidate();
void hiliteLast();
void cancelHilite();
Psquare Square(int x,int y);
void browseFolder(char *buf,HWND wnd,int item,char *txt);
void drawTitle();
void setPriority(HANDLE process);
unsigned rnd(unsigned n);
TturCell *getCell(int loser,int winner);
bool cmpExt(char *s,char *e);
void delDir(char *path,bool d);
int checkDir(HWND hDlg, int item);
DWORD WINAPI threadLoop(LPVOID);
DWORD WINAPI pipeThreadLoop(LPVOID);
void turGetBrain(int i, char *out, int n, bool noPath);
char *getTurDir(char *buf);
void getMsgFn(char *fn);
DWORD searchAI(const char *src, int n, char *dest, char **filePart);
void wrMsgNewGame(int p1,int p2);
void wrMessage(char *fmt,...);
void executeCmd(char *cmd);
int getNplayers();
//if filename is NULL, opening is read from filename define by fnOpenings, otherwise by the parameter
void initOpeningTab(char* filename = NULL);
void shutOpeningTab();
signed char* getOpening(int i);
//parametres viz declaration of initOpeningTab and newGame
void turStart(char* openingFileName = NULL);
void turRestart();
void turNext();
bool turNext(Tclient *client);
void turLocalNext();
void turResult();
void turGameFinished();
bool doMove(Psquare p);
bool doMove1(Psquare p, int action=0);
void init();
void resetScore();
//parameter forceAutoBegin force to use automatic opening even if global autoBegin is 0
void newGame(int pl, bool openingEnabled, bool forceAutoBegin = false);
void restartGame();
void resume();
void softPause();
void hardPause();
void killBrains();
int stopThinking();
void finishGame(int errCode);
void turAddTime();
void saveRec(int errCode, bool includeUndoMoves);
void saveRecTmp(int errCode);
void wrGameResult();
void sendInfoEval();
void boardChanged();
bool notSuspended();
bool redo();
bool undo();
void setLevel(int l,int h);
void switchPlayer(int h1, int h2);
void savePsq(char *fn, int format, int errCode, bool includeUndoMoves);
void openPsq(char *fn);
DWORD getTickCount();
int initWSA();
int startListen();
int startConnection();
int clientStart();
void clientEnd();
void killClient(int i);
void turAbort();
void stopListen();
void serverEnd();
int serverStart();
void clientFinished();
int calcCRC(const char *fn, DWORD &result);
void wrState();
int rdState();
int rd(char *buf, int len);
int rd1();
int rd2();
int rd4();
int wr(char *buf, int len);
int wr1(SOCKET s, int i);
int wr1(int i);
int netGameStart();
void netGameEnd();
void newNetGame(LPARAM lP);
void netGameMove(Psquare p);
void netGameMsg();
void netGameUndo();
void netGameNew();
//-----------------------------------------------------------------
#define nxtP(p,i) (p=(Psquare)(((char*)p)+(i*s)))
#define prvP(p,i) (p=(Psquare)(((char*)p)-(i*s)))
#define nxtS(p,s) ((Psquare)(((char*)p)+diroff[s]))
template <class T> inline void amin(T &x,int m){ if(x<m) x=m; }
template <class T> inline void amax(T &x,int m){ if(x>m) x=m; }
template <class T> inline void aminmax(T &x,int l,int h){
if(x<l) x=l;
if(x>h) x=h;
}