File: usermove.c

package info (click to toggle)
empire 1.7-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 368 kB
  • ctags: 565
  • sloc: ansic: 5,627; makefile: 109
file content (1117 lines) | stat: -rw-r--r-- 26,090 bytes parent folder | download | duplicates (2)
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
/* %W% %G% %U% - (c) Copyright 1987, 1988 Chuck Simmons */

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

/*
usermove.c -- Let the user move her troops.
*/

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

void fatal(piece_info_t *obj,long loc,char *message,char *response);
void move_to_dest(piece_info_t *obj,long dest);
void move_army_to_city(piece_info_t *obj,long city_loc);
int awake(piece_info_t *obj);
extern int get_piece_name();

void
user_move () {
	void piece_move();

	int i, j, sec, sec_start;
	piece_info_t *obj, *next_obj;
	int prod;

	/* First we loop through objects to update the user's view
	of the world and perform any other necessary processing.
	We would like to have the world view up to date before
	asking the user any questions.  This means that we should
	also scan through all cities before possibly asking the
	user what to produce in each city. */

	for (i = 0; i < NUM_OBJECTS; i++)
	for (obj = user_obj[i]; obj != NULL; obj = obj->piece_link.next) {
		obj->moved = 0; /* nothing moved yet */
		scan (user_map, obj->loc); /* refresh user's view of world */
	}

	/* produce new hardware */
	for (i = 0; i < NUM_CITY; i++)
	    if (city[i].owner == USER) {
		scan (user_map, city[i].loc);
		prod = city[i].prod;

		if (prod == NOPIECE) { /* need production? */
			set_prod (&(city[i])); /* ask user what to produce */
		}
		else if (city[i].work++ >= (long)piece_attr[prod].build_time) {
				/* kermyt begin */
			ksend1("%s has been completed at city %d.\n", piece_attr[prod].article,city[i].loc,0,0,0,0,0,0);
				/* kermyt end */
			comment1 ("%s has been completed at city %d.\n", piece_attr[prod].article,city[i].loc,0,0,0,0,0,0);

			produce (&city[i]);
			/* produce should set object.moved to 0 */
		}
	}

	/* move all satellites */
	for (obj = user_obj[SATELLITE]; obj != NULL; obj = next_obj) {
		next_obj = obj->piece_link.next;
		move_sat (obj);
	}
	
	sec_start = cur_sector (); /* get currently displayed sector */
	if (sec_start == -1) sec_start = 0;

	/* loop through sectors, moving every piece in the sector */
	for (i = sec_start; i < sec_start + NUM_SECTORS; i++) {
		sec = i % NUM_SECTORS;
		sector_change (); /* allow screen to be redrawn */

		for (j = 0; j < NUM_OBJECTS; j++) /* loop through obj lists */
		for (obj = user_obj[move_order[j]]; obj != NULL;
			obj = next_obj) { /* loop through objs in list */
			next_obj = obj->piece_link.next;

			if (!obj->moved) /* object not moved yet? */
			if (loc_sector (obj->loc) == sec) /* object in sector? */
			piece_move (obj); /* yup; move the object */
		}
		if (cur_sector () == sec) { /* is sector displayed? */
			print_sector_u (sec); /* make screen up-to-date */
			(void) refresh (); /* show it to the user */
		}
	}
	if (save_movie) save_movie_screen ();
}

/*
Move a piece.  We loop until all the moves of a piece are made.  Within
the loop, we first awaken the piece if it is adjacent to an enemy piece.
Then we attempt to handle any preprogrammed function for the piece.  If
the piece has not moved after this, we ask the user what to do.
*/

void
piece_move (obj)
piece_info_t *obj;
{
	void move_random(), move_fill(), move_land(), move_explore();
	void move_path(), move_dir(), move_armyload(), ask_user();
	void move_armyattack(), move_ttload(), move_repair();
	void move_transport();

	int changed_loc;
	int speed, max_hits;
	int saved_moves;
	int need_input;
	long saved_loc;
	city_info_t *cityp;

	/* set func for piece if on city */
	cityp = find_city (obj->loc);
	if (cityp != NULL)
		if (cityp->func[obj->type] != NOFUNC)
			obj->func = cityp->func[obj->type];

	changed_loc = FALSE; /* not changed yet */
	speed = piece_attr[obj->type].speed;
	max_hits = piece_attr[obj->type].max_hits;
	need_input = FALSE; /* don't require user input yet */

	while (obj->moved < obj_moves (obj)) {
		saved_moves = obj->moved; /* save moves made */
		saved_loc = obj->loc; /* remember starting location */

		if (awake (obj) || need_input){ /* need user input? */
			ask_user (obj);
			topini (); /* clear info lines */
			display_loc_u (obj->loc); /* let user see result */
			(void) refresh ();
			need_input = FALSE; /* we got it */
		}
		
		if (obj->moved == saved_moves) /* user set function? */
		switch (obj->func) { /* handle preprogrammed function */
		case NOFUNC:    break;
		case RANDOM:    move_random (obj); break;
		case SENTRY:    obj->moved = speed; break;
		case FILL:      move_fill (obj); break;
		case LAND:      move_land (obj); break;
		case EXPLORE:   move_explore (obj); break;
		case ARMYLOAD:  move_armyload (obj); break;
		case ARMYATTACK:move_armyattack (obj); break;
		case TTLOAD:    move_ttload (obj); break;
		case REPAIR:    move_repair (obj); break;
		case WFTRANSPORT: move_transport (obj); break;

		case MOVE_N:
		case MOVE_NE:
		case MOVE_E:
		case MOVE_SE:
		case MOVE_S:
		case MOVE_SW:
		case MOVE_W:
		case MOVE_NW:
			move_dir (obj); break;

		default: move_path (obj); break;
		}

		if (obj->moved == saved_moves) need_input = TRUE;
		
		/* handle fighters specially.  If in a city or carrier, turn
		is over and reset range to max.  Otherwise, if
		range = 0, fighter crashes and burns and turn is over. */

		if (obj->type == FIGHTER && obj->hits > 0) {
			if ((user_map[obj->loc].contents == 'O'
			  || user_map[obj->loc].contents == 'C')
			&& obj->moved > 0) {
				obj->range = piece_attr[FIGHTER].range;
				obj->moved = speed;
				obj->func = NOFUNC;
				comment ("Landing confirmed.",0,0,0,0,0,0,0,0);
			}
			else if (obj->range == 0) {
				comment ("Fighter at %d crashed and burned.",obj->loc,0,0,0,0,0,0,0);
				kill_obj (obj, obj->loc);
			}
		}

		if (saved_loc != obj->loc) changed_loc = TRUE;
	}
	/* if a boat is in port, damaged, and never moved, fix some damage */
	if (obj->hits > 0 /* still alive? */
		&& !changed_loc /* object never changed location? */
		&& obj->type != ARMY && obj->type != FIGHTER /* it is a boat? */
		&& obj->hits < max_hits /* it is damaged? */
		&& user_map[obj->loc].contents == 'O') /* it is in port? */
	obj->hits++; /* fix some damage */
}

/*
Move a piece at random.  We create a list of empty squares to which
the piece can move.  If there are none, we do nothing, otherwise we 
move the piece to a random adjacent square.
*/

void move_random (obj)
piece_info_t *obj;
{
	long loc_list[8];
	int i, nloc;
	long loc;

	nloc = 0;

	for (i = 0; i < 8; i++) {
		loc = obj->loc + dir_offset[i];
		if (good_loc (obj, loc)) {
			loc_list[nloc] = loc; /* remember this location */
			nloc++; /* count locations we can move to */
		}
	}
	if (nloc == 0) return; /* no legal move */
	i = irand ((long)nloc-1); /* choose random direction */
	move_obj (obj, loc_list[i]); /* move the piece */
}

/*
Have a piece explore.  We look for the nearest unexplored territory
which the piece can reach and have to piece move toward the
territory.
*/

void move_explore (obj)
piece_info_t *obj;
{
	path_map_t path_map[MAP_SIZE];
	long loc;
	char *terrain;

	switch (obj->type) {
	case ARMY:
		loc = vmap_find_lobj (path_map, user_map, obj->loc, &user_army);
		terrain = "+";
		break;
	case FIGHTER:
		loc = vmap_find_aobj (path_map, user_map, obj->loc, &user_fighter);
		terrain = "+.O";
		break;
	default:
		loc = vmap_find_wobj (path_map, user_map, obj->loc, &user_ship);
		terrain = ".O";
		break;
	}
	
	if (loc == obj->loc) return; /* nothing to explore */

	if (user_map[loc].contents == ' ' && path_map[loc].cost == 2)
		vmap_mark_adjacent (path_map, obj->loc);
	else vmap_mark_path (path_map, user_map, loc);

	loc = vmap_find_dir (path_map, user_map, obj->loc, terrain, " ");
	if (loc != obj->loc) move_obj (obj, loc);
}

/*
Move an army onto a transport when it arrives.  We scan around the
army to find a non-full transport.  If one is present, we move the
army to the transport and waken the army.
*/

void
move_transport (obj)
piece_info_t *obj;
{
	long loc;

	/* look for an adjacent transport */
	loc = find_transport (USER, obj->loc);
	
	if (loc != obj->loc) {
		move_obj (obj, loc);
		obj->func = NOFUNC;
	}
	else obj->moved = piece_attr[obj->type].speed;
}

/*
Move an army toward the nearest loading transport.
If there is an adjacent transport, move the army onto
the transport, and awaken the army.
*/

static view_map_t amap[MAP_SIZE];

void
move_armyload (obj)
piece_info_t *obj;
{
	long loc;
	piece_info_t *p;
	int i;

	ABORT;
	
	/* look for an adjacent transport */
	loc = find_transport (USER, obj->loc);

	if (loc != obj->loc) {
		move_obj (obj, loc);
		obj->func = NOFUNC;
	}
	else { /* look for nearest non-full transport */
		(void) memcpy (amap, user_map, sizeof (view_map_t) * MAP_SIZE);

		/* mark loading transports or cities building transports */
		for (p = user_obj[TRANSPORT]; p; p = p->piece_link.next)
		if (p->count < obj_capacity (p)) /* not full? */
		amap[p->loc].contents = '$';
		
		for (i = 0; i < NUM_CITY; i++)
		if (city[i].owner == USER && city[i].prod == TRANSPORT)
		amap[city[i].loc].contents = '$';
	}
}
		
/*
Move an army toward an attackable city or enemy army.
*/

void
move_armyattack (obj)
piece_info_t *obj;
{
	path_map_t path_map[MAP_SIZE];
	long loc;

	ASSERT (obj->type == ARMY);

	loc = vmap_find_lobj (path_map, user_map, obj->loc, &user_army_attack);
	
	if (loc == obj->loc) return; /* nothing to attack */

	vmap_mark_path (path_map, user_map, loc);

	loc = vmap_find_dir (path_map, user_map, obj->loc, "+", "X*a");
	if (loc != obj->loc) move_obj (obj, loc);
}

void
move_ttload (obj)
piece_info_t *obj;
{
	ABORT;
	obj = obj;
}

/*
Move a ship toward port.  If the ship is healthy, wake it up.
*/

void
move_repair (obj)
piece_info_t *obj;
{
	path_map_t path_map[MAP_SIZE];
	long loc;

	ASSERT (obj->type > FIGHTER);
	
	if (obj->hits == piece_attr[obj->type].max_hits) {
		obj->func = NOFUNC;
		return;
	}
	
	if (user_map[obj->loc].contents == 'O') { /* it is in port? */
		obj->moved += 1;
		return;
	}

	loc = vmap_find_wobj (path_map, user_map, obj->loc, &user_ship_repair);
	
	if (loc == obj->loc) return; /* no reachable city */

	vmap_mark_path (path_map, user_map, loc);

	/* try to be next to ocean to avoid enemy pieces */
	loc = vmap_find_dir (path_map, user_map, obj->loc, ".O", ".");
	if (loc != obj->loc) move_obj (obj, loc);
}

/*
Here we have a transport or carrier waiting to be filled.  If the
object is not full, we set the move count to its maximum value.
Otherwise we awaken the object.
*/

void move_fill (obj)
piece_info_t *obj;
{
	if (obj->count == obj_capacity (obj)) /* full? */
		obj->func = NOFUNC; /* awaken full boat */
	else obj->moved = piece_attr[obj->type].speed;
}

/*
Here we have a piece that wants to land at the nearest carrier or
owned city.  We scan through the lists of cities and carriers looking
for the closest one.  We then move toward that item's location.
The nearest landing field must be within the object's range.
*/

void
move_land (obj)
piece_info_t *obj;
{
	long best_dist, best_loc;
	long new_dist;
	piece_info_t *p;

	best_dist = find_nearest_city (obj->loc, USER, &best_loc);

	for (p = user_obj[CARRIER]; p != NULL; p = p->piece_link.next) {
		new_dist = dist (obj->loc, p->loc);
		if (new_dist < best_dist) {
			best_dist = new_dist;
			best_loc = p->loc;
		}
	}
	if (best_dist == 0) obj->moved += 1; /* fighter is on a city */
	
	else if (best_dist <= obj->range)
		move_to_dest (obj, best_loc);
		
	else obj->func = NOFUNC; /* can't reach city or carrier */
}

/*
Move a piece in the specified direction if possible.
If the object is a fighter which has travelled for half its range,
we wake it up.
*/

void move_dir (obj)
piece_info_t *obj;
{
	long loc;
	int dir;

	dir = MOVE_DIR (obj->func);
	loc = obj->loc + dir_offset[dir];

	if (good_loc (obj, loc))
		move_obj (obj, loc);
}

/*
Move a piece toward a specified destination if possible.  For each
direction, we see if moving in that direction would bring us closer
to our destination, and if there is nothing in the way.  If so, we
move in the first direction we find.
*/

void move_path (obj)
piece_info_t *obj;
{
	if (obj->loc == obj->func)
		obj->func = NOFUNC;
	else move_to_dest (obj, obj->func);
}

/*
Move a piece toward a specific destination.  We first map out
the paths to the destination, if we can't get there, we return.
Then we mark the paths to the destination.  Then we choose a
move.
*/

void move_to_dest (obj, dest)
piece_info_t *obj;
long dest;
{
	path_map_t path_map[MAP_SIZE];
	int fterrain;
	char *mterrain;
	long new_loc;
	
	switch (obj->type) {
	case ARMY:
		fterrain = T_LAND;
		mterrain = "+";
		break;
	case FIGHTER:
		fterrain = T_AIR;
		mterrain = "+.O";
		break;
	default:
		fterrain = T_WATER;
		mterrain = ".O";
		break;
	}
	
	new_loc = vmap_find_dest (path_map, user_map, obj->loc, dest,
                                  USER, fterrain);
	if (new_loc == obj->loc) return; /* can't get there */
	
	vmap_mark_path (path_map, user_map, dest);
	new_loc = vmap_find_dir (path_map, user_map, obj->loc, mterrain, " .");
	if (new_loc == obj->loc) return; /* can't move ahead */
	ASSERT (good_loc (obj, new_loc));
	move_obj (obj, new_loc); /* everything looks good */
}

/*
Ask the user to move her piece.
*/

void ask_user (obj)
piece_info_t *obj;
{
	void user_skip(), user_fill(), user_dir(), user_set_dir();
	void user_wake(), user_set_city_func(), user_cancel_auto();
	void user_redraw(), user_random(), user_land(), user_sentry();
	void user_help(), reset_func(), user_explore();
	void user_build(), user_transport();
	void user_armyattack(), user_repair();

	char c;

    for (;;) {
	display_loc_u (obj->loc); /* display piece to move */
	describe_obj (obj); /* describe object to be moved */
	display_score (); /* show current score */
	display_loc_u (obj->loc); /* reposition cursor */

	c = get_chx (); /* get command from user (no echo) */
	switch (c) {
	case 'Q': user_dir (obj, NORTHWEST); return;
	case 'W': user_dir (obj, NORTH); return;
	case 'E': user_dir (obj, NORTHEAST); return;
	case 'D': user_dir (obj, EAST); return;
	case 'C': user_dir (obj, SOUTHEAST); return;
	case 'X': user_dir (obj, SOUTH); return;
	case 'Z': user_dir (obj, SOUTHWEST); return;
	case 'A': user_dir (obj, WEST); return;

	case 'J': edit (obj->loc); reset_func (obj); return;
	case 'V': user_set_city_func (obj); reset_func (obj); return;
	
	case ' ': user_skip (obj); return;
	case 'F': user_fill (obj); return;
	case 'I': user_set_dir (obj); return;
	case 'R': user_random (obj); return;
	case 'S': user_sentry (obj); return;
	case 'L': user_land (obj); return;
	case 'G': user_explore (obj); return;
	case 'T': user_transport (obj); return;
	case 'U': user_repair (obj); return;
	case 'Y': user_armyattack (obj); return;

	case 'B': user_build (obj); break;
	case 'H': user_help (); break;
	case 'K': user_wake (obj); break;
	case 'O': user_cancel_auto (); break;
	case '\014':
	case 'P': user_redraw (); break;
	case '?': describe_obj (obj); break;

	default: (void) beep ();
	}
    }
}

/*
Here, if the passed object is on a city, we assign
the city's function to the object.  However, we then awaken the
object if necessary because the user only changed the city
function, and did not tell us what to do with the object.
*/

void
reset_func (obj)
piece_info_t *obj;
{
	city_info_t *cityp;
	
	cityp = find_city (obj->loc);

	if (cityp != NULL)
	if (cityp->func[obj->type] != NOFUNC) {
		obj->func = cityp->func[obj->type];
		(void) awake (obj);
	} 
}

/*
Increment the number of moves a piece has used.  If the piece
is an army and the army is in a city, move the army to
the city.
*/

void
user_skip (obj)
piece_info_t *obj;
{
	if (obj->type == ARMY && user_map[obj->loc].contents == 'O')
		move_army_to_city (obj, obj->loc);
	else obj->moved++;
}

/*
Put an object in FILL mode.  The object must be either a transport
or carrier.  If not, we beep at the user.
*/

void
user_fill (obj)
piece_info_t *obj;
{
	if (obj->type != TRANSPORT && obj->type != CARRIER) (void) beep ();
	else obj->func = FILL;
}

/*
Print out help information.
*/

void
user_help () {
	char c;

	help (help_user, user_lines);
	prompt ("Press any key to continue: ",0,0,0,0,0,0,0,0);
	c = get_chx ();
	c = c; /* keep lint happy */
}

/*
Set an object's function to move in a certain direction.
*/

void
user_set_dir (obj)
piece_info_t *obj;
{
	char c;

	c = get_chx ();
	switch (c) {
	case 'Q': obj->func = MOVE_NW; break;
	case 'W': obj->func = MOVE_N ; break;
	case 'E': obj->func = MOVE_NE; break;
	case 'D': obj->func = MOVE_E ; break;
	case 'C': obj->func = MOVE_SE; break;
	case 'X': obj->func = MOVE_S ; break;
	case 'Z': obj->func = MOVE_SW; break;
	case 'A': obj->func = MOVE_W ; break;
	default: (void) beep (); break;
	}
}

/*
Wake up the current piece.
*/

void
user_wake (obj)
piece_info_t *obj;
{
	obj->func = NOFUNC;
}

/*
Set the piece's func to random.  
*/

void
user_random (obj)
piece_info_t *obj;
{
	obj->func = RANDOM;
}

/*
Set a piece's function to sentry.
*/

void
user_sentry (obj)
piece_info_t *obj;
{
	obj->func = SENTRY;
}

/*
Set a fighter's function to land at the nearest city.
*/

void
user_land (obj)
piece_info_t *obj;
{
	if (obj->type != FIGHTER) (void) beep ();
	else obj->func = LAND;
}

/*
Set the piece's func to explore.
*/

void
user_explore (obj)
piece_info_t *obj;
{
	obj->func = EXPLORE;
}

/*
Set an army's function to WFTRANSPORT.
*/

void
user_transport (obj)
piece_info_t *obj;
{
	if (obj->type != ARMY) (void) beep ();
	else obj->func = WFTRANSPORT;
}

/*
Set an army's function to ARMYATTACK.
*/

void
user_armyattack (obj)
piece_info_t *obj;
{
	if (obj->type != ARMY) (void) beep ();
	else obj->func = ARMYATTACK;
}

/*
Set a ship's function to REPAIR.
*/

void
user_repair (obj)
piece_info_t *obj;
{
	if (obj->type == ARMY || obj->type == FIGHTER) (void) beep ();
	else obj->func = REPAIR;
}

/*
Set a city's function.
*/

void
user_set_city_func (obj)
piece_info_t *obj;
{
	void e_city_fill(), e_city_explore(), e_city_stasis();
	void e_city_wake(), e_city_random(), e_city_repair();
	void e_city_attack();
	
	int type;
	char e;
	city_info_t *cityp;

	cityp = find_city (obj->loc);
	if (!cityp || cityp->owner != USER) {
		(void) beep ();
		return;
	}

	type = get_piece_name();
	if (type == NOPIECE) {
		(void) beep ();
		return;
	}
	
	e = get_chx ();
	
	switch (e) {
	case 'F': /* fill */
		e_city_fill (cityp, type);
		break;
	case 'G': /* explore */
		e_city_explore (cityp, type);
		break;
	case 'I': /* directional stasis */
		e_city_stasis (cityp, type);
		break;
	case 'K': /* turn off function */
		e_city_wake (cityp, type);
		break;
	case 'R': /* make piece move randomly */
		e_city_random (cityp, type);
		break;
	case 'U': /* repair ship */
		e_city_repair (cityp, type);
		break;
	case 'Y': /* set army func to attack */
		e_city_attack (cityp, type);
		break;
	default: /* bad command? */
		(void) beep ();
		break;
	}
}

/*
Change a city's production.
*/

void
user_build (obj)
piece_info_t *obj;
{
	city_info_t *cityp;

	if (user_map[obj->loc].contents != 'O') { /* no user city here? */
		(void) beep ();
		return;
	}
	cityp = find_city (obj->loc);
	ASSERT (cityp != NULL);
	set_prod (cityp);
}

/*
Move a piece in the direction specified by the user.
This routine handles attacking objects.
*/

void
user_dir (obj, dir)
piece_info_t *obj;
int dir;
{
	void user_dir_army(), user_dir_fighter(), user_dir_ship();

	long loc;

	loc = obj->loc + dir_offset[dir];

	if (good_loc (obj, loc)) {
		move_obj (obj, loc);
		return;
	}
	if (!map[loc].on_board) {
		error ("You cannot move to the edge of the world.",0,0,0,0,0,0,0,0);
		delay ();
		return;
	}
	switch (obj->type) {
	case ARMY: user_dir_army (obj, loc); break;
	case FIGHTER: user_dir_fighter (obj, loc); break;
	default: user_dir_ship (obj, loc); break;
	}
}

/*
We have an army that wants to attack something or move onto some
unreasonable terrain.  We check for errors, question the user if
necessary, and attack if necessary.
*/

void
user_dir_army (obj, loc)
piece_info_t *obj;
long loc;
{
	int enemy_killed;
	
	enemy_killed = FALSE;

	if (user_map[loc].contents == 'O') /* attacking own city */
		move_army_to_city (obj, loc);

	else if (user_map[loc].contents == 'T') /* transport full? */
		fatal (obj, loc,
	"Sorry, sir.  There is no more room on the transport.  Do you insist? ",
	"Your army jumped into the briny and drowned.");

	else if (map[loc].contents == '.') { /* going for a swim? */
		if (!getyn ( /* thanks to Craig Hansen for this next message */
	"Troops can't walk on water, sir.  Do you really want to go to sea? "))
		return;

		if (user_map[obj->loc].contents == 'T')
		{
			comment ("Your army jumped into the briny and drowned.",0,0,0,0,0,0,0,0);
			ksend ("Your army jumped into the briny and drowned.\n",0,0,0,0,0,0,0,0);
		}
		else if (user_map[loc].contents == '.')
		{
			comment ("Your army marched dutifully into the sea and drowned.",0,0,0,0,0,0,0,0);
			ksend ("Your army marched dutifully into the sea and drowned.\n",0,0,0,0,0,0,0,0);
		}
		else { /* attack something at sea */
			enemy_killed = islower (user_map[loc].contents);
			attack (obj, loc);
	
			if (obj->hits > 0) /* ship won? */
			{
				comment ("Your army regretfully drowns after its successful assault.",0,0,0,0,0,0,0,0);
				ksend ("Your army regretfully drowns after it's successful assault.",0,0,0,0,0,0,0,0);
			}
		}
		if (obj->hits > 0) {
			kill_obj (obj, loc);
			if (enemy_killed) scan (comp_map, loc);
		}
	}
		
	else if (isupper (user_map[loc].contents)
		&& user_map[loc].contents != 'X') { /* attacking self */
		if (!getyn (
	"Sir, those are our men!  Do you really want to attack them? "))
		return;

		attack (obj, loc);
	}

	else attack (obj, loc);
}

/*
Here we have a fighter wanting to attack something.  There are only
three cases:  attacking a city, attacking ourself, attacking the enemy.
*/

void
user_dir_fighter (obj, loc)
piece_info_t *obj;
long loc;
{
	if (map[loc].contents == '*')
		fatal (obj, loc,
	"That's never worked before, sir.  Do you really want to try? ",
	"Your fighter was shot down.");

	else if (isupper (user_map[loc].contents)) {
		if (!getyn (
	"Sir, those are our men!  Do you really want to attack them? "))
		return;

		attack (obj, loc);
	}

	else attack (obj, loc);
}

/*
Here we have a ship attacking something, or trying to move on
shore.  Our cases are: moving ashore (and subcases), attacking
a city, attacking self, attacking enemy.
*/
	
void
user_dir_ship (obj, loc)
piece_info_t *obj;
long loc;
{
	int enemy_killed;

	enemy_killed = FALSE;

	if (map[loc].contents == '*') {
		(void) sprintf (jnkbuf, "Your %s broke up on shore.",
				piece_attr[obj->type].name);

		fatal (obj, loc,
	"That's never worked before, sir.  Do you really want to try? ",
			jnkbuf);
	}

	else if (map[loc].contents == '+') { /* moving ashore? */
		if (!getyn ("Ships need sea to float, sir.  Do you really want to go ashore? ")) return;

		if (user_map[loc].contents == '+')
		{
			comment1 ("Your %s broke up on shore.", piece_attr[obj->type].name,0,0,0,0,0,0,0);
			ksend1 ("Your %s broke up on shore.", piece_attr[obj->type].name,0,0,0,0,0,0,0);
		}
		else { /* attack something on shore */
			enemy_killed = islower (user_map[loc].contents);
			attack (obj, loc);

			if (obj->hits > 0) /* ship won? */
			{
				comment1 ("Your %s breaks up after its successful assault.", piece_attr[obj->type].name,0,0,0,0,0,0,0);
				ksend1 ("Your %s breaks up after its successful assault.", piece_attr[obj->type].name,0,0,0,0,0,0,0);
			}
		}
		if (obj->hits > 0) {
			kill_obj (obj, loc);
			if (enemy_killed) scan (comp_map, loc);
		}
	}
		
	else if (isupper (user_map[loc].contents)) { /* attacking self */
		if (!getyn (
	"Sir, those are our men!  Do you really want to attack them? "))
		return;

		attack (obj, loc);
	}

	else attack (obj, loc);
}

/*
Here a user wants to move an army to a city.  If the city contains
a non-full transport, we make the move.  Otherwise we ask the user
if she really wants to attack the city.
*/

void
move_army_to_city (obj, city_loc)
piece_info_t *obj;
long city_loc;
{
	piece_info_t *tt;

	tt = find_nfull (TRANSPORT, city_loc);

	if (tt != NULL) move_obj (obj, city_loc);

	else fatal (obj, city_loc,
	"That's our city, sir!  Do you really want to attack the garrison? ",
	"Your rebel army was liquidated.");
}

/*
Cancel automove mode.
*/

void
user_cancel_auto () {
	if (!automove)
		comment ("Not in auto mode!",0,0,0,0,0,0,0,0);
	else {
		automove = FALSE;
		comment ("Auto mode cancelled.",0,0,0,0,0,0,0,0);
	}
}

/*
Redraw the screen.
*/

void
user_redraw () {
	redraw ();
}

/*
Awaken an object if it needs to be.  Normally, objects are awakened
when they are next to an enemy object or an unowned city.  Armies
on troop transports are not awakened if they are surrounded by sea.
We return true if the object is now awake.  Objects are never
completely awoken here if their function is a destination.  But we
will return TRUE if we want the user to have control.
*/

int
awake (obj)
piece_info_t *obj;
{
	int i;
	char c;
	long t;

	if (obj->type == ARMY && vmap_at_sea (user_map, obj->loc)) {
	    obj->moved = piece_attr[ARMY].range;
	    return (FALSE);
	}
	if (obj->func == NOFUNC) return (TRUE); /* object is awake */
	
	if (obj->type == FIGHTER /* wake fighters */
	    && obj->func != LAND /* that aren't returning to base */
	    && obj->func < 0 /* and which don't have a path */
	    && obj->range <= find_nearest_city (obj->loc, USER, &t) + 2) {
		obj->func = NOFUNC; /* wake piece */
		return (TRUE);
	}
	for (i = 0; i < 8; i++) { /* for each surrounding cell */
		c = user_map[obj->loc+dir_offset[i]].contents;

		if (islower (c) || c == '*' || c == 'X') {
			if (obj->func < 0) obj->func = NOFUNC; /* awaken */
			return (TRUE);
		}
	}
	return (FALSE);
}

/*
Question the user about a fatal move.  If the user responds 'y',
print out the response and kill the object.
*/

void
fatal (obj, loc, message, response)
piece_info_t *obj;
long loc;
char *message;
char *response;
{
	if (getyn (message)) {
		comment (response,0,0,0,0,0,0,0,0);
		kill_obj (obj, loc);
	}
}