-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathedit.c
644 lines (547 loc) · 10.9 KB
/
edit.c
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
/* %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.
*/
/*
edit.c -- Routines to handle edit mode commands.
*/
#ifdef SYSV
#include <string.h>
#else
#include <strings.h>
#endif
#include <curses.h>
#include <ctype.h>
#include "empire.h"
#include "extern.h"
void e_move(long *path_start, long loc);
void
edit(edit_cursor)
long edit_cursor;
{
char e_cursor();
void e_leave(), e_print(), e_random();
void e_stasis(), e_end(), e_wake(), e_sleep();
void e_info(), e_prod(), e_help(), e_explore();
void e_fill(), e_land(), e_city_func(), e_transport();
void e_attack(), e_repair();
long path_start;
int path_type;
char e;
path_start = -1; /* not building a path yet */
for (;;) { /* until user gives command to leave */
display_loc_u (edit_cursor); /* position cursor */
e = e_cursor (&edit_cursor); /* handle cursor movement */
switch (e) {
case 'B': /* change city production */
e_prod (edit_cursor);
break;
case 'F': /* fill */
e_fill (edit_cursor);
break;
case 'G': /* explore */
e_explore (edit_cursor);
break;
case 'H': /* help */
e_help ();
break;
case 'I': /* directional stasis */
e_stasis (edit_cursor);
break;
case 'K': /* wake up anything and everything */
e_wake (edit_cursor);
break;
case 'L': /* land plane */
e_land (edit_cursor);
break;
case 'M': /* start move to location */
path_type = NOPIECE;
e_move (&path_start, edit_cursor);
break;
case 'N': /* end move to location */
e_end (&path_start, edit_cursor, path_type);
break;
case 'O': /* leave display mode */
e_leave ();
return;
case 'P': /* print new sector */
e_print (&edit_cursor);
break;
case 'R': /* make piece move randomly */
e_random (edit_cursor);
break;
case 'S': /* sleep */
e_sleep (edit_cursor);
break;
case 'T': /* transport army */
e_transport (edit_cursor);
break;
case 'U': /* repair ship */
e_repair (edit_cursor);
break;
case 'V': /* set city function */
e_city_func (&path_start, edit_cursor, &path_type);
break;
case 'Y': /* set army func to attack */
e_attack (edit_cursor);
break;
case '?': /* request info */
e_info (edit_cursor);
break;
case '\014': /* control-L */
redraw ();
break;
default: /* bad command? */
huh ();
break;
}
}
}
/*
Get the next command. We handle cursor movement here.
This routine is an attempt to make cursor movement reasonably
fast.
*/
static char dirchars[] = "WwEeDdCcXxZzAaQq";
char
e_cursor (edit_cursor)
long *edit_cursor;
{
char e;
char *p;
/* set up terminal */
(void) crmode ();
(void) refresh ();
e = getch ();
topini (); /* clear any error messages */
for (;;) {
p = strchr (dirchars, e);
if (!p) break;
if (!move_cursor (edit_cursor, dir_offset[(p-dirchars) / 2]))
(void) beep ();
(void) refresh ();
e = getch ();
}
(void) nocrmode (); /* reset terminal */
if (islower (e)) e = upper (e);
return e;
}
/*
Leave edit mode.
*/
void
e_leave () {
}
/*
Print new sector.
*/
void
e_print (edit_cursor)
long *edit_cursor;
{
int sector;
sector = get_range ("New Sector? ", 0, NUM_SECTORS-1);
/* position cursor at center of sector */
*edit_cursor = sector_loc (sector);
sector_change (); /* allow change of sector */
}
/*
Set the function of a piece.
*/
void
e_set_func (loc, func)
long loc;
long func;
{
piece_info_t *obj;
obj = find_obj_at_loc (loc);
if (obj != NULL && obj->owner == USER) {
obj->func = func;
return;
}
huh (); /* no object here */
}
/* Set the function of a city for some piece. */
void
e_set_city_func (cityp, type, func)
city_info_t *cityp;
int type;
long func;
{
cityp->func[type] = func;
}
/*
Set a piece to move randomly.
*/
void
e_random (loc)
long loc;
{
e_set_func (loc, RANDOM);
}
void
e_city_random (cityp, type)
city_info_t *cityp;
int type;
{
e_set_city_func (cityp, type, RANDOM);
}
/*
Put a ship in fill mode.
*/
void
e_fill (loc)
long loc;
{
if (user_map[loc].contents == 'T' || user_map[loc].contents == 'C')
e_set_func (loc, FILL);
else huh ();
}
void
e_city_fill (cityp, type)
city_info_t *cityp;
int type;
{
if (type == TRANSPORT || type == CARRIER)
e_set_city_func (cityp, type, FILL);
else huh ();
}
/*
Set a piece to explore.
*/
void
e_explore (loc)
long loc;
{
e_set_func (loc, EXPLORE);
}
void
e_city_explore (cityp, type)
city_info_t *cityp;
int type;
{
e_set_city_func (cityp, type, EXPLORE);
}
/*
Set a fighter to land.
*/
void
e_land (loc)
long loc;
{
if (user_map[loc].contents == 'F')
e_set_func (loc, LAND);
else huh ();
}
/*
Set an army's function to TRANSPORT.
*/
void
e_transport (loc)
long loc;
{
if (user_map[loc].contents == 'A')
e_set_func (loc, WFTRANSPORT);
else huh ();
}
/*
Set an army's function to ATTACK.
*/
void
e_attack (loc)
long loc;
{
if (user_map[loc].contents == 'A')
e_set_func (loc, ARMYATTACK);
else huh ();
}
void
e_city_attack (cityp, type)
city_info_t *cityp;
int type;
{
if (type == ARMY)
e_set_city_func (cityp, type, ARMYATTACK);
else huh ();
}
/*
Set a ship's function to REPAIR.
*/
void
e_repair (loc)
long loc;
{
if (strchr ("PDSTBC", user_map[loc].contents))
e_set_func (loc, REPAIR);
else huh ();
}
void
e_city_repair (cityp, type)
city_info_t *cityp;
int type;
{
if (type == ARMY || type == FIGHTER || type == SATELLITE)
huh ();
else e_set_city_func (cityp, type, REPAIR);
}
/*
Set object to move in a direction.
*/
static char dirs[] = "WEDCXZAQ";
void
e_stasis (loc)
long loc;
{
char e;
char *p;
if (!isupper (user_map[loc].contents)) huh (); /* no object here */
else if (user_map[loc].contents == 'X') huh ();
else {
e = get_chx(); /* get a direction */
p = strchr (dirs, e);
if (p == NULL) huh ();
else e_set_func (loc, (long)(MOVE_N - (p - dirs)));
}
}
void
e_city_stasis (cityp, type)
city_info_t *cityp;
int type;
{
char e;
char *p;
e = get_chx(); /* get a direction */
p = strchr (dirs, e);
if (p == NULL) huh ();
else e_set_city_func (cityp, type, (long)(MOVE_N - (p - dirs)));
}
/*
Wake up anything and everything.
*/
void
e_wake (loc)
long loc;
{
city_info_t *cityp;
piece_info_t *obj;
int i;
cityp = find_city (loc);
if (cityp != NULL) {
for (i = 0; i < NUM_OBJECTS; i++)
cityp->func[i] = NOFUNC;
}
for (obj = map[loc].objp; obj != NULL; obj = obj->loc_link.next)
obj->func = NOFUNC;
}
void
e_city_wake (cityp, type)
city_info_t *cityp;
int type;
{
e_set_city_func (cityp, type, NOFUNC);
}
/*
Set a city's function. We get the piece type to set, then
the function itself.
*/
void
e_city_func (path_start, loc, path_type)
long *path_start;
long loc;
int *path_type;
{
int type;
char e;
city_info_t *cityp;
cityp = find_city (loc);
if (!cityp || cityp->owner != USER) {
huh ();
return;
}
type = get_piece_name();
if (type == NOPIECE) {
huh ();
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 'M': /* start move to location */
*path_type = type;
e_move (path_start, loc);
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? */
huh ();
break;
}
}
/*
Beginning of move to location.
*/
void
e_move (path_start, loc)
long *path_start;
long loc;
{
if (!isupper(user_map[loc].contents)) huh (); /* nothing there? */
else if (user_map[loc].contents == 'X') huh (); /* enemy city? */
else *path_start = loc;
}
/*
End of move to location.
*/
void
e_end (path_start, loc, path_type)
long *path_start;
long loc;
int path_type;
{
city_info_t *cityp;
if (*path_start == -1) huh (); /* no path started? */
else if (path_type == NOPIECE) e_set_func (*path_start, loc);
else {
cityp = find_city (*path_start);
ASSERT (cityp);
e_set_city_func (cityp, path_type, loc);
}
*path_start = -1; /* remember no path in progress */
}
/*
Put a piece to sleep.
*/
void
e_sleep (loc)
long loc;
{
if (user_map[loc].contents == 'O') huh (); /* can't sleep a city */
else e_set_func (loc, SENTRY);
}
/*
Print out information about a piece.
*/
void
e_info (edit_cursor)
long edit_cursor;
{
void e_city_info(), e_piece_info();
char ab;
ab = user_map[edit_cursor].contents;
if (ab == 'O') e_city_info (edit_cursor);
else if (ab == 'X' && debug) e_city_info (edit_cursor);
else if ((ab >= 'A') && (ab <= 'T'))
e_piece_info (edit_cursor, ab);
else if ((ab >= 'a') && (ab <= 't') && (debug))
e_piece_info (edit_cursor, ab);
else huh ();
}
/*
Print info about a piece.
*/
void
e_piece_info (edit_cursor, ab)
long edit_cursor;
char ab;
{
piece_info_t *obj;
int type;
char *p;
ab = upper (ab);
p = strchr (type_chars, ab);
type = p - type_chars;
obj = find_obj (type, edit_cursor);
ASSERT (obj != NULL);
describe_obj (obj);
}
/*
Display info on a city.
*/
void
e_city_info (edit_cursor)
long edit_cursor;
{
piece_info_t *obj;
city_info_t *cityp;
int f, s;
char func_buf[STRSIZE];
char temp_buf[STRSIZE];
char junk_buf2[STRSIZE];
error (0,0,0,0,0,0,0,0,0); /* clear line */
f = 0; /* no fighters counted yet */
for (obj = map[edit_cursor].objp; obj != NULL;
obj = obj->loc_link.next)
if (obj->type == FIGHTER) f++;
s = 0; /* no ships counted yet */
for (obj = map[edit_cursor].objp; obj != NULL;
obj = obj->loc_link.next)
if (obj->type >= DESTROYER) s++;
if (f == 1 && s == 1)
(void) sprintf (jnkbuf, "1 fighter landed, 1 ship docked");
else if (f == 1)
(void) sprintf (jnkbuf, "1 fighter landed, %d ships docked", s);
else if (s == 1)
(void) sprintf (jnkbuf, "%d fighters landed, 1 ship docked", f);
else (void) sprintf (jnkbuf, "%d fighters landed, %d ships docked", f, s);
cityp = find_city (edit_cursor);
ASSERT (cityp != NULL);
*func_buf = 0; /* nothing in buffer */
for (s = 0; s < NUM_OBJECTS; s++) { /* for each piece */
if (cityp->func[s] < 0)
(void) sprintf (temp_buf, "%c:%s; ",
piece_attr[s].sname,
func_name[FUNCI(cityp->func[s])]);
else (void) sprintf (temp_buf, "%c: %d;",
piece_attr[s].sname,
cityp->func[s]);
(void) strcat (func_buf, temp_buf);
}
(void) sprintf (junk_buf2,
"City at location %d will complete %s on round %d",
cityp->loc,
piece_attr[cityp->prod].article,
date + piece_attr[cityp->prod].build_time - cityp->work);
info (junk_buf2, jnkbuf, func_buf);
}
/*
Change city production.
*/
void
e_prod (loc)
long loc;
{
city_info_t *cityp;
cityp = find_city (loc);
if (cityp == NULL) huh (); /* no city? */
else set_prod (cityp);
}
/*
get help
*/
void
e_help () {
help (help_edit, edit_lines);
prompt ("Press any key to continue: ",0,0,0,0,0,0,0,0);
(void) get_chx ();
}