File: util.c

package info (click to toggle)
empire 1.2-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 368 kB
  • ctags: 566
  • sloc: ansic: 5,592; makefile: 112
file content (446 lines) | stat: -rw-r--r-- 8,526 bytes parent folder | download
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* $Id: util.c,v 1.1.1.1 2000/10/04 02:28:54 weasel Exp $  - (c) Copyright 1987, 1988 Chuck Simmons */

/*
 *    Copyright (C) 1987, 1988 Chuck Simmons
 * 
 * See the file COPYING, distributed with empire, for restriction
 * and warranty information.
 */

/*
util.c -- various utility routines.
*/

#include <curses.h>
#include <ctype.h>
#include "empire.h"
#include "extern.h"


/*
Convert a string to uppercase.
Shirley this is defined elsewhere?
*/

#include <ctype.h>

void
tupper (str)
char	*str;
{
	while (*str) {
		if (islower (*str)) *str = upper (*str);
		str++;
	}
}

/*
Convert a character to uppercase (if it is lowercase)
*/

char
upper (c)
char c;
{
	if (islower (c))
		return toupper (c);
	else return c;
}

/*
Clear the end of a specified line starting at the specified column.
*/

void
clreol(linep, colp)
int linep, colp;
{
	(void) move (linep, colp);
	(void) clrtoeol();
}

/*
Initialize the terminal.
*/

void
ttinit()
{
	(void) initscr();
	(void) noecho();
	(void) crmode();
#ifdef A_COLOR
	init_colors();
#endif /* A_COLOR */
	lines = LINES;
	cols = COLS;
	if (lines > MAP_HEIGHT + NUMTOPS + 1)
		lines = MAP_HEIGHT + NUMTOPS + 1;
	if (cols > MAP_WIDTH + NUMSIDES)
		cols = MAP_WIDTH + NUMSIDES;
}


/*
Clear the screen.  We must also kill information maintained about the
display.
*/

void
clear_screen () {
	(void) clear ();
	(void) refresh ();
	kill_display ();
}

/*
Redraw the screen.
*/

void
redraw () {
	(void) clearok (curscr, TRUE);
	(void) refresh ();
}

/*
Wait a little bit to give user a chance to see a message.  We refresh
the screen and pause for a few milliseconds.
*/

void
delay () {
	(void) refresh ();
	(void) napms (delay_time); /* pause a bit */
}


/*
Clean up the display.  This routine gets called as we leave the game.
*/

void
close_disp()
{
	(void) move (LINES - 1, 0);
	(void) clrtoeol ();
	(void) refresh ();
	(void) endwin ();
}

/*
Position the cursor and output a string.
*/

void
pos_str1 (row, col, str, a, b, c, d, e, f, g, h)
int row, col;
char *str, *a;
int b, c, d, e, f, g, h;
{
	(void) move (row, col);
	addprintf1 (str, a, b, c, d, e, f, g, h);
}
void
pos_str (row, col, str, a, b, c, d, e, f, g, h)
int row, col;
char *str;
int a, b, c, d, e, f, g, h;
{
	(void) move (row, col);
	addprintf (str, a, b, c, d, e, f, g, h);
}

void
/* VARARGS1 */
addprintf (str, a, b, c, d, e, f, g, h)
char *str;
int a, b, c, d, e, f, g, h;
{
	char junkbuf[STRSIZE];
	
	(void) sprintf (junkbuf, str, a, b, c, d, e, f, g, h);
	(void) addstr (junkbuf);
}
void
/* VARARGS1 */
addprintf1 (str, a, b, c, d, e, f, g, h)
char *str;
char *a;
int b, c, d, e, f, g, h;
{
	char junkbuf[STRSIZE];
	
	(void) sprintf (junkbuf, str, a, b, c, d, e, f, g, h);
	(void) addstr (junkbuf);
}
void
/* VARARGS1 */
addprintf2 (str, a, b, c, d, e, f, g, h)
char *str;
char *a, *e, *f;
int b, c, d, g, h;
{
	char junkbuf[STRSIZE];
	
	(void) sprintf (junkbuf, str, a, b, c, d, e, f, g, h);
	(void) addstr (junkbuf);
}

/*
Report a bug.
*/

void
assert (expression, file, line)
char *expression;
char *file;
int line;
{
	char buf[STRSIZE];
	int a;

	(void) move (lines, 0);
	close_disp ();

	(void) sprintf (buf, "assert failed: file %s line %d: %s",
			file, line, expression);

	a = 1; /* keep lint quiet */
	a /= 0; /* force a core dump */
	a = a; /* keep lint quiet */
}

/*
End the game by cleaning up the display.
*/

void
empend ()
{
	close_disp ();
	exit (0);
}

/*
 * 03a 01Apr88 aml .Hacked movement algorithms for computer.
 * 02b 01Jun87 aml .First round of bug fixes.
 * 02a 01Jan87 aml .Translated to C.
 * 01b 27May85 cal .Fixed round number update bug. Made truename simple.
 * 01a 01Sep83 cal .Taken from a Decus tape
 */

void
ver ()
{
        (void) addstr ("EMPIRE, Version 5.00 site Amdahl 1-Apr-1988");
}

/*
Here is a little routine to perform consistency checking on the
database.  I'm finding that my database is becoming inconsistent,
and I've no idea why.  Possibly this will help.

We perform the following functions:

1)  Make sure no list contains loops.

2)  Make sure every object is in either the free list with 0 hits,
or it is in the correct object list and a location list with non-zero hits,
and an appropriate owner.

3)  Make sure every city is on the map.

4)  Make sure every object is in the correct location and that
objects on the map have non-zero hits.

5)  Make sure every object in a cargo list has a ship pointer.

6)  Make sure every object with a ship pointer is in that ship's
cargo list.
*/

static int in_free[LIST_SIZE]; /* TRUE if object in free list */
static int in_obj[LIST_SIZE]; /* TRUE if object in obj list */
static int in_loc[LIST_SIZE]; /* TRUE if object in a loc list */
static int in_cargo[LIST_SIZE]; /* TRUE if object in a cargo list */

void
check () {
	void check_cargo(), check_obj(), check_obj_cargo();
	
	long i, j;
	piece_info_t *p;
	
	/* nothing in any list yet */
	for (i = 0; i < LIST_SIZE; i++) {
		in_free[i] = 0;
		in_obj[i] = 0;
		in_loc[i] = 0;
		in_cargo[i] = 0;
	}
		
	/* Mark all objects in free list.  Make sure objects in free list
	have zero hits. */
	
	for (p = free_list; p != NULL; p = p->piece_link.next) {
		i = p - object;
		ASSERT (!in_free[i]);
		in_free[i] = 1;
		ASSERT (p->hits == 0);
		if (p->piece_link.prev)
			ASSERT (p->piece_link.prev->piece_link.next == p);
	}
	
	/* Mark all objects in the map.
	Check that cities are in corect location.
	Check that objects are in correct location,
	have a good owner, and good hits. */
	
	for (i = 0; i < MAP_SIZE; i++) {
		if (map[i].cityp) ASSERT (map[i].cityp->loc == i);
		
		for (p = map[i].objp; p != NULL; p = p->loc_link.next) {
			ASSERT (p->loc == i);
			ASSERT (p->hits > 0);
			ASSERT (p->owner == USER || p->owner == COMP);
				
			j = p - object;
			ASSERT (!in_loc[j]);
			in_loc[j] = 1;
			
			if (p->loc_link.prev)
				ASSERT (p->loc_link.prev->loc_link.next == p);
		}
	}

	/* make sure all cities are on map */

	for (i = 0; i < NUM_CITY; i++)
		ASSERT (map[city[i].loc].cityp == &(city[i]));

	/* Scan object lists. */
	
	check_obj (comp_obj, COMP);
	check_obj (user_obj, USER);
	
	/* Scan cargo lists. */
	
	check_cargo (user_obj[TRANSPORT], ARMY);
	check_cargo (comp_obj[TRANSPORT], ARMY);
	check_cargo (user_obj[CARRIER], FIGHTER);
	check_cargo (comp_obj[CARRIER], FIGHTER);
	
	/* Make sure all objects with ship pointers are in cargo. */

	check_obj_cargo (comp_obj);
	check_obj_cargo (user_obj);
	
	/* Make sure every object is either free or in loc and obj list. */

	for (i = 0; i < LIST_SIZE; i++)
		ASSERT (in_free[i] != (in_loc[i] && in_obj[i]));
}

/*
Check object lists.  We look for:

1)  Loops and bad prev pointers.

2)  Dead objects.

3)  Invalid types.

4)  Invalid owners.
*/

void
check_obj (list, owner)
piece_info_t **list;
int owner;
{
	long i, j;
	piece_info_t *p;
	
	for (i = 0; i < NUM_OBJECTS; i++)
	for (p = list[i]; p != NULL; p = p->piece_link.next) {
		ASSERT (p->owner == owner);
		ASSERT (p->type == i);
		ASSERT (p->hits > 0);
		
		j = p - object;
		ASSERT (!in_obj[j]);
		in_obj[j] = 1;
	
		if (p->piece_link.prev)
			ASSERT (p->piece_link.prev->piece_link.next == p);
	}
}

/*
Check cargo lists.  We assume object lists are valid.
as we will place bits in the 'in_cargo' array that are used by
'check_obj'.

Check for:

1)  Number of items in list is same as cargo count.

2)  Type of cargo is correct.

3)  Location of cargo matches location of ship.

4)  Ship pointer of cargo points to correct ship.

5)  There are no loops in cargo list and prev ptrs are correct.

6)  All cargo is alive.
*/

void
check_cargo (list, cargo_type)
piece_info_t *list;
int cargo_type;
{
	piece_info_t *p, *q;
	long j, count;
	
	for (p = list; p != NULL; p = p->piece_link.next) {
		count = 0;
		for (q = p->cargo; q != NULL; q = q->cargo_link.next) {
			count += 1; /* count items in list */
			ASSERT (q->type == cargo_type);
			ASSERT (q->owner == p->owner);
			ASSERT (q->hits > 0);
			ASSERT (q->ship == p);
			ASSERT (q->loc == p->loc);
			
			j = q - object;
			ASSERT (!in_cargo[j]);
			in_cargo[j] = 1;

			if (p->cargo_link.prev)
				ASSERT (p->cargo_link.prev->cargo_link.next == p);
                }
		ASSERT (count == p->count);
        }
}

/*
Scan through object lists making sure every object with a ship
pointer appears in a cargo list.  We assume object and cargo
lists are valid.
*/

void
check_obj_cargo (list)
piece_info_t **list;
{
	piece_info_t *p;
	long i;

	for (i = 0; i < NUM_OBJECTS; i++)
	for (p = list[i]; p != NULL; p = p->piece_link.next) {
		if (p->ship) ASSERT (in_cargo[p-object]);
	}
}