File: levels.c

package info (click to toggle)
phalanx 25-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,048 kB
  • sloc: ansic: 8,829; sh: 80; makefile: 79
file content (210 lines) | stat: -rw-r--r-- 4,628 bytes parent folder | download | duplicates (2)
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
#include "phalanx.h"

long T1, T2;

long Time = 600*100;
long Otim = 100;


/* return time in centiseconds */
long ptime(void)
{
	if( Flag.cpu ) return ((double)clock())*100/CLOCKS_PER_SEC;
	else
	{
#ifdef _WIN32
		return GetTickCount()/10;
#else
		struct timeval t;
		gettimeofday( &t, NULL );
		return t.tv_sec*100 + t.tv_usec/10000;
#endif
	}
}



void l_level( char * l )
{
	int moves, seconds, minutes, increment;

	while( *l == ' ' ) l++;
	moves = atoi(l);

	while( isdigit((int)*l) ) l++;
	while( *l == ' ' ) l++;

	if( *l == '\n' || *l == '\0' )
	{
		printfl("fixed time %i seconds\n", moves );
		Flag.level = fixedtime; Flag.centiseconds = moves*100;
		return;
	}

	minutes = atoi(l);

	while( isdigit((int)*l) ) l++;
	if( *l == ':' )
	{
		l++;
		seconds = atoi(l);
		while( isdigit((int)*l) ) l++;
	}
	else seconds = 0;

	while( isdigit((int)*l) ) l++;
	while( *l == ' ' ) l++;
	increment = atoi(l);

	if( moves==0 )
		printfl(
		 "level: all moves in %i:%02i, increment %i seconds\n",
		 minutes, seconds, increment );
	else
		printfl(
		 "level: %i moves in %i:%02i, increment %i seconds\n",
		 moves, minutes, seconds, increment );

	Flag.level = timecontrol;
	Flag.moves = moves;
	Flag.centiseconds = minutes*6000+seconds*100;
	Flag.increment = increment;
	Time = Flag.centiseconds;
}



void l_startsearch(void)
{
	int moves;
	T1 = ptime();

	/* Adjust the DrawScore to avoid long boring drawish endgames.
	 * The default DrawScore is -10; Phalanx tries to avoid draws.
	 * That's good, but I have observed too many games that ended
	 * with the 50 moves rule draw, where the engine played
	 * an KR vs KR endgame. The code below fixes that behaviour. */
	if(
		Counter>=80  /* Counter==80 is move #40 */
		&&
		(
			   ( DrawScore <  0 && G[Counter].mtrl <= 3*R_VALUE )
			|| ( DrawScore < 10 && G[Counter].mtrl <= R_VALUE )
		)
	  ) DrawScore++;

	switch( Flag.level )
	{
	case timecontrol:
		if( Flag.moves > 0 ) moves = 2*Flag.moves;
		else
		{
			moves = Counter +
			        (G[Counter].mtrl+G[Counter].xmtrl)/800;
			if( Counter < 120 )
				moves += 60 - Counter/4;
			else	moves += 30;
		}

		if( Flag.increment == 0 ) T2 = 0;
		else
		if( Time/Flag.increment >= 1600 )
			T2 = Flag.increment * 60;
		else
		if( Time/Flag.increment <= 400 )
			T2 = Flag.increment * 10;
		else
			T2 = Flag.increment * (Time/Flag.increment/8-20)/3;

		if( Flag.post && T2 && Flag.xboard<2 )
		printfl( "    -> increment adds %g s to soft time limit\n",
			((float)T2) / (float)100 );

		T2 += Time / ( moves - Counter%moves + 4 );

		if( Flag.ponder ) T2 += T2/8;

		if( Flag.increment==0 && Flag.level==timecontrol )
		{
		   if( Time<150/*0*/ ) T2 -= T2/2;
		   else if( Time<600/*0*/ ) T2 -= T2/4;

		   if(    Flag.moves==0  /* all moves TC */
		       && Time < Otim    /* less time than opponent */
		       && Time < 600*100 /* and less than 10 minutes */
		       && Flag.ponder != 2 )
		   {
			long Reduction;
			Reduction = T2 - T2*(Time+Time)/(Otim+Time);
			Reduction = Reduction*(60000-Time)/(60000);

/* printf("telluser Ponder=%i Time=%li Otim=%li reducing %li by %li\n", Flag.ponder, Time, Otim, T2, Reduction); */

			T2 -= Reduction;
		   }
		}

		if( Flag.post && Flag.xboard<2 )
		printfl( "    -> soft time limit %g s\n",
		   ((float)T2) / (float)100 );

		/*** Now, set up the hard limit ***/
		     if( Time < 150/*0*/) Flag.centiseconds = Time/(moves+1)*3;
		else if( Time < 300/*0*/) Flag.centiseconds = Time/(moves+1)*6;
		else if( Time < 600/*0*/) Flag.centiseconds = Time/(moves+1)*12;
		else                  Flag.centiseconds = Time/(moves+1)*18; 

		Flag.centiseconds += 400*Flag.increment;

		if( Flag.centiseconds > Time-Flag.increment*100 )
		{
			if( Time > 200*Flag.increment )
			      Flag.centiseconds = Time-100*Flag.increment;
			else  Flag.centiseconds = Time/2;
		}

		if( Flag.centiseconds < 25 ) Flag.centiseconds = 25;

		if( Flag.post && Flag.xboard<2 )
		printfl( "    -> hard time limit %g s\n",
			((float)Flag.centiseconds) / (float)100 );

	break;
	case averagetime:
		T2 = Flag.centiseconds/3;
	break;
	default: break;
	}

	if( T2 < 1 ) T2 = 1;
}



int l_iterate(void)
{
	if( Flag.level == fixeddepth )
	{
		return ( Depth < Flag.depth );
	}
	else
	if( Flag.level == averagetime || Flag.level == timecontrol )
	{
		long t = ptime();
		switch( EasyMove )
		{
			case 1:  return ( t <= T1 + T2/2 );
			case 2:  return ( t <= T1 + T2/4 );
			default:
				 if( Depth<400 ) /* stabilize low nps levels */ 
					return ( t <= T1 + T2*2/3 );
				 if( Turns==0 )
					return ( t <= T1 + T2 );
				 else
					return ( t <= T1 + T2*(14+Turns)/10 );
		}
	}
	else
	return 1;
}