1 /*
2  *    Copyright (C) 1987, 1988 Chuck Simmons
3  *
4  * See the file COPYING, distributed with empire, for restriction
5  * and warranty information.
6  */
7 
8 /*
9 empire.h -- type and constant declarations
10 */
11 
12 #include <stdbool.h>
13 
14 #ifndef NULL
15 #define NULL 0
16 #endif
17 
18 typedef unsigned char uchar;
19 
20 typedef long loc_t;	/* represent a board location in 4-digit form */
21 
22 typedef long count_t;	/* for iterating over or counting board locations */
23 
24 #define ASSERT(x) if (!(x)) assert ("x", __FILE__, __LINE__);
25 #define ABORT assert ("aborting", __FILE__, __LINE__)
26 
27 /* directions one can move */
28 #define NORTH 0
29 #define NORTHEAST 1
30 #define EAST 2
31 #define SOUTHEAST 3
32 #define SOUTH 4
33 #define SOUTHWEST 5
34 #define WEST 6
35 #define NORTHWEST 7
36 
37 #define NUMTOPS 3 /* number of lines at top of screen for messages */
38 #define NUMSIDES 6 /* number of lines at side of screen */
39 
40 /*
41  * This used to be 80, and that was appropriate back when this game was
42  * played on hardware terminals.  Nowadays it's almost certain to be running
43  * on a software terminal emulator that would have been considered
44  * dizzyingly huge back in the day.  Memory is cheap, we'll leave some
45  * headroom.
46  */
47 #define STRSIZE 400 /* number of characters in a string */
48 
49 /* Information we maintain about cities. */
50 
51 #define UNOWNED 0
52 #define USER 1
53 #define COMP 2
54 
55 /* Piece types. */
56 #define ARMY 0
57 #define FIGHTER 1
58 #define PATROL 2
59 #define DESTROYER 3
60 #define SUBMARINE 4
61 #define TRANSPORT 5
62 #define CARRIER 6
63 #define BATTLESHIP 7
64 #define SATELLITE 8
65 #define NUM_OBJECTS 9 /* number of defined objects */
66 #define NOPIECE ((char)255) /* a 'null' piece */
67 
68 #define LIST_SIZE 5000 /* max number of pieces on board */
69 
70 typedef struct city_info {
71 	loc_t loc; /* location of city */
72 	uchar owner; /* UNOWNED, USER, COMP */
73 	long func[NUM_OBJECTS]; /* function for each object */
74 	long work; /* units of work performed */
75 	char prod; /* item being produced */
76 } city_info_t;
77 
78 /*
79 Types of programmed movement.  Use negative numbers for special
80 functions, use positive numbers to move toward a specific location.
81 */
82 
83 #define NOFUNC -1	/* no programmed function */
84 #define RANDOM -2	/* move randomly */
85 #define SENTRY -3	/* sleep */
86 #define FILL -4         /* fill transport */
87 #define LAND -5         /* land fighter at city */
88 #define EXPLORE -6      /* piece explores nearby */
89 #define ARMYLOAD -7     /* army moves toward and boards a transport */
90 #define ARMYATTACK -8   /* army looks for city to attack */
91 #define TTLOAD -9       /* transport moves toward loading armies */
92 #define REPAIR -10      /* ship moves toward port */
93 #define WFTRANSPORT -11 /* army boards a transport */
94 #define MOVE_N -12      /* move north */
95 #define MOVE_NE -13     /* move northeast */
96 #define MOVE_E -14      /* move east */
97 #define MOVE_SE -15     /* move southeast */
98 #define MOVE_S -16      /* move south */
99 #define MOVE_SW -17     /* move southwest */
100 #define MOVE_W -18      /* move west */
101 #define MOVE_NW -19     /* move northwest */
102 
103 /* Index to list of function names. */
104 #define FUNCI(x) (-(x)-1)
105 
106 /*
107 Macro to convert a movement function into a direction.
108 */
109 
110 #define MOVE_DIR(a) (-(a)+MOVE_N)
111 
112 /*
113 Information we maintain about each piece.
114 */
115 
116 typedef struct { /* ptrs for doubly linked list */
117 	struct piece_info *next; /* pointer to next in list */
118 	struct piece_info *prev; /* pointer to prev in list */
119 } link_t;
120 
121 typedef struct piece_info {
122 	link_t piece_link; /* linked list of pieces of this type */
123 	link_t loc_link; /* linked list of pieces at a location */
124 	link_t cargo_link; /* linked list of cargo pieces */
125 	int owner; /* owner of piece */
126 	int type; /* type of piece */
127 	loc_t loc; /* location of piece */
128 	long func; /* programmed type of movement */
129 	short hits; /* hits left */
130 	int moved; /* moves made */
131 	struct piece_info *ship; /* pointer to containing ship */
132 	struct piece_info *cargo; /* pointer to cargo list */
133 	short count; /* count of items on board */
134 	short range; /* current range (if applicable) */
135 } piece_info_t;
136 
137 /*
138 Macros to link and unlink an object from a doubly linked list.
139 */
140 
141 #define LINK(head,obj,list) { \
142 	obj->list.prev = NULL; \
143 	obj->list.next = head; \
144 	if (head) head->list.prev = obj; \
145 	head = obj; \
146 }
147 
148 #define UNLINK(head,obj,list) { \
149 	if (obj->list.next) \
150 		obj->list.next->list.prev = obj->list.prev; \
151         if (obj->list.prev) \
152 		obj->list.prev->list.next = obj->list.next; \
153         else head = obj->list.next; \
154 	obj->list.next = NULL; /* encourage mem faults in buggy code */ \
155 	obj->list.prev = NULL; \
156 }
157 
158 /* macros to set map and list of an object */
159 #define MAP(owner) ((owner) == USER ? game.user_map : game.comp_map)
160 #define LIST(owner) ((owner) == USER ? game.user_obj : game.comp_obj)
161 
162 /* macro to step through adjacent cells */
163 #define FOR_ADJ(loc,new_loc,i) for (i=0; (i<8 ? new_loc=loc+dir_offset[i],1 : 0); i++)
164 #define FOR_ADJ_ON(loc,new_loc,i) FOR_ADJ(loc,new_loc,i) if (game.real_map[new_loc].on_board)
165 
166 /*
167 We maintain attributes for each piece.  Attributes are currently constant,
168 but the way has been paved to allow user's to change attributes at the
169 beginning of a game.
170 */
171 
172 #define INFINITY 10000000 /* a large number */
173 
174 typedef struct piece_attr {
175 	char sname; /* eg 'C' */
176 	char name[20]; /* eg "aircraft carrier" */
177 	char nickname[20]; /* eg "carrier" */
178 	char article[20]; /* eg "an aircraft carrier" */
179 	char plural[20]; /* eg "aircraft carriers" */
180 	char terrain[4]; /* terrain piece can pass over eg "." */
181 	uchar build_time; /* time needed to build unit */
182 	uchar strength; /* attack strength */
183 	uchar max_hits; /* number of hits when completely repaired */
184 	uchar speed; /* number of squares moved per turn */
185 	uchar capacity; /* max objects that can be held */
186 	long range; /* range of piece */
187 } piece_attr_t;
188 
189 /*
190 There are 3 maps.  'map' describes the world as it actually
191 exists; it tells whether each map cell is land, water or a city;
192 it tells whether or not a square is on the board.
193 
194 'user_map' describes the user's view of the world.  'comp_map' describes
195 the computer's view of the world.
196 
197 As of the 1.7 version, MAP_WIDTH and MAP_HEIGHT are now free paramaters.
198 You can change them and the code will adjust properly.
199 */
200 
201 #define MAP_WIDTH 100
202 #define MAP_HEIGHT 60
203 #define MAP_SIZE (MAP_WIDTH * MAP_HEIGHT)
204 
205 /* #define NUM_CITY 70 */
206 /* #define NUM_CITY (MAP_SIZE / 85) */
207 #define NUM_CITY ((100 * (MAP_WIDTH + MAP_HEIGHT)) / 228)
208 
209 typedef struct real_map { /* a cell of the actual map */
210 	char contents; /* MAP_LAND, MAP_SEA, or MAP_CITY */
211 	bool on_board; /* TRUE iff on the board */
212 	city_info_t *cityp; /* ptr to city at this location */
213 	piece_info_t *objp; /* list of objects at this location */
214 } real_map_t;
215 
216 typedef struct view_map { /* a cell of one player's world view */
217 	char contents; /* MAP_LAND, MAP_SEA, MAP_CITY, 'A', 'a', etc */
218 	long seen; /* date when last updated */
219 } view_map_t;
220 
221 /* Define information we maintain for a pathmap. */
222 
223 typedef struct {
224 	int cost; /* total cost to get here */
225 	int inc_cost; /* incremental cost to get here */
226 	char terrain; /* T_LAND, T_WATER, T_UNKNOWN, T_PATH */
227 } path_map_t;
228 
229 #define T_UNKNOWN 0
230 #define T_PATH 1
231 #define T_LAND 2
232 #define T_WATER 4
233 #define T_AIR (T_LAND | T_WATER)
234 
235 /* A record for counts we obtain when scanning a continent. */
236 
237 typedef struct {
238 	int user_cities; /* number of user cities on continent */
239 	int user_objects[NUM_OBJECTS];
240 	int comp_cities;
241 	int comp_objects[NUM_OBJECTS];
242 	int size; /* size of continent in cells */
243 	int unowned_cities; /* number of unowned cities */
244 	int unexplored; /* unexplored territory */
245 } scan_counts_t;
246 
247 /* Define useful constants for accessing sectors. */
248 
249 #define SECTOR_ROWS 5 /* number of vertical sectors */
250 #define SECTOR_COLS 2 /* number of horizontal sectors */
251 #define NUM_SECTORS (SECTOR_ROWS * SECTOR_COLS) /* total sectors */
252 #define ROWS_PER_SECTOR ((MAP_HEIGHT+SECTOR_ROWS-1)/SECTOR_ROWS)
253 #define COLS_PER_SECTOR ((MAP_WIDTH+SECTOR_COLS-1)/SECTOR_COLS)
254 
255 /* Information we need for finding a path for moving a piece. */
256 
257 typedef struct {
258 	char city_owner; /* char that represents home city */
259 	char *objectives; /* list of objectives */
260 	int weights[11]; /* weight of each objective */
261 } move_info_t;
262 
263 /* special weights */
264 #define W_TT_BUILD -1 /* special cost for city building a tt */
265 
266 /* List of cells in the perimeter of our searching for a path. */
267 
268 typedef struct {
269 	long len; /* number of items in list */
270 	long list[MAP_SIZE]; /* list of locations */
271 } perimeter_t;
272 
273 enum win_t {no_win, wipeout_win, ratio_win};
274 
275 #define MAP_LAND	'+'
276 #define MAP_SEA 	'.'
277 #define MAP_CITY	'*'
278 
279 typedef struct {
280     /* user-supplied parameters */
281     int SMOOTH;        /* number of times to smooth map */
282     int WATER_RATIO;   /* percentage of map that is water */
283     int MIN_CITY_DIST; /* cities must be at least this far apart */
284     int delay_time;
285     int save_interval; /* turns between autosaves */
286 
287     /* the world */
288     real_map_t real_map[MAP_SIZE]; /* the way the world really looks */
289     view_map_t comp_map[MAP_SIZE]; /* computer's view of the world */
290     view_map_t user_map[MAP_SIZE]; /* user's view of the world */
291     city_info_t city[NUM_CITY]; /* city information */
292 
293     /* miscellaneous */
294     long date; /* number of game turns played */
295     bool automove; /* true iff user is in automove mode */
296     bool resigned; /* true iff computer resigned */
297     bool debug; /* true iff in debugging mode */
298     bool print_debug; /* true iff we print debugging stuff */
299     char print_vmap; /* the map-printing mode */
300     bool trace_pmap; /* true if we are tracing pmaps */
301     int win; /* set when game is over - not a bool */
302     char jnkbuf[STRSIZE]; /* general purpose temporary buffer */
303     bool save_movie; /* true iff we should save movie screens */
304     int user_score; /* "score" for user and computer */
305     int comp_score;
306     char *savefile;
307     bool showprod;
308 
309     /*
310       There is one array to hold all allocated objects no matter who
311       owns them.  Objects are allocated from the array and placed on
312       a list corresponding to the type of object and its owner.
313     */
314 
315     piece_info_t *free_list; /* index to free items in object list */
316     piece_info_t *user_obj[NUM_OBJECTS]; /* indices to user lists */
317     piece_info_t *comp_obj[NUM_OBJECTS]; /* indices to computer lists */
318     piece_info_t object[LIST_SIZE]; /* object list */
319 
320     /* Display information. */
321     int lines; /* lines on screen */
322     int cols; /* columns on screen */
323 } gamestate_t;
324 
325 /* end */
326