-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheplayer.d
3924 lines (3384 loc) · 97.8 KB
/
eplayer.d
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
/*
* Empire, the Wargame of the Century (tm)
* Copyright (C) 1978-2004 by Walter Bright
* All Rights Reserved
*
* You may use this source for personal use only. To use it commercially
* or to distribute source or binaries of Empire, please contact
* www.digitalmars.com.
*
* Written by Walter Bright.
* This source is written in the D Programming Language.
* See www.digitalmars.com/d/ for the D specification and compiler.
*
* Use entirely at your own risk. There is no warranty, expressed or implied.
*/
module eplayer;
import empire;
import display;
import path;
import move;
// For each player
struct Player
{
uint num; // player number (1..numply)
uint round; // round number
ubyte *map;
int human; // !=0 if human player
ubyte watch; // display attribute DAxxxx if non-zero
int movedone; // !=0 if we moved the piece this turn
int uninum; // what unit number we're on
int secflg; // if next unit has to be in current sector
ubyte defeat; // true if player is defeated
Display *display;
int turns; // number of turns completed
static Player *get(int num) { return &player[num]; }
// Human player
Unit *usv; // current unit pointer
int mode; // mdXXXX: input modes
loc_t curloc; // current location of cursor
int frmloc; // use when in TO mode
int maxrng;
int citnum;
int savmod;
int nrdy; // true if we're not ready
int modsave;
// Computer strategy
ubyte target[CITMAX]; // There is a TARGET byte for each city.
// If the computer knows about the city
// but doesn't own it, it is true.
uint troopt[6][5]; // The 6 rows correspond to the ships
// DTSRCB in that order. The 5 columns
// correspond to locations of enemy ships
// discovered, in order from newest to
// oldest sighting.
uint loci[LOCMAX]; // Locations of enemy armies sighted,
// from most to least recent.
uint numuni[TYPMAX]; // # of units of each type
uint numown; // # of our owned cities
uint numtar; // # of cities listed as targets
uint numphs[TYPMAX]; // # of cities producing each type of unit
/*************************************
* Give time slice to a player.
*/
void tslice()
{ int i;
Unit *u;
Player *p = this;
if (!p.human)
{ int x;
x = p.display.text.TTinr();
switch (x)
{ case 3:
done(1);
break;
case 'S':
cwatch();
break;
case 'O':
do
p = p.nextp();
while (p.human);
exchange_display(p);
return;
default:
break;
}
}
if (numleft == 1)
return;
// Loop through all the units making sure that each unit moves
// once per round.
for (; p.uninum < unitop; p.uninum++)
{
i = p.uninum; // get unit number
u = &unit[i];
if (u.mov || // if unit has already moved
!u.loc || // if unit doesn't exist
u.own != p.num) // if the unit isn't ours
continue;
if (p.secflg && // if move by sector and
!p.display.insect(u.loc,2) && // not in current sector and
p.movedone) // previous move was completed
continue;
if (p.watch)
p.secflg = true; // back to moving by sector
p.movedone = p.Mmove(u); // move the unit
if (p.movedone)
u.mov = true; // indicate that it's moved
return;
}
// We've moved all the units for this round that are in this sector.
p.uninum = 0; // reset
if (p.secflg) // if only in sector showing
{ p.secflg = false; // try anybody
return;
}
// We've moved all the units for this round.
if (p.watch) // only by sector if we're watching
p.secflg = true; // back to moving by sector
for (i = unitop; i--;)
{ u = &unit[i];
if (u.own == p.num)
u.mov = false; // reset all the unimov entries
}
finrnd(); // finish up round
}
/*******************
* Finish up the round for this player.
*/
void finrnd()
{
if (!human) // if computer player
cityph(); // adjust city phases as req'd
display.remove_sticky(); // remove any 'sticky' messages
hrdprd(this); // hardware production
chkwin(); // see if anybody won
round++; // next round
for (int i = 1; i <= numply; i++)
Player.get(i).notify_round(this,round); // type out the round #
display.text.flush();
}
static int locold; // previous loc of unit
static int snsflg; // set if do sensor for enemy
/***************************
* Perform a move for a unit. Return true if
* move was successfully completed.
*/
int Mmove(Unit *u)
{ dir_t r2;
int e;
Player *p = this;
do
{ p.sensor(u.loc); // get up to date before move
if (p.human) // if human player
{ if (!p.hmove(u,&r2)) // do human move
return 0; // not ready
}
else // computer move
{ if (!p.cmove(u,&r2))
return 0; // not ready
}
/*
* see if unit was destroyed while in cmove or hmove
*/
if (!u.loc || u.own != p.num) // unit was destroyed
break;
debug chkmov(r2); // check for legit move
assert(chkloc(u.loc)); // check for valid location
e = p.evalu8(u,r2);
} while (e);
p.turns = 0; // reset
return 1; // done with this piece
}
/*********************************
* Evaluate the move.
* Input:
* uninum = unit number
* r2 = move
* Returns:
* true if we get another move
*/
int evalu8(Unit *u,dir_t r2)
{
loc_t loc = u.loc; // location of unit
int type = u.typ; // what type of unit we have
int ab = .map[loc]; // what's there
int ac;
Player *p = this;
Display *d = p.display;
Text *t = &d.text;
/*
* perform the move
*/
locold = loc; // remember for drag
snsflg = 0; // don't do sensor for enemy
updlst(loc,type); // fixup map location left
loc += arrow(r2); // move to new loc
ac = .map[loc]; // map value of where we are
u.loc = loc; // update unit location
/*
* Watch out for an A on a T attempting to attack a ship
*/
if (type == A && sea[ac] &&
.typ[ab] == T && r2 != -1)
{ d.drown(u); // can't do this!
killit(u);
return false;
}
/*
* perform battles as req'd, watch for A on T or F on C
*/
if (.typ[ac] >= A) // if ac is a unit
{ if (.own[ac] == p.num) // if we own the piece
{ if (type == A && .typ[ac] == T)
{ if (.typ[ab] != T)
{ d.boarding(u); // if A boarding a transport
eomove(u.loc);
}
return false;
}
if (type == F && .typ[ac] == C)
{ if (.typ[ab] != C)
{
// if F landing on a carrier
u.hit = typx[F].hittab; // reset range of F
d.landing(u);
eomove(u.loc);
}
return false;
}
}
else
snsflg = .own[ac]; // do sensor for enemy
if (fight(u,loc)) // if we fight & lose
{ killit(u); // remove the carcass
return false; // all done
}
ac = updmap(loc); // fix up map
}
/*
* take care of special stuff for armies
*/
if (type == A) // if army
{ if (ac == MAPsea) // if moving onto sea
{ d.drown(u); // drown him
killit(u);
}
else if (ac == MAPland) // if moving onto land
{ change(type,loc,ac); // update map loc
eomove(loc); // do end of move processing
}
else
{
assert(.typ[ac] == X);
attcit(loc); // then must be attacking a city
killit(u); // always destroyed
}
return false; // all done
}
/*
* take care of special stuff for fighters
*/
if (type == F) // if fighter
{ if (.typ[ac] == X) // if moving onto a city
{ if (.own[ac] == p.num) // if the city is ours
{
// land the plane
u.hit = typx[F].hittab; // reset range of F
d.landing(u);
eomove(u.loc);
}
else // unowned city
{
d.shot_down(u);
killit(u); // a fatal error
}
return false;
}
else // moving onto sea or land
{ if (--u.hit) // if not run out of fuel yet
change(type,loc,ac); // good move
else // ran out of fuel
{
d.no_fuel(u);
killit(u);
return false;
}
}
}
/*
* take care of ships
*/
if (type >= D) // take care of ships
{ if (ac == MAPsea) // if moving onto sea
change(type,loc,ac); // then fix map & we're done
else // ran aground or docked
{ if (.own[ac] != p.num) // if not owned
{ d.aground(u); // ran aground
killit(u);
return false;
}
if (u.hit < typx[type].hittab)
u.hit++; // ship in port, repair it
d.docking(u, loc);
}
}
eomove(loc); // do ending sensor probes
switch (type)
{
case F:
if (u.hit % 4 == 0)
return false; // if fighter has moved 4
break;
case T:
case C:
p.drag(u); // drag along As and Fs
case D:
case S:
case R:
case B:
if (p.turns || // if already got extra move
u.hit <= typx[type].hittab/2) // or half damaged
return false;
break;
default:
assert(0);
}
if (.typ[ac] == X) // if in city
return false; // then no extra moves
p.turns++; // # of turns completed
return true; // get another move
}
/****************************************
* Attack city at loc and determine outcome.
*/
void attcit(loc_t loc)
{ int ab = .map[loc];
Player *patt = this;
Player *pdef = Player.get(own[ab]);
City *c;
assert(loc < MAPSIZE);
c = fndcit(loc);
if (patt == pdef)
patt.display.city_attackown();
else if (ranq() & 1) // 50% chance of takeover
{
int mapval; // city map value
int i;
pdef.display.city_conquered(loc);
patt.display.city_subjugated();
pdef.notify_city_lost(c);
assert(c.own == pdef.num);
mapval = 4 + 10 * (patt.num - 1); // map val of conquered city
assert(.own[mapval] == patt.num);
.map[loc] = mapval; // set reference map
snsflg = c.own; // !=0 if do sensor for enemy
c.own = patt.num; // set new owner
/* Destroy any enemy pieces in the city.
*/
for (i = 0; i < unitop; i++)
{ Unit *u = &unit[i];
if (u.loc == loc && u.own != patt.num)
{
assert(u.own == pdef.num);
pdef.notify_destroy(u);
u.destroy();
}
}
c.phs = -1; // select new phase
patt.notify_city_won(c);
}
else
{
pdef.notify_city_repelled(c);
pdef.display.city_repelled(loc); // invasion was repelled
patt.display.city_crushed(); // assault was crushed
}
}
/*********************************
* Type out sector indicated by upper left corner loc.
*/
void sector(loc_t loc)
{
Player *p = this;
Display *d = p.display;
Text *t = &p.display.text;
if (!t.watch)
return; // not watching this player
//if (loc >= MAPSIZE) PRINTF("sector(loc = %d)\n", loc);
assert(loc < MAPSIZE);
if (loc == d.secbas)
return; // this sector is already showing
global.player = p;
global.ulcorner = loc;
global.map = map;
global.offsetx = 0;
global.offsety = 0;
d.secbas = loc; // set new sector base
invalidateSector();
}
/*****************************
* Do sensor probe around loc. Update player maps, screen and
* computer player variables. If an enemy is detected, call sensor
* for him also.
*/
static int dirtab[9] = [3,2,1,4,-1,0,5,6,7]; // to minimize chars
// sent to screen
void sensor(loc_t loc)
{
int i,r2,o;
uint z6;
ubyte pab,rab;
Player *p = this;
debug assert(chkloc(loc));
for (i = 9; i--;) // look at 9 directions
{ r2 = dirtab[i]; // get direction
z6 = loc + arrow(r2); // get new location
pab = map[z6]; // get player map value
rab = .map[z6]; // and reference map value
if (pab != rab) // if there is a change
{ map[z6] = rab; // update player map
if (p.watch)
p.display.mapprt(z6); // print map value on screen
if (!p.human)
p.updcmp(z6); // update computer strat variables
}
/* Check to see if it's an enemy piece or city. If so, do a sensor
* probe about this loc for the enemy.
*/
o = own[rab];
if (o && o != p.num)
{
Player *pe = Player.get(o);
pab = pe.map[loc];
rab = .map[loc];
if (pab != rab) // if there is a change
{ pe.map[loc] = rab; // update player map
if (pe.watch)
pe.display.mapprt(loc); // print map value on screen
if (!pe.human)
pe.updcmp(loc); // update computer strat variables
}
}
}
}
/*******************************
* Center the sector about loc.
*/
void center(loc_t loc)
{ int row,col,rowsize,colsize,size;
Player *p = this;
Display *d = p.display;
row = ROW(loc);
col = COL(loc);
size = d.Smax - d.Smin; // display size
rowsize = size >> 8; // # of rows - 1
colsize = size & 0xFF;
row -= rowsize / 2;
col -= colsize / 2;
if (row < 0) row = 0;
if (row > Mrowmx - rowsize) row = Mrowmx - rowsize;
if (col < 0) col = 0;
if (col > Mcolmx - colsize) col = Mcolmx - colsize;
p.sector(row * (Mcolmx + 1) + col); // type new sector
}
/*******************************
* Select initial city for player.
*/
void citsel()
{ int n;
loc_t loc;
City *c;
Player *p = this;
do
{ n = empire.random(CITMAX); // select a city at random
c = &city[n];
loc = c.loc;
}
while (!loc || // if city doesn't exist or
edger(loc) == 8 || // island city or
c.own); // already owned
c.own = p.num; // claim the city
.map[loc] = 4 + (p.num - 1) * 10; // set map value
p.sensor(loc); // do a sensor probe
if (p.human) // if human player
p.phasin(c); // get city phase
else // else computer player
p.cphasin(c);
}
/* ===================== Human strategy ===================================== */
/**********************************
* Get move from player.
* Watch out for unit being destroyed while in tty input wait for a move.
* Input:
* u unit number
* pr2 -> where output move is to go
* Output:
* *pr2 move selected (valid only if false is returned)
* Returns:
* 0 move not completed
* !=0 move successfully completed
*/
int hmove(Unit *u,dir_t *pr2)
{ loc_t oldloc;
int cmd;
Player *p = this;
Display *d = p.display;
Text *t = &d.text;
assert(u.loc);
assert(u.own == p.num);
if (u != p.usv)
p.nrdy = 0; // it's a different unit!
if (p.nrdy == 1) goto cmdin; // get command
if (p.nrdy == 2) goto dirin; // get direction in cmdI
bhmove:
p.usv = u; // remember unit number
*pr2 = -1; // default no move
if (sursea(u)) return 1; // if A on T at sea
if (p.mycode(u,pr2)) // if automatic move
{
t.flush();
done: p.setmode(mdNONE);
p.chksleep(u,*pr2); // see if we put it to sleep
t.speaker_click();
return 1;
}
/* Enter movement mode.
*/
movmod:
p.curloc = u.loc; // set current location
d.headng(u); // print heading
if (!d.insect(p.curloc,2)) // if not in current sector
center(p.curloc); // then print out the sector
p.setmode(mdMOVE); // put in move mode
goto cmdscn;
/* Bad command
*/
cmderr:
cmderror();
/* Command scanner
*/
cmdscn:
p.sensor(u.loc); // bring map up to date
cmdin:
d.pcur(p.curloc); // position cursor
if ((cmd = t.TTinr()) == -1) // if no input from tty
{ p.nrdy = 1;
return 0; // not ready
}
p.nrdy = 0; // reset flag
/* Evaluate result if it's a direction command.
*/
oldloc = p.curloc; // remember
if (cmdcur(&p.curloc,cmd,pr2)) // if direction command
{ if (p.curloc == oldloc) // if bad direction command
goto cmderr; // then error
if (p.mode == mdMOVE) // if in move mode
{ if (!p.seeifok(u,*pr2) && // if move is destructive
!p.rusure()) // and he backs out
{ p.curloc = u.loc;
goto cmdscn; // give him another chance
}
goto done; // we're done
}
if (p.mode == mdTO) // if in TO mode
{ if (dist(p.curloc,p.frmloc) > p.maxrng)
{ p.curloc = oldloc;
goto cmderr; // too far away
}
}
p.typhdg(); // update heading
goto cmdscn;
}
/* Check for command in our table.
*/
switch (cmd)
{ default:
goto cmderr; // no defaults!
case 3:
done(1);
case ' ': // stay put
*pr2 = -1;
if (p.mode == mdMOVE)
{ if (!p.seeifok(u,*pr2) && // if move is destructive
!p.rusure()) // and he backs out
goto cmdscn; // give him another chance
goto done; // only allowed in move mode
}
cmderror();
break;
case 'F': // from
p.cmdF(u); break;
case 'G': // goto nearest city/carrier
if (p.cmdG(u))
goto bhmove; // if ifo and ila were changed
break;
case 'H': // 20 free moves to enemy
if (!p.rusure())
goto cmdin; // give him a chance to back out
p.setmode(mdNONE);
p.round += 20; // use this as our mechanism
return 0; // not ready
case 'I': // direction
if (!p.valid(u) || p.mode == mdTO)
{ cmderror();
break;
}
p.modsave = p.mode;
p.setmode(mdDIR); // new mode
dirinp: d.pcur(p.curloc); // position cursor
dirin: if ((cmd = t.TTinr()) == -1)
{ p.nrdy = 2;
return 0; // player is not ready
}
p.nrdy = 0; // reset flag
if (cmd == ESC) // if abort
{ p.setmode(p.modsave);
break;
}
oldloc = p.curloc;
if (!cmdcur(&oldloc,cmd,pr2) || oldloc == p.curloc)
{ cmderror();
goto dirinp; // try again
}
p.setmode(p.modsave); // back to old mode
if (p.mycmod(u,fnDI,*pr2))
goto bhmove; // back to beginning
break;
case 'J': // toggle sound on/off
t.speaker ^= true; break;
case 'K': // wake up
p.cmdK(u); break;
case 'L': // load armies
if (p.cmdL(u))
goto bhmove; // if ifo and ila were changed
break;
case 'N': // new screen
center(p.curloc);
break;
case 'P': // production phase
p.cmdP(); break;
case 'R': // random
if (p.mycmod(u,fnRA,0))
goto bhmove; // back to beginning
break;
case 'S': // sentry
if (.typ[map[p.curloc]] == X)
goto cmderr; // can't put in sentry in a city
if (p.mycmod(u,fnSE,0))
goto bhmove;
break;
case 'T': // to
if (p.cmdT(u))
goto bhmove; // if ifo and ila were changed
break;
case 'U': // wake up units aboard
p.cmdU(u); break;
case 'V': // save game
if (p.mode != mdMOVE)
goto cmderr; // only in move mode
savgam(); // save game next time around
return 0; // move not completed
case 'Y': // enter survey mode
if (p.mode == mdSURV)
goto cmderr; // allready in survey mode
p.setmode(mdSURV); // enter survey mode
p.typhdg();
break;
case ESC:
if (p.mode == mdMOVE)
goto cmderr; // already in move mode
goto movmod; // return to move mode
case '<': // decrease time delay
d.timeinterval = (d.timeinterval < 1)
? 0 : d.timeinterval - 1;
break;
case '>': // increase time delay
d.timeinterval++;
break;
} // switch (cmd)
goto cmdscn;
} // hmove
/********************************
* Process the 'F' command.
*/
void cmdF(Unit *u)
{ int ab,md;
Player *p = this;
md = p.mode; // shorthand
if (md == mdTO) goto err;
ab = .map[p.curloc];
p.maxrng = 100; // default unless fighter
p.citnum = -1; // default unless from city
if (!p.valid(u) || // if not a unit
(md == mdSURV && typ[ab] == X)) // or we're sitting on a city
{ if (!p.cittst()) // if not an owned city
goto err;
p.maxrng = typx[F].hittab; // set to max fighter range
p.citnum = fndcit(p.curloc) - &city[0]; // find city #
}
else if (p.curloc != u.loc ||
(md == mdSURV && u.typ == F && typ[ab] == C))
{ Unit *ui;
ui = fnduni(p.curloc); // get unit number
if (ui.typ == F) // if it's a fighter
p.maxrng = ui.hit; // set max range
}
else
{ if (u.typ == F) // if it's a fighter
p.maxrng = u.hit;
}
p.savmod = p.mode; // save current mode
p.setmode(mdTO); // switch to TO mode
p.frmloc = p.curloc; // set from location
return;
err:
cmderror();
}
/***********************************
* Command 'G'.
* Find the nearest city or carrier we can fly to. Works
* for setting fipath[]s also.
* Returns:
* true if we modified the ifo and ila of the unum unit.
*/
int cmdG(Unit *u)
{ int md,cloc,i,mindist,minloc;
Player *p = this;
md = p.mode;
if (md == mdTO) goto err;
cloc = p.curloc;
/* First find nearest city.
*/
mindist = typx[F].hittab + 1; // we want one within range
for (i = CITMAX; i--;)
{ if (city[i].own == p.num && // if we own the city and
cloc != city[i].loc && // we're not already there and
city[i].loc && // the city exists and
dist(cloc,city[i].loc) < mindist)
{ minloc = city[i].loc;
mindist = dist(cloc,minloc);
}
}
/* Look for a closer carrier.
*/
for (i = unitop; i--;)
{ if (unit[i].typ == C && // if it's a carrier and
unit[i].own == p.num && // we own it and
unit[i].loc && // it exists and
cloc != unit[i].loc && // we're not already there and
dist(cloc,unit[i].loc) < mindist)
{ minloc = unit[i].loc;
mindist = dist(cloc,minloc);
}
}
if (mindist == typx[F].hittab + 1) goto err; // if we failed
if (md == mdMOVE)
{ if (u.typ != F) goto err;
if (u.hit < mindist) goto err; // if out of range
return p.mycmod(u,fnMO,minloc);
}
if (p.cittst()) // if we set fipath[]
{ fndcit(cloc).fipath = minloc;
p.typhdg();
return false;
}
if (p.valid(u)) // if valid unit
{ Unit *ui = fnduni(cloc); // find the unit number
if (ui.typ != F ||
ui.hit < mindist)
goto err;
return p.mycmod(u,fnMO,minloc);
}
err:
cmderror();
return false;
}
/***********************************
* Wake up unit if on a unit, clear fipath[]
* if on a city.
*/
void cmdK(Unit *u)
{
Player *p = this;
if (p.mode == mdMOVE)
{ p.mycmod(u,fnAW,0);
return;
}
if (p.cittst()) // if we're on a valid city
{ fndcit(p.curloc).fipath = 0; // zero out fipath
p.typhdg();
return;
}
if (p.valid(u)) // if a valid unit
p.mycmod(u,fnAW,0); // wake up unit
else
cmderror();
}
/***********************************
* Process 'L' command.
* Load armies/fighters on transports/carriers.
* Don't allow it if he's in a city.
* Returns:
* true if we modified the ifo and ila of the unum unit.
*/
int cmdL(Unit *u)
{ int type,ab;
Player *p = this;
ab = .map[p.curloc];
if (own[ab] == p.num && // if we own the unit and
(typ[ab] == T || typ[ab] == C)) // it's a transport or carrier
return p.mycmod(u,fnFI,0); // put in fill mode
cmderror();
return false;
}
/***********************************
* Enter new city production phase.
*/
void cmdP()
{
Player *p = this;
if (p.mode != mdSURV) // only allowed in survey mode
goto err;
if (!p.cittst()) goto err; // not a valid city
p.setmode(mdPHAS);
p.phasin(fndcit(p.curloc)); // get new phase for city
p.setmode(mdSURV); // back to survey mode
return;
err:
cmderror();
}
/***********************************
* Process 'T' command.
* Returns:
* true if we modified the ifo and ila of the unum unit.
*/
int cmdT(Unit *u)
{ int ila;
Player *p = this;
if (p.mode != mdTO) // if not in TO mode
{ cmderror();
return false;
}
p.setmode(p.savmod); // back to previous mode
ila = p.curloc;
p.curloc = p.frmloc; // set cursor back
if (p.citnum == -1) // if it wasn't a city
return p.mycmod(u,fnMO,ila); // set new function for device
else // else it was a city
{ city[p.citnum].fipath = ila;
p.typhdg();
return false;
}
}
/***********************************
* Wake up units aboard.
*/
void cmdU(Unit *u)
{ int i,type;