1 /*
2  * init.c
3  *
4  * Initialisations for 3Dc engine and pieces.
5  * Interface initialisation is external.
6  */
7 /*
8 
9     3Dc, a game of 3-Dimensional Chess
10     Copyright (C) 1995  Paul Hicks
11 
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16 
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU General Public License for more details.
21 
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 
26     E-Mail: paulh@euristix.ie
27 */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #include "machine.h"
33 #include "3Dc.h"
34 
35 int n3DcErr;
36 Piece *SQUARE_EMPTY, *SQUARE_INVALID;
37 
38 stack *MoveStack; /* The history of moves */
39 Piece *Board[LEVELS][RANKS][FILES]; /* The board */
40 Piece *Muster[COLOURS][PIECES]; /* The list of pieces */
41 int titleCount[TITLES] =
42 {
43   /* king     */ 1,
44   /* queen    */ 1,
45   /* bishop   */ 2,
46   /* knight   */ 2,
47   /* rook     */ 2,
48   /* prince   */ 2,
49   /* princess */ 2,
50   /* abbey    */ 4,
51   /* cannon   */ 4,
52   /* galley   */ 4,
53   /* pawn     */ 24
54 };
55 
56 /*
57  * This function sets up the board
58  */
59 Global Boolean
Init3Dc(void)60 Init3Dc(void)
61 {
62   File x;
63   Rank y;
64   Level z;
65   Colour bw;
66   int count[COLOURS][TITLES] = {{0,0,0,0,0,0,0,0,0,0,0},
67                                 {0,0,0,0,0,0,0,0,0,0,0}};
68   Title name;
69   /* This structure is mainly for "obviousness"; it is entirely trivial */
70   Title StartBoard[LEVELS][RANKS][FILES] =
71 
72   { /* The boards */
73     { /* Bottom board */
74       { galley,  cannon, abbey, prince, princess,abbey, cannon, galley},
75       {   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn},
76       {   none,   none,   none,   none,   none,   none,   none,   none},
77       {   none,   none,   none,   none,   none,   none,   none,   none},
78       {   none,   none,   none,   none,   none,   none,   none,   none},
79       {   none,   none,   none,   none,   none,   none,   none,   none},
80       {   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn},
81       { galley,  cannon, abbey, prince, princess,abbey, cannon, galley},
82     },
83     { /* Middle board */
84       {   rook, knight, bishop,   king,  queen, bishop, knight,   rook},
85       {   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn},
86       {   none,   none,   none,   none,   none,   none,   none,   none},
87       {   none,   none,   none,   none,   none,   none,   none,   none},
88       {   none,   none,   none,   none,   none,   none,   none,   none},
89       {   none,   none,   none,   none,   none,   none,   none,   none},
90       {   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn},
91       {   rook, knight, bishop,   king,  queen, bishop, knight,   rook}
92     },
93     { /* Top board */
94       { galley,  cannon, abbey, prince, princess,abbey, cannon, galley},
95       {   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn},
96       {   none,   none,   none,   none,   none,   none,   none,   none},
97       {   none,   none,   none,   none,   none,   none,   none,   none},
98       {   none,   none,   none,   none,   none,   none,   none,   none},
99       {   none,   none,   none,   none,   none,   none,   none,   none},
100       {   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn},
101       { galley,  cannon, abbey, prince, princess,abbey, cannon, galley},
102     }
103   }; /* StartBoard */
104 
105   for (z = 0; z < LEVELS; ++z)
106     {
107       bw = WHITE;
108       for (y = 0; y < RANKS; ++y)
109         {
110           /* From the 4th rank on is black's half of the board */
111           if (y == 4)
112             bw = BLACK;
113 
114           for (x = 0; x < FILES; ++x)
115             {
116               name = StartBoard[z][y][x];
117               if ((name != none)
118                   /*
119                    * this part of the conditional is unnecessary as
120                    * there are no "unknown" variables
121                    */
122                   /*
123                    * && (count[bw][name] < titleCount[name])
124                    */
125                   )
126                 {
127                   Muster[bw][MusterIdx(name, count[bw][name])] =
128                     Board[z][y][x] =
129                       PieceNew(name, x, y, z, bw);
130                   (count[bw][name])++;
131                 }
132             }
133         }
134     }
135 
136   /* That's the pieces done.  Now for the move stack */
137   MoveStack = StackNew();
138 
139   /* Start the random number generator */
140   srandom((unsigned)time(NULL));
141 
142   /*
143    * And finally initialise the global variables:
144    * these are really dynamic global identifiers, in that they
145    * are read-only interfaces to various modules; kind of like
146    * getopt()'s optind and optarg.
147    */
148   n3DcErr = 0;
149 
150   SQUARE_INVALID = (Piece *)malloc(sizeof(Piece));
151   SQUARE_EMPTY = (Piece *)malloc(sizeof(Piece));
152   if (!CHECK(SQUARE_INVALID != NULL) &&
153       !CHECK(SQUARE_EMPTY != NULL))
154     return FALSE; /* If there's no memory now there never will be.. */
155 
156   return TRUE;
157 }
158 
159