initmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* This file contains the map-initialization routines for CHESS.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. This file is part of CHESS.
  4. CHESS is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the CHESS General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. CHESS, but only under the conditions described in the
  12. CHESS General Public License. A copy of this license is
  13. supposed to have been given to you along with CHESS so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. #include <stdio.h>
  18. #include "algdefs.h"
  19. #include "crucialdefs.h"
  20. #include "fixeddefs.h"
  21. #include "externmap.h"
  22. #include "bitmacs.h"
  23. extern FILE *hisf;
  24. extern tomove,ngamemvs;
  25. char *sqchar[BPERW*WPERM] = {
  26. "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
  27. "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
  28. "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
  29. "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
  30. "a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
  31. "a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
  32. "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
  33. "a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8"};
  34. MAP sqbits[BPERW*WPERM] = {
  35. a1, b1, c1, d1, e1, f1, g1, h1,
  36. a2, b2, c2, d2, e2, f2, g2, h2,
  37. a3, b3, c3, d3, e3, f3, g3, h3,
  38. a4, b4, c4, d4, e4, f4, g4, h4,
  39. a5, b5, c5, d5, e5, f5, g5, h5,
  40. a6, b6, c6, d6, e6, f6, g6, h6,
  41. a7, b7, c7, d7, e7, f7, g7, h7,
  42. a8, b8, c8, d8, e8, f8, g8, h8};
  43. init_diag()
  44. {
  45. diagleft[0][LOBD] = a1;
  46. diagleft[0][HIBD] = 0;
  47. diagleft[1][LOBD] = b1|a2;
  48. diagleft[1][HIBD] = 0;
  49. diagleft[2][LOBD] = c1|b2|a3;
  50. diagleft[2][HIBD] = 0;
  51. diagleft[3][LOBD] = d1|c2|b3|a4;
  52. diagleft[3][HIBD] = 0;
  53. diagleft[4][LOBD] = e1|d2|c3|b4;
  54. diagleft[4][HIBD] = a5;
  55. diagleft[5][LOBD] = f1|e2|d3|c4;
  56. diagleft[5][HIBD] = b5|a6;
  57. diagleft[6][LOBD] = g1|f2|e3|d4;
  58. diagleft[6][HIBD] = c5|b6|a7;
  59. diagleft[7][LOBD] = h1|g2|f3|e4;
  60. diagleft[7][HIBD] = d5|c6|b7|a8;
  61. diagleft[8][LOBD] = h2|g3|f4;
  62. diagleft[8][HIBD] = e5|d6|c7|b8;
  63. diagleft[9][LOBD] = h3|g4;
  64. diagleft[9][HIBD] = f5|e6|d7|c8;
  65. diagleft[10][LOBD] = h4;
  66. diagleft[10][HIBD] = g5|f6|e7|d8;
  67. diagleft[11][LOBD] = 0;
  68. diagleft[11][HIBD] = h5|g6|f7|e8;
  69. diagleft[12][LOBD] = 0;
  70. diagleft[12][HIBD] = h6|g7|f8;
  71. diagleft[13][LOBD] = 0;
  72. diagleft[13][HIBD] = h7|g8;
  73. diagleft[14][LOBD] = 0;
  74. diagleft[14][HIBD] = h8;
  75. diagright[7][LOBD] = a1|b2|c3|d4;
  76. diagright[7][HIBD] = e5|f6|g7|h8;
  77. diagright[8][LOBD] = b1|c2|d3|e4;
  78. diagright[8][HIBD] = f5|g6|h7;
  79. diagright[9][LOBD] = c1|d2|e3|f4;
  80. diagright[9][HIBD] = g5|h6;
  81. diagright[10][LOBD] = d1|e2|f3|g4;
  82. diagright[10][HIBD] = h5;
  83. diagright[11][LOBD] = e1|f2|g3|h4;
  84. diagright[11][HIBD] = 0;
  85. diagright[12][LOBD] = f1|g2|h3;
  86. diagright[12][HIBD] = 0;
  87. diagright[13][LOBD] = g1|h2;
  88. diagright[13][HIBD] = 0;
  89. diagright[14][LOBD] = h1;
  90. diagright[14][HIBD] = 0;
  91. diagright[6][LOBD] = a2|b3|c4;
  92. diagright[6][HIBD] = d5|e6|f7|g8;
  93. diagright[5][LOBD] = a3|b4;
  94. diagright[5][HIBD] = c5|d6|e7|f8;
  95. diagright[4][LOBD] = a4;
  96. diagright[4][HIBD] = b5|c6|d7|e8;
  97. diagright[3][LOBD] = 0;
  98. diagright[3][HIBD] = a5|b6|c7|d8;
  99. diagright[2][LOBD] = 0;
  100. diagright[2][HIBD] = a6|b7|c8;
  101. diagright[1][LOBD] = 0;
  102. diagright[1][HIBD] = a7|b8;
  103. diagright[0][LOBD] = 0;
  104. diagright[0][HIBD] = a8;
  105. }
  106. init_rows()
  107. {
  108. rows[0][0] = a1|b1|c1|d1|e1|f1|g1|h1;
  109. rows[0][1] = 0;
  110. rows[1][0] = a2|b2|c2|d2|e2|f2|g2|h2;
  111. rows[1][1] = 0;
  112. rows[2][0] = a3|b3|c3|d3|e3|f3|g3|h3;
  113. rows[2][1] = 0;
  114. rows[3][0] = a4|b4|c4|d4|e4|f4|g4|h4;
  115. rows[3][1] = 0;
  116. rows[4][0] = 0;
  117. rows[4][1] = a5|b5|c5|d5|e5|f5|g5|h5;
  118. rows[5][0] = 0;
  119. rows[5][1] = a6|b6|c6|d6|e6|f6|g6|h6;
  120. rows[6][0] = 0;
  121. rows[6][1] = a7|b7|c7|d7|e7|f7|g7|h7;
  122. rows[7][0] = 0;
  123. rows[7][1] = a8|b8|c8|d8|e8|f8|g8|h8;
  124. }
  125. init_cols()
  126. {
  127. cols[0][0] = a1|a2|a3|a4;
  128. cols[0][1] = a5|a6|a7|a8;
  129. cols[1][0] = b1|b2|b3|b4;
  130. cols[1][1] = b5|b6|b7|b8;
  131. cols[2][0] = c1|c2|c3|c4;
  132. cols[2][1] = c5|c6|c7|c8;
  133. cols[3][0] = d1|d2|d3|d4;
  134. cols[3][1] = d5|d6|d7|d8;
  135. cols[4][0] = e1|e2|e3|e4;
  136. cols[4][1] = e5|e6|e7|e8;
  137. cols[5][0] = f1|f2|f3|f4;
  138. cols[5][1] = f5|f6|f7|f8;
  139. cols[6][0] = g1|g2|g3|g4;
  140. cols[6][1] = g5|g6|g7|g8;
  141. cols[7][0] = h1|h2|h3|h4;
  142. cols[7][1] = h5|h6|h7|h8;
  143. }
  144. init_maps()
  145. {
  146. register sq,pc,col;
  147. for (pc = 0; pc < NPIECES; pc++)
  148. for (col = 0; col < NCOLORS; col++)
  149. BIT_ZERO(pieces[col][pc]);
  150. for (pc = 0; pc < NPIECES; pc++)
  151. for (col = 0; col < NCOLORS; col++)
  152. BIT_ZERO(pieces[col][pc]);
  153. for (sq = A1; sq <= H8; sq++)
  154. for (col = 0; col < NCOLORS; col++)
  155. {
  156. BIT_ZERO(attackfr[col][sq]);
  157. BIT_ZERO(attackto[col][sq]);
  158. BIT_ZERO(pmoveto[col][sq]);
  159. BIT_ZERO(pmovefr[col][sq]);
  160. }
  161. pieces[WHITE][KING][LOBD] = e1;
  162. pieces[WHITE][KING][HIBD] = 0;
  163. pieces[BLACK][KING][HIBD] = e8;
  164. pieces[BLACK][KING][LOBD] = 0;
  165. pieces[WHITE][QUEEN][LOBD] = d1;
  166. pieces[WHITE][QUEEN][HIBD] = 0;
  167. pieces[BLACK][QUEEN][HIBD] = d8;
  168. pieces[BLACK][QUEEN][LOBD] = 0;
  169. pieces[WHITE][ROOK][LOBD] = a1|h1;
  170. pieces[WHITE][ROOK][HIBD] = 0;
  171. pieces[BLACK][ROOK][HIBD] = a8|h8;
  172. pieces[BLACK][ROOK][LOBD] = 0;
  173. pieces[WHITE][BISHOP][LOBD] = c1|f1;
  174. pieces[WHITE][BISHOP][HIBD] = 0;
  175. pieces[BLACK][BISHOP][HIBD] = c8|f8;
  176. pieces[BLACK][BISHOP][LOBD] = 0;
  177. pieces[WHITE][KNIGHT][LOBD] = b1|g1;
  178. pieces[WHITE][KNIGHT][HIBD] = 0;
  179. pieces[BLACK][KNIGHT][HIBD] = b8|g8;
  180. pieces[BLACK][KNIGHT][LOBD] = 0;
  181. pieces[WHITE][PAWN][LOBD] = a2|b2|c2|d2|e2|f2|g2|h2;
  182. pieces[WHITE][PAWN][HIBD] = 0;
  183. pieces[BLACK][PAWN][HIBD] = a7|b7|c7|d7|e7|f7|g7|h7;
  184. pieces[BLACK][PAWN][LOBD] = 0;
  185. side[WHITE][LOBD] = a1|b1|c1|d1|e1|f1|g1|h1|a2|b2|c2|d2|e2|f2|g2|h2;
  186. side[WHITE][HIBD] = 0;
  187. side[BLACK][HIBD] = a7|b7|c7|d7|e7|f7|g7|h7|a8|b8|c8|d8|e8|f8|g8|h8;
  188. side[BLACK][LOBD] = 0;
  189. empty[LOBD] = a3|b3|c3|d3|e3|f3|g3|h3|a4|b4|c4|d4|e4|f4|g4|h4;
  190. empty[HIBD] = a5|b5|c5|d5|e5|f5|g5|h5|a6|b6|c6|d6|e6|f6|g6|h6;
  191. edge[LOBD] = a1|b1|c1|d1|e1|f1|g1|h1|a2|h2|a3|h3|a4|h4;
  192. edge[HIBD] = a5|h5|a6|h6|a7|h7|a8|b8|c8|d8|e8|f8|g8|h8;
  193. sanctuary[LOBD] = a1|a2|b1|b2|h1|h2|g1|g2;
  194. sanctuary[HIBD] = a7|a8|b7|b8|g7|g8|h7|h8;
  195. }
  196. #define Put_Kn_map(sq,low,hi) knightmvmaps[sq][LOBD] = low;\
  197. knightmvmaps[sq][HIBD] = hi;
  198. init_knight()
  199. {
  200. Put_Kn_map(A1,b3|c2,0);
  201. Put_Kn_map(B1,a3|c3|d2,0);
  202. Put_Kn_map(C1,a2|b3|d3|e2,0);
  203. Put_Kn_map(D1,b2|c3|e3|f2,0);
  204. Put_Kn_map(E1,c2|d3|f3|g2,0);
  205. Put_Kn_map(F1,d2|e3|g3|h2,0);
  206. Put_Kn_map(G1,e2|f3|h3,0);
  207. Put_Kn_map(H1,f2|g3,0);
  208. Put_Kn_map(A2,b4|c3|c1,0);
  209. Put_Kn_map(B2,a4|c4|d3|d1,0);
  210. Put_Kn_map(C2,a1|a3|b4|d4|e3|e1,0);
  211. Put_Kn_map(D2,b1|b3|c4|e4|f3|f1,0);
  212. Put_Kn_map(E2,c1|c3|d4|f4|g3|g1,0);
  213. Put_Kn_map(F2,d1|d3|e4|g4|h3|h1,0);
  214. Put_Kn_map(G2,e1|e3|f4|h4,0);
  215. Put_Kn_map(H2,f1|f3|g4,0);
  216. Put_Kn_map(A3,c4|c2|b1,b5);
  217. Put_Kn_map(B3,d4|d2|a1|c1,a5|c5);
  218. Put_Kn_map(C3,a4|a2|b1|d1|e2|e4,b5|d5);
  219. Put_Kn_map(D3,b4|f4|b2|f2|c1|e1,c5|e5);
  220. Put_Kn_map(E3,c2|c4|g4|g2|d1|f1,d5|f5);
  221. Put_Kn_map(F3,d2|d4|h2|h4|e1|g1,e5|g5);
  222. Put_Kn_map(G3,e4|e2|f1|h1,f5|h5);
  223. Put_Kn_map(H3,f4|f2|g1,g5);
  224. Put_Kn_map(A4,b2|c3,b6|c5);
  225. Put_Kn_map(B4,a2|c2|d3,d5|c6|a6);
  226. Put_Kn_map(C4,a3|b2|d2|e3,a5|b6|d6|e5);
  227. Put_Kn_map(D4,b3|c2|e2|f3,b5|c6|e6|f5);
  228. Put_Kn_map(E4,c3|d2|f2|g3,c5|d6|f6|g5);
  229. Put_Kn_map(F4,d3|e2|g2|h3,d5|e6|g6|h5);
  230. Put_Kn_map(G4,e3|f2|h2,e5|f6|h6);
  231. Put_Kn_map(H4,f3|g2,g6|f5);
  232. Put_Kn_map(A5,b3|c4,b7|c6);
  233. Put_Kn_map(B5,a3|c3|d4,a7|c7|d6);
  234. Put_Kn_map(C5,a4|b3|d3|e4,a6|b7|d7|e6);
  235. Put_Kn_map(D5,b4|c3|e3|f4,b6|c7|e7|f6);
  236. Put_Kn_map(E5,c4|d3|f3|g4,c6|d7|f7|g6);
  237. Put_Kn_map(F5,d4|e3|g3|h4,d6|e7|g7|h6);
  238. Put_Kn_map(G5,e4|f3|h3,e6|f7|h7);
  239. Put_Kn_map(H5,f4|g3,f6|g7);
  240. Put_Kn_map(A6,b4,c5|c7|b8);
  241. Put_Kn_map(B6,a4|c4,d5|d7|a8|c8);
  242. Put_Kn_map(C6,b4|d4,a5|e5|a7|e7|b8|d8);
  243. Put_Kn_map(D6,c4|d4,b5|f5|b7|f7|c8|e8);
  244. Put_Kn_map(E6,d4|f4,c5|c7|g5|g7|d8|f8);
  245. Put_Kn_map(F6,e4|g4,d5|d7|h5|h7|e8|g8);
  246. Put_Kn_map(G6,f4|h4,e5|e7|f8|h8);
  247. Put_Kn_map(H6,g4,f5|f7|g8);
  248. Put_Kn_map(A7,0,c8|c6|b5);
  249. Put_Kn_map(B7,0,a5|c5|d6|d8);
  250. Put_Kn_map(C7,0,b5|d5|e6|e8|a6|a8);
  251. Put_Kn_map(D7,0,c5|e5|b6|f6|b8|f8);
  252. Put_Kn_map(E7,0,c6|c8|g6|g8|d5|f5);
  253. Put_Kn_map(F7,0,e5|g5|d6|d8|h6|h8);
  254. Put_Kn_map(G7,0,f5|h5|e6|e8);
  255. Put_Kn_map(H7,0,g5|f6|f8);
  256. Put_Kn_map(A8,0,b6|c7);
  257. Put_Kn_map(B8,0,a6|c6|d7);
  258. Put_Kn_map(C8,0,a7|b6|d6|e7);
  259. Put_Kn_map(D8,0,b7|c6|e6|f7);
  260. Put_Kn_map(E8,0,c7|d6|f6|g7);
  261. Put_Kn_map(F8,0,d7|e6|g6|h7);
  262. Put_Kn_map(G8,0,e7|f6|h6);
  263. Put_Kn_map(H8,0,f7|g6);
  264. }
  265. init()
  266. {
  267. if (hisf != NULL) /* Open game file? Close it. */
  268. fclose(hisf);
  269. hisf = NULL; /* Start fresh game file. */
  270. tomove = WHITE; /* White moves first */
  271. ngamemvs = -1; /* Moves so far in game */
  272. zero_game(); /* Start fresh with game log */
  273. init_diag(); /* Initiate diagonals */
  274. init_rows(); /* And rows */
  275. init_cols(); /* And columns */
  276. init_knight(); /* And knight maps */
  277. init_king(); /* And King maps */
  278. init_maps(); /* Everything else */
  279. }
  280. #define Put_Ki_map(sq,low,hi) kingmvmaps[sq][LOBD] = low;\
  281. kingmvmaps[sq][HIBD] = hi;
  282. init_king()
  283. {
  284. Put_Ki_map(A1,a2|b2|b1,0);
  285. Put_Ki_map(B1,a1|a2|b2|c2|c1,0);
  286. Put_Ki_map(C1,b1|b2|c2|d2|d1,0);
  287. Put_Ki_map(D1,c1|c2|d2|e2|e1,0);
  288. Put_Ki_map(E1,d1|d2|e2|f2|f1,0);
  289. Put_Ki_map(F1,e1|e2|f2|g2|g1,0);
  290. Put_Ki_map(G1,f1|f2|g2|h1|h2,0);
  291. Put_Ki_map(H1,g1|g2|h2,0);
  292. Put_Ki_map(A2,a3|b3|b2|b1|a1,0);
  293. Put_Ki_map(B2,a1|a2|a3|b1|b3|c1|c2|c3,0);
  294. Put_Ki_map(C2,b1|b2|b3|c1|c3|d1|d2|d3,0);
  295. Put_Ki_map(D2,c1|c2|c3|d1|d3|e1|e2|e3,0);
  296. Put_Ki_map(E2,d1|d2|d3|e1|e3|f1|f2|f3,0);
  297. Put_Ki_map(F2,e1|e2|e3|f1|f3|g1|g2|g3,0);
  298. Put_Ki_map(G2,f1|f2|f3|g1|g3|h1|h2|h3,0);
  299. Put_Ki_map(H2,g1|g2|g3|h1|h3,0);
  300. Put_Ki_map(A3,a4|b4|b3|b2|a2,0);
  301. Put_Ki_map(B3,a2|a3|a4|b4|b2|c2|c3|c4,0);
  302. Put_Ki_map(C3,b2|b3|b4|c2|c4|d2|d3|d4,0);
  303. Put_Ki_map(D3,c2|c3|c4|d2|d4|e2|e3|e4,0);
  304. Put_Ki_map(E3,d2|d3|d4|e2|e4|f2|f3|f4,0);
  305. Put_Ki_map(F3,e2|e3|e4|f2|f4|g2|g3|g4,0);
  306. Put_Ki_map(G3,f2|f3|f4|g2|g4|h2|h3|h4,0);
  307. Put_Ki_map(H3,g2|g3|g4|h2|h4,0);
  308. Put_Ki_map(A4,a3|b3|b4,a5|b5);
  309. Put_Ki_map(B4,a3|a4|b3|c3|c4,a5|b5|c5);
  310. Put_Ki_map(C4,b4|b3|c3|d3|d4,b5|c5|d5);
  311. Put_Ki_map(D4,c4|c3|d3|e3|e4,c5|d5|e5);
  312. Put_Ki_map(E4,d3|d4|e3|f3|f4,d5|e5|f5);
  313. Put_Ki_map(F4,e3|f4|f3|g3|g4,e5|f5|g5);
  314. Put_Ki_map(G4,f3|f4|g3|h3|h4,f5|g5|h5);
  315. Put_Ki_map(H4,h3|g3|g4,g5|h5)
  316. Put_Ki_map(A5,a4|b4,b5|b6|a6);
  317. Put_Ki_map(B5,a4|b4|c4,a5|c5|a6|b6|c6);
  318. Put_Ki_map(C5,b4|c4|d4,b5|d5|b6|c6|d6);
  319. Put_Ki_map(D5,c4|d4|e4,c5|e5|c6|d6|e6);
  320. Put_Ki_map(E5,d4|e4|f4,d5|f5|d6|e6|f6);
  321. Put_Ki_map(F5,e4|f4|g4,e5|g5|e6|f6|g6);
  322. Put_Ki_map(G5,f4|g4|h4,f5|h5|f6|g6|h6);
  323. Put_Ki_map(H5,g4|h4,g5|g6|h6);
  324. Put_Ki_map(A6,0,a5|b5|b6|a7|b7);
  325. Put_Ki_map(B6,0,a5|b5|c5|a6|c6|a7|b7|c7);
  326. Put_Ki_map(C6,0,b5|c5|d5|b6|d6|b7|c7|d7);
  327. Put_Ki_map(D6,0,c5|d5|e5|c6|e6|c7|d7|e7);
  328. Put_Ki_map(E6,0,d5|e5|f5|d6|f6|d7|e7|f7);
  329. Put_Ki_map(F6,0,e5|f5|g5|e6|g6|e7|f7|g7);
  330. Put_Ki_map(G6,0,f5|g5|h5|f6|h6|f7|g7|h7);
  331. Put_Ki_map(H6,0,g5|h5|g6|g7|h7);
  332. Put_Ki_map(A7,0,a6|b6|b7|a8|b8);
  333. Put_Ki_map(B7,0,a6|b6|c6|a7|c7|a8|b8|c8);
  334. Put_Ki_map(C7,0,b6|c6|d6|b7|d7|b8|c8|d8);
  335. Put_Ki_map(D7,0,c6|d6|e6|c7|d7|c8|d8|e8);
  336. Put_Ki_map(E7,0,d6|e6|f6|d7|f7|d8|e8|f8);
  337. Put_Ki_map(F7,0,e6|f6|g6|e7|g7|e8|f8|g8);
  338. Put_Ki_map(G7,0,f6|g6|h6|f7|h7|f8|g8|h8);
  339. Put_Ki_map(H7,0,g6|h6|g7|g8|h8);
  340. Put_Ki_map(A8,0,a7|b7|b8);
  341. Put_Ki_map(B8,0,a7|b7|c7|a8|c8);
  342. Put_Ki_map(C8,0,b8|b7|c7|d7|d8);
  343. Put_Ki_map(D8,0,c8|c7|d7|e7|e8);
  344. Put_Ki_map(E8,0,d7|e7|f7|d8|f8);
  345. Put_Ki_map(F8,0,e8|e7|f7|g7|g8);
  346. Put_Ki_map(G8,0,f7|g7|h7|f8|h8);
  347. Put_Ki_map(H8,0,g8|g7|h7);
  348. }