jail.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* This file contains code for X-CHESS.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. This file is part of X-CHESS.
  4. X-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 X-CHESS General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. X-CHESS, but only under the conditions described in the
  12. X-CHESS General Public License. A copy of this license is
  13. supposed to have been given to you along with X-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. /* RCS Info: $Revision: 1.3 $ on $Date: 86/11/26 12:09:54 $
  18. * $Source: /users/faustus/xchess/RCS/jail.c,v $
  19. * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
  20. * Permission is granted to do anything with this code except sell it
  21. * or remove this message.
  22. *
  23. */
  24. #include "xchess.h"
  25. #include "pawn_small.bitmap"
  26. #include "rook_small.bitmap"
  27. #include "knight_small.bitmap"
  28. #include "bishop_small.bitmap"
  29. #include "queen_small.bitmap"
  30. #include "king_small.bitmap"
  31. #include "pawn_small_outline.bitmap"
  32. #include "rook_small_outline.bitmap"
  33. #include "knight_small_outline.bitmap"
  34. #include "bishop_small_outline.bitmap"
  35. #include "queen_small_outline.bitmap"
  36. #include "king_small_outline.bitmap"
  37. static bool pos[32];
  38. static piecetype pcs[] = { KING, QUEEN, ROOK, ROOK, BISHOP, BISHOP, KNIGHT,
  39. KNIGHT, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN } ;
  40. extern int piecepos();
  41. extern short *bitsget();
  42. void
  43. jail_init(win)
  44. windata *win;
  45. {
  46. int i;
  47. for (i = 0; i < 32; i++)
  48. pos[i] = false;
  49. jail_draw(win);
  50. return;
  51. }
  52. #define JAIL_HEADER "Captured Pieces"
  53. void
  54. jail_draw(win)
  55. windata *win;
  56. {
  57. int i;
  58. short *bits;
  59. piece p;
  60. XSetDisplay(win->display);
  61. i = XStringWidth(JAIL_HEADER, win->large, 0, 0);
  62. XText(win->jailwin, (JAIL_WIDTH - i) / 2, 1, JAIL_HEADER,
  63. strlen(JAIL_HEADER), win->large->id,
  64. win->textcolor.pixel, win->textback.pixel);
  65. for (i = 0; i < 16; i++)
  66. if (pos[i]) {
  67. p.color = WHITE;
  68. p.type = pcs[i];
  69. bits = bitsget(&p);
  70. XBitmapBitsPut(win->jailwin, 5 + (i % 8) * 32, 25 +
  71. (i / 8) * 32, 32, 32, bits,
  72. win->blackpiece.pixel,
  73. win->textback.pixel,
  74. (Bitmap) 0, GXcopy, AllPlanes);
  75. } else {
  76. XPixSet(win->jailwin, 5 + (i % 8) * 32, 25 + (i / 8) *
  77. 32, 32, 32, win->textback.pixel);
  78. }
  79. for (i = 0; i < 16; i++)
  80. if (pos[i + 16]) {
  81. p.color = BLACK;
  82. p.type = pcs[i];
  83. bits = bitsget(&p);
  84. XBitmapBitsPut(win->jailwin, 5 + (i % 8) * 32, 94 +
  85. (i / 8) * 32, 32, 32, bits,
  86. win->blackpiece.pixel,
  87. win->textback.pixel,
  88. (Bitmap) 0, GXcopy,
  89. AllPlanes);
  90. } else {
  91. XPixSet(win->jailwin, 5 + (i % 8) * 32, 94 + (i / 8) *
  92. 32, 32, 32, win->textback.pixel);
  93. }
  94. return;
  95. }
  96. void
  97. jail_add(p)
  98. piece *p;
  99. {
  100. int i = piecepos(p, false);
  101. short *bits;
  102. pos[i] = true;
  103. bits = bitsget(p);
  104. XSetDisplay(win1->display);
  105. XBitmapBitsPut(win1->jailwin, 5 + (i % 8) * 32, ((i >= 16) ? 30 : 25) +
  106. (i / 8) * 32, 32, 32, bits, win1->blackpiece.pixel,
  107. win1->textback.pixel, (Bitmap) 0, GXcopy, AllPlanes);
  108. if (!oneboard) {
  109. XSetDisplay(win2->display);
  110. XBitmapBitsPut(win2->jailwin, 5 + (i % 8) * 32, ((i >= 16) ? 30
  111. : 25) + (i / 8) * 32, 32, 32, bits,
  112. win2->blackpiece.pixel, win2->textback.pixel,
  113. (Bitmap) 0, GXcopy, AllPlanes);
  114. }
  115. return;
  116. }
  117. void
  118. jail_remove(p)
  119. piece *p;
  120. {
  121. int i = piecepos(p, true);
  122. pos[i] = false;
  123. XSetDisplay(win1->display);
  124. XPixSet(win1->jailwin, 5 + (i % 8) * 32, ((i >= 16) ? 30 : 25) +
  125. (i / 8) * 32, 32, 32, win1->textback.pixel);
  126. if (!oneboard) {
  127. XSetDisplay(win2->display);
  128. XPixSet(win2->jailwin, 5 + (i % 8) * 32, ((i >= 16) ? 30 : 25) +
  129. (i / 8) * 32, 32, 32, win2->textback.pixel);
  130. }
  131. return;
  132. }
  133. static short *
  134. bitsget(p)
  135. piece *p;
  136. {
  137. short *bits;
  138. switch (p->type) {
  139. case PAWN:
  140. bits = (p->color == WHITE) ? pawn_small_outline_bits :
  141. pawn_small_bits;
  142. break;
  143. case ROOK:
  144. bits = (p->color == WHITE) ? rook_small_outline_bits :
  145. rook_small_bits;
  146. break;
  147. case KNIGHT:
  148. bits = (p->color == WHITE) ? knight_small_outline_bits :
  149. knight_small_bits;
  150. break;
  151. case BISHOP:
  152. bits = (p->color == WHITE) ? bishop_small_outline_bits :
  153. bishop_small_bits;
  154. break;
  155. case QUEEN:
  156. bits = (p->color == WHITE) ? queen_small_outline_bits :
  157. queen_small_bits;
  158. break;
  159. case KING:
  160. bits = (p->color == WHITE) ? king_small_outline_bits :
  161. king_small_bits;
  162. break;
  163. }
  164. return (bits);
  165. }
  166. static int
  167. piecepos(p, there)
  168. piece *p;
  169. bool there;
  170. {
  171. int i, base = (p->color == WHITE) ? 0 : 16;
  172. switch (p->type) {
  173. case PAWN:
  174. for (i = base + 8; (i < base + 15) && pos[i]; i++)
  175. ;
  176. if (there && !pos[i])
  177. i--;
  178. break;
  179. case KING:
  180. /* Hmm... */
  181. i = base;
  182. break;
  183. case QUEEN:
  184. i = base + 1;
  185. break;
  186. case ROOK:
  187. i = base + 2;
  188. if ((there && pos[i + 1]) || (!there && pos[i]))
  189. i++;
  190. break;
  191. case BISHOP:
  192. i = base + 4;
  193. if ((there && pos[i + 1]) || (!there && pos[i]))
  194. i++;
  195. break;
  196. case KNIGHT:
  197. i = base + 6;
  198. if ((there && pos[i + 1]) || (!there && pos[i]))
  199. i++;
  200. break;
  201. }
  202. return (i);
  203. }