-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathempire-user_move.adb
1081 lines (969 loc) · 35.4 KB
/
empire-user_move.adb
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
with Empire.Attack;
with Empire.Editing;
with Empire.Locations;
with Empire.Mapping;
with Empire.Math;
with Empire.Objects;
with Empire.Ui;
-- use Empire.Strings; -- XXX for '&'
package body Empire.User_Move is
procedure User_Move is
Sec, Sec_Start : Location_T;
Obj, Next_Obj : Piece_Info_P;
Prod : Piece_Choice_T;
begin
-- 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 (XXX but we don't --
-- Jim).
for N in Piece_Type_T'Range loop
Obj := User_Obj (N);
while Obj /= null loop
Obj.Moved := 0;
Objects.Scan (USER, Obj.Loc); -- refresh user's view of world
Obj := Obj.Links (Piece_Link).Next;
end loop;
end loop;
-- produce new hardware
for I in City'Range loop
if City (I).Owner = USER then
Objects.Scan (USER, City (I).Loc);
Prod := City (I).Prod;
if Prod = NOPIECE then
Objects.Ask_Prod (City (I)); -- ask user what to produce
elsif City (I).work >= Piece_Attr (Prod).Build_Time then
Ui.Info
("City at " &
Location_T'Image (City (I).Loc) &
" has compeleted " &
Strings.To_String (Piece_Attr (Prod).Article));
Objects.Produce (City (I));
else
Ui.Debug_Info
("city at " &
Location_T'Image (City (I).Loc) &
" has completed " &
Integer'Image (City (I).work) &
" work units.");
City (I).work := City (I).work + 1;
end if;
end if;
end loop;
-- move all satellites (they're not user-controled)
Obj := User_Obj (SATELLITE);
while Obj /= null loop
-- cache next satellite, as the satellite may run out of fuel and
-- burn (losing its linkage in Kill_Obj)
Next_Obj := Obj.Links (Piece_Link).Next;
Objects.Move_Sat (Obj);
Obj := Next_Obj;
end loop;
Sec_Start := Ui.Cur_Sector; -- get currently displayed sector.
Sec := Sec_Start;
-- loop through sectors, moving every piece in the sector
loop
for N in Move_Order'Range loop
Ui.Debug_Info
("about to move all pieces of type " &
Piece_Type_T'Image (Move_Order (N)));
-- note that this means that move_order is obeyed only within
-- each sector it would make a better simulation, but a worst UI,
-- to reverse the nesting of the loops bonus question: can this
-- be gamed (say to move a slow unit at the edge of a sector
-- early?) a: probably not to advantage. could be a disadvantage
-- (transport has to move before fighter sweep, eg)
Obj := User_Obj (Move_Order (N));
while Obj /= null loop
-- cache next object, as this object may die in the mean time
Next_Obj := Obj.Links (Piece_Link).Next;
if Obj.Moved = 0 then
if Locations.Loc_Sector (Obj.Loc) = Sec then
Piece_Move (Obj);
end if;
end if;
if Ui.Cur_Sector = Sec then
Ui.Print_Sector (USER, Sec);
end if;
Obj := Next_Obj;
end loop;
end loop;
if Sec = Sector_T'Last then
Sec := Sector_T'First;
else
Sec := Sector_T'Succ (Sec);
end if;
exit when Sec = Sec_Start; -- we've come full circle
end loop;
end User_Move;
-- 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.
procedure Piece_Move (Obj : in Piece_Info_P) is
Changed_Loc : Boolean := False;
Need_Input : Boolean := False;
Awoke : Boolean := False;
Saved_Moves : Integer;
Saved_Loc : Location_T;
Cityp : City_Info_P;
begin
-- set func for piece if on city
Cityp := Objects.Find_City_At_Loc (Obj.Loc);
if Cityp /= null and then Cityp.Func (Obj.Piece_Type) /= NOFUNC then
User_Obj_Func (Obj, Cityp.Func (Obj.Piece_Type));
end if;
-- this recomputes, which is needed because Obj_Moves(Obj) can drop if
-- Obj takes damage
while Obj.Moved < Objects.Obj_Moves (Obj) loop
Saved_Moves := Obj.Moved;
Saved_Loc := Obj.Loc;
Awake (Obj, Awoke);
if Awoke or Need_Input then
Ask_User (Obj);
-- XXX should we also display before asking, or is this guaranteed by
-- sequence?
Ui.Display_Loc (USER, Obj.Loc); -- let user see result at once
Need_Input := False;
end if;
if Obj.Moved = Saved_Moves -- non-move (user didn't move object)
then
case Obj.Func is
when NOFUNC =>
null; -- will result in need_input being set, below
when RANDOM =>
Move_Random (Obj);
when SENTRY =>
-- for now, sentry means `guard in place'. some empire
-- games have a roving sentry function...
Obj.Moved := Objects.Obj_Moves (Obj);
when FILL =>
Move_Fill (Obj);
when LAND =>
Move_Land (Obj);
when EXPLORE =>
Move_Explore (Obj);
when ARMYATTACK =>
Move_Armyattack (Obj);
when REPAIR =>
Move_Repair (Obj);
when WFTRANSPORT =>
Move_Transport (Obj);
when MOVE_N .. MOVE_NW =>
Move_Dir (Obj);
when others =>
-- MOVE_TO_DEST
-- the two COMP_ funcs won't occur
Move_Path (Obj);
end case;
end if;
-- if Obj.Func was NOFUNC, or object failed to move:
if Obj.Moved = Saved_Moves then
Need_Input := True;
end if;
-- handle aircraft 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.
-- note that aircraft fuel state is checked in Awake.
-- XXX can we get here with an object with zero remaining hits?
if Piece_Attr (Obj.Piece_Type).Class = AIRCRAFT and Obj.Hits > 0 then
if
(View (USER) (Obj.Loc).Contents = 'O' or
View (USER) (Obj.Loc).Contents = 'C') and
Obj.Moved > 0
then
Obj.Piece_Range :=
Piece_Attr (Obj.Piece_Type).Piece_Range; -- refuel
Obj.Moved := Objects.Obj_Moves (Obj); -- end move
User_Obj_Func (Obj, NOFUNC); -- end function
Ui.Info ("Landing confirmed.");
elsif Obj.Piece_Range = 0 then
Ui.Info
("Fighter at " &
Integer'Image (Obj.Loc) &
" crashed and burned.");
Objects.Kill_Obj (Obj, Obj.Loc);
end if;
end if;
if Saved_Loc /= Obj.Loc then
Changed_Loc := True;
end if;
end loop;
-- if a boat is in port, damaged, and never moved, fix some damage
-- we check if still alive? can we get here otherwise?
if Obj.Hits > 0 and
not Changed_Loc and
Piece_Attr (Obj.Piece_Type).Class = SHIP and
Obj.Hits < Piece_Attr (Obj.Piece_Type).Max_Hits and
View (USER) (Obj.Loc).Contents = 'O'
then
Obj.Hits := Obj.Hits + 1;
end if;
end Piece_Move;
-- 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.
procedure Move_Random (Obj : in Piece_Info_P) is
Loc_List : array (0 .. 7) of Location_T;
NLoc : Integer := 0;
Loc : Location_T;
I : Integer;
begin
for I in Direction_T'Range loop
Loc := Obj.Loc + Dir_Offset (I);
if Objects.Good_Loc (Obj, Loc) then
Loc_List (NLoc) := Loc;
NLoc := NLoc + 1;
end if;
end loop;
if NLoc = 0 then
return; -- no legal move
end if;
I :=
Math.Rand_Long (NLoc - 1); -- randomize among found good directions
Objects.Move_Obj (Obj, Loc_List (I));
end Move_Random;
-- Have a piece explore. We look for the nearest unexplored territory which
-- the piece can reach and have to piece move toward the territory.
procedure Move_Explore (Obj : in Piece_Info_P) is
Pmap : Mapping.Path_Map;
Loc : Location_T;
Terrain : Acceptable_Content_Array;
begin
case Piece_Attr (Obj.Piece_Type).Class is
when GROUND =>
Mapping.Vmap_Find_Ground_Obj
(Loc,
Pmap,
View (USER),
Obj.Loc,
User_Army);
Terrain := ('+' => True, others => False);
when AIRCRAFT =>
Mapping.Vmap_Find_Aircraft_Obj
(Loc,
Pmap,
View (USER),
Obj.Loc,
User_Fighter);
Terrain := ('+' | '.' | 'O' => True, others => False);
when SHIP =>
Mapping.Vmap_Find_Ship_Obj
(Loc,
Pmap,
View (USER),
Obj.Loc,
User_Ship);
Terrain := ('.' | 'O' => True, others => False);
when SPACECRAFT =>
raise Program_Error; -- spacecraft can't have a function
end case;
if Loc = Obj.Loc then
return; -- nothing to explore (that's reachable)
end if;
if View (USER) (Loc).Contents = ' ' and Pmap.Cost (Loc) = 2 then
Pmap.Mark_Adjacent (Obj.Loc);
else
Pmap.Mark_Path (View (USER), Loc);
end if;
Loc :=
Pmap.Find_Dir (View (USER), Obj.Loc, Terrain, (' ' => 1, others => 0));
if Loc /= Obj.Loc then
Objects.Move_Obj (Obj, Loc);
end if;
end Move_Explore;
-- 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. Otherwise, we wait until next turn.
procedure Move_Transport (Obj : in Piece_Info_P) is
Loc : Location_T;
Found : Boolean;
Cost : Integer;
begin
-- look for an adjacent transport XXX we limit range to `1', as we
-- don't check to see if path XXX is traversable. This is clear room for
-- improvement, though XXX if we do, we should probably only consider
-- transports which XXX are loading -- otherwise we will chase an
-- approaching transport XXX along the shore!
Objects.Find_Nearest_Obj
(Obj.Loc,
USER,
(TRANSPORT => True, others => False),
1,
True,
Found,
Loc,
Cost);
if Found then
Objects.Move_Obj (Obj, Loc);
User_Obj_Func (Obj, NOFUNC);
else
Obj.Moved := Objects.Obj_Moves (Obj);
end if;
end Move_Transport;
-- cempire had a move_armyload, defined as:
-- Move an army toward the nearest loading transport. If there is an
-- adjacent transport, move the army onto the transport, and awaken
-- the army.
-- but it was set to panic.
-- and an empty stub of a reciprocal function, Tt_load, but it too was set
-- to panic
-- Move an army toward an attackable city or enemy army
procedure Move_Armyattack (Obj : in Piece_Info_P) is
Pmap : Mapping.Path_Map;
Loc : Location_T;
begin
if Obj.Piece_Type /= ARMY then
raise Program_Error;
end if;
Mapping.Vmap_Find_Ground_Obj
(Loc,
Pmap,
View (USER),
Obj.Loc,
User_Army_Attack);
if Loc = Obj.Loc then
return; -- nothing to attack
end if;
Pmap.Mark_Path (View (USER), Loc);
Loc :=
Pmap.Find_Dir
(View
(USER), Obj
.Loc,
('+' => True,
others => False),
('X' => 3,
'*' => 2,
'a' => 1,
others => 0));
if Loc /= Obj.Loc then
Objects.Move_Obj (Obj, Loc);
end if;
end Move_Armyattack;
-- Move a ship toward port. If the ship is healthy, wake it up.
procedure Move_Repair (Obj : in Piece_Info_P) is
Pmap : Mapping.Path_Map;
Loc : Location_T;
begin
if Piece_Attr (Obj.Piece_Type).Class /= SHIP then
raise Program_Error;
end if;
if Obj.Hits = Piece_Attr (Obj.Piece_Type).Max_Hits then
-- we don't need repair
User_Obj_Func (Obj, NOFUNC);
return;
end if;
if View (USER) (Obj.Loc).Contents = 'O' -- in port
then
Obj.Moved := Obj.Moved + 1;
return;
end if;
Mapping.Vmap_Find_Ship_Obj
(Loc,
Pmap,
View (USER),
Obj.Loc,
User_Ship_Repair);
if Loc = Obj.Loc -- no reachable city (how?)
then
return;
end if;
Pmap.Mark_Path (View (USER), Loc);
-- try to avoid land (literally, stay adjacent to ocean)
Loc :=
Pmap.Find_Dir
(View
(USER), Obj
.Loc,
('.' | 'O' => True,
others => False),
('.' => 1,
others => 0));
if Loc /= Obj.Loc then
Objects.Move_Obj (Obj, Loc);
end if;
end Move_Repair;
-- 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.
procedure Move_Fill (Obj : in Piece_Info_P) is
begin
if Obj.Count = Objects.Obj_Capacity (Obj) then
User_Obj_Func (Obj, NOFUNC);
else
Obj.Moved := Objects.Obj_Moves (Obj);
end if;
end Move_Fill;
-- 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.
procedure Move_Land (Obj : in Piece_Info_P) is
Best_Dist, New_Dist : Integer;
Best_Loc, Carrier_loc : Location_T;
City_Found, Carrier_Found : Boolean;
begin
Objects.Find_Nearest_City
(Obj.Loc,
USER,
City_Found,
Best_Loc,
Best_Dist);
-- see if we can find a carrier closer than the nearest city
Objects.Find_Nearest_Obj
(Obj.Loc,
USER,
(CARRIER => True, others => False),
INFINITY,
True,
Carrier_Found,
Carrier_loc,
New_Dist);
if Carrier_Found then
New_Dist := New_Dist + Carrier_Bias; -- see discussion in Awake()
if not City_Found or New_Dist < Best_Dist then
Best_Dist := New_Dist;
Best_Loc := Carrier_loc;
end if;
end if;
if Best_Dist = 0 -- we're already there
then
Obj.Moved := Obj.Moved + 1; -- XXX why?
elsif Best_Dist < Obj.Piece_Range then
Move_To_Dest (Obj, Best_Loc);
else
User_Obj_Func (Obj, NOFUNC); -- can't reach city or carrier
end if;
end Move_Land;
-- Move a piece in the specified direction if possible. XXX If the object
-- is an aircraft which has travelled for half its range, XXX we should
-- wake it up. (original code's comment claimed to do so, XXX but no such
-- code was present)
procedure Move_Dir (Obj : in Piece_Info_P) is
Loc : Location_T;
Dir : Direction_T;
begin
Dir := Move_Func_Directions (Obj.Func);
Loc := Obj.Loc + Dir_Offset (Dir);
if Objects.Good_Loc (Obj, Loc) then
Objects.Move_Obj (Obj, Loc);
end if;
end Move_Dir;
-- 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.
procedure Move_Path (Obj : in Piece_Info_P) is
begin
if Obj.Func /= MOVE_TO_DEST then
raise Program_Error;
end if;
if Obj.Dest = Obj.Loc then
User_Obj_Func (Obj, NOFUNC); -- we're heeeeere
else
Move_To_Dest (Obj, Obj.Dest);
end if;
end Move_Path;
-- 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.
procedure Move_To_Dest (Obj : in Piece_Info_P; Dest : in Location_T) is
Pmap : Mapping.Path_Map;
Fterrain : Terrain_T;
Mterrain : Acceptable_Content_Array;
New_Loc : Location_T;
begin
case Piece_Attr (Obj.Piece_Type).Class is
when GROUND =>
Fterrain := T_LAND;
Mterrain := ('+' => True, others => False);
when AIRCRAFT =>
Fterrain := T_AIR;
Mterrain := ('+' | '.' | 'O' => True, others => False);
when SHIP =>
Fterrain := T_WATER;
Mterrain := ('.' | 'O' => True, others => False);
when SPACECRAFT =>
raise Program_Error; -- spacecraft can't make controlled moves
end case;
Mapping.Vmap_Find_Dest
(New_Loc,
Pmap,
View (USER),
Obj.Loc,
Dest,
USER,
Fterrain);
if New_Loc = Obj.Loc then
return; -- can't get there -- user_move will remove func setting
end if;
Pmap.Mark_Path (View (USER), Dest);
New_Loc :=
Pmap.Find_Dir
(View (USER), Obj.Loc, Mterrain, (' ' => 2, '.' => 1, others => 0));
if New_Loc = Obj.Loc -- no good move along path
then
return;
end if;
if not Objects.Good_Loc (Obj, New_Loc) then
raise Program_Error;
end if;
Objects.Move_Obj (Obj, New_Loc);
end Move_To_Dest;
-- Ask the user to move his own darn piece
procedure Ask_User (Obj : in Piece_Info_P) is
C : Character;
begin
loop
Ui.Display_Loc (USER, Obj.Loc);
Objects.Describe_Obj (Obj);
Ui.Display_Score;
-- repositions cursor, after re-drawing normal score line
Ui.Display_Loc (USER, Obj.Loc);
C := Ui.Get_Chx; -- get command (no echo)
-- XXX this should be table driven, esp. as it has to match XXX data
-- in two places in empire.ads (help and func names)
case C is
when 'Q' =>
User_Dir (Obj, NORTHWEST);
return; -- so we don't loop
when 'W' =>
User_Dir (Obj, NORTH);
return;
when 'E' =>
User_Dir (Obj, NORTHEAST);
return;
when 'D' =>
User_Dir (Obj, EAST);
return;
when 'C' =>
User_Dir (Obj, SOUTHEAST);
return;
when 'X' =>
User_Dir (Obj, SOUTH);
return;
when 'Z' =>
User_Dir (Obj, SOUTHWEST);
return;
when 'A' =>
User_Dir (Obj, WEST);
return;
when 'J' =>
Editing.Edit (Obj.Loc);
Reset_Func (Obj);
return;
when 'V' =>
User_Set_City_Func (Obj.Loc);
Reset_Func (Obj);
return;
when ' ' =>
User_Skip (Obj);
return;
when 'F' =>
User_Obj_Func
(Obj,
FILL,
(TRANSPORT | CARRIER => True, others => False));
return;
when 'I' =>
User_Set_Dir (Obj);
return;
when 'R' =>
User_Obj_Func (Obj, RANDOM);
return;
when 'S' =>
User_Obj_Func (Obj, SENTRY);
return;
when 'L' =>
User_Obj_Func (Obj, LAND, (FIGHTER => True, others => False));
return;
when 'G' =>
User_Obj_Func (Obj, EXPLORE);
return;
when 'T' =>
User_Obj_Func
(Obj,
WFTRANSPORT,
(ARMY => True, others => False));
return;
when 'U' =>
User_Obj_Func
(Obj,
REPAIR,
(ARMY | FIGHTER | SATELLITE => False, others => True));
return;
when 'Y' =>
User_Obj_Func
(Obj,
ARMYATTACK,
(ARMY => True, others => False));
return;
when 'B' =>
User_Build (Obj.Loc);
-- from here in we loop, thus asking user again after command is
-- complete
when '?' =>
Ui.Help (Help_User);
when 'K' =>
User_Obj_Func (Obj, NOFUNC);
return;
when 'O' =>
User_Cancel_Auto;
when 'P' | Character'Val (12) =>
Ui.Redraw;
when '=' =>
Objects.Describe_Obj (Obj);
when others =>
-- Ui.Huh, maybe?
Ui.Alert;
end case;
end loop;
end Ask_User;
-- 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).
procedure Reset_Func (Obj : in Piece_Info_P) is
Cityp : City_Info_P;
B : Boolean;
begin
Cityp := Objects.Find_City_At_Loc (Obj.Loc);
if Cityp /= null and then Cityp.Func (Obj.Piece_Type) /= NOFUNC then
User_Obj_Func (Obj, Cityp.Func (Obj.Piece_Type));
Awake (Obj, B);
end if;
end Reset_Func;
procedure User_Obj_Func
(Obj : in Piece_Info_P;
Func : in Function_T;
Ptypes : in Acceptable_Piece_Array :=
(SATELLITE => False, others => True);
Dest_If_Move_To_Dest : Location_T := 0)
is
begin
if Ptypes (Obj.Piece_Type) then
Obj.Func := Func;
if Func = MOVE_TO_DEST then
Obj.Dest := Dest_If_Move_To_Dest;
end if;
else
-- XXX should be Ui.Huh, ala empire-editing.adb?
Ui.Alert;
end if;
end User_Obj_Func;
-- 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.
procedure User_Skip (Obj : in Piece_Info_P) is
begin
if Obj.Piece_Type = ARMY and View (USER) (Obj.Loc).Contents = 'O' then
Move_Army_To_City (Obj, Obj.Loc);
else
Obj.Moved := Obj.Moved + 1;
end if;
end User_Skip;
-- Set an object's function to move in a certain direction.
procedure User_Set_Dir (Obj : in Piece_Info_P) is
C : Character;
begin
C := Ui.Get_Chx;
-- XXX should loop until valid result?
case C is
when 'Q' =>
User_Obj_Func (Obj, MOVE_NW);
when 'W' =>
User_Obj_Func (Obj, MOVE_N);
when 'E' =>
User_Obj_Func (Obj, MOVE_NE);
when 'D' =>
User_Obj_Func (Obj, MOVE_E);
when 'C' =>
User_Obj_Func (Obj, MOVE_SE);
when 'X' =>
User_Obj_Func (Obj, MOVE_S);
when 'Z' =>
User_Obj_Func (Obj, MOVE_SW);
when 'A' =>
User_Obj_Func (Obj, MOVE_W);
when others =>
Ui.Alert;
end case;
end User_Set_Dir;
-- Set a city's function
-- XXX XXX XXX this is identical to E_City_Func, except that that func
-- supports XXX XXX XXX a path. they should be merged.
procedure User_Set_City_Func (loc : in Location_T) is
Ptype : Piece_Choice_T;
E : Character;
Cityp : City_Info_P;
begin
Cityp := Objects.Find_City_At_Loc (loc, (USER => True, others => False));
if Cityp = null then
Ui.Alert;
return;
end if;
Ptype := Ui.Get_Piece_Name;
if Ptype = NOPIECE then
-- XXX maybe we should raise program_error -- how did we get here?
Ui.Alert;
return;
end if;
-- XXX maybe a prompt?
E := Ui.Get_Chx;
case E is
when 'F' =>
if (Ptype = TRANSPORT) or (Ptype = CARRIER) then
Editing.E_User_City_Func (loc, FILL, Ptype);
else
Ui.Huh;
end if;
when 'G' =>
Editing.E_User_City_Func (loc, EXPLORE, Ptype);
when 'I' =>
Editing.E_City_Move_Direction (loc, Ptype);
when 'K' =>
Editing.E_User_City_Func (loc, NOFUNC, Ptype);
when 'R' =>
Editing.E_User_City_Func (loc, RANDOM, Ptype);
when 'U' =>
Editing.E_City_Repair (loc, Ptype);
when 'Y' =>
if Ptype = ARMY then
Editing.E_User_City_Func (loc, ARMYATTACK, Ptype);
else
Ui.Huh;
end if;
when others =>
Ui.Huh;
end case;
end User_Set_City_Func;
-- Change a city's production
procedure User_Build (Loc : in Location_T) is
Cityp : City_Info_P;
begin
Cityp := Objects.Find_City_At_Loc (Loc, (USER => True, others => False));
if Cityp = null then
Ui.Alert;
return;
end if;
Objects.Ask_Prod (Cityp.all);
end User_Build;
-- Move a piece in the direction specified by the user. This routine
-- handles attacking objects.
procedure User_Dir (Obj : in Piece_Info_P; Dir : Direction_T) is
Loc : Location_T;
begin
Loc := Obj.Loc + Dir_Offset (Dir);
if Objects.Good_Loc (Obj, Loc) then
Objects.Move_Obj (Obj, Loc);
return;
end if;
if not Map (Loc).On_Board then
Ui.Error ("You cannot move off the edge of the world.");
return;
end if;
case Piece_Attr (Obj.Piece_Type).Class is
when GROUND =>
User_Dir_Ground (Obj, Loc);
when AIRCRAFT =>
User_Dir_Aircraft (Obj, Loc);
when SHIP =>
User_Dir_Ship (Obj, Loc);
when SPACECRAFT =>
raise Program_Error; -- satellites are never under user control
end case;
end User_Dir;
-- 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.
procedure User_Dir_Ground (Obj : in Piece_Info_P; Loc : in Location_T) is
begin
-- remember, if we get here, this is NOT an uncontested move -- so see
-- what the problem is, and if it's an enemy, have at it.
-- original code had terrible cutesy-ism -- various fatal actions were
-- allowed, after a snarky yes/no prompt. If we want this, we can add a
-- `disband' command.
if View (USER) (Loc).Contents = 'O' -- moving into own city
then
Move_Army_To_City (Obj, Loc);
elsif View (USER) (Loc).Contents = 'T' then
-- if we got here, the transport is full
Ui.Error
("The transport at location " &
Location_T'Image (Loc) &
" is full.");
elsif Map (Loc).Contents = '.' then
-- more cutesy-ism was here. We die if we walk out to sea, but if
-- there was an enemy there, we get to attack him first. XXX this is
-- gone, but some form of army attack out to sea might be worthwhile
-- message apparently from Craig Hansen
Ui.Error ("Troops can't walk on water, sir.");
elsif User_Content (View (USER) (Loc).Contents) then
-- XXX XXX XXX ideally, we could coexist with a satellite or airborne
-- aircraft, actually
Ui.Error ("Sir, those are our own men. We can't attack them!");
else
Attack.Attack (Obj, Loc);
end if;
end User_Dir_Ground;
-- Here we have a fighter wanting to attack something. There are only three
-- cases: attacking a city, attacking ourself, attacking the enemy. this
-- had the same cutesy-ism as above in the original
procedure User_Dir_Aircraft (Obj : in Piece_Info_P; Loc : Location_T) is
begin
if Map (Loc).Contents = '*' then
Ui.Error
("The air defenses over the city at " &
Location_T'Image (Loc) &
" are too strong, we can't go there.");
elsif User_Content (View (USER) (Loc).Contents) then
-- XXX XXX XXX ideally, we could coexist with anything but another
-- aircraft
Ui.Error ("Sir, those are our own men. We can't attack them!");
else
Attack.Attack (Obj, Loc);
end if;
end User_Dir_Aircraft;
-- 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. needless by now to say, this had the same
-- cutesy-ism as the other two
procedure User_Dir_Ship (Obj : in Piece_Info_P; Loc : in Location_T) is
begin
if Map (Loc).Contents = '*' then
Ui.Error
("The port defenses of the city at " &
Location_T'Image (Loc) &
" are too strong, we can't go there.");
elsif Map (Loc).Contents = '+' then
Ui.Error ("Ships need sea to float sir. We can't go ashore.");
-- XXX original code let any ship attack a shore unit, but the ship
-- always died. What XXX we really want is to allow a battleship or
-- destroyer to bombard the shore (costs one move, XXX doesn't move
-- the ship).
elsif User_Content (View (USER) (Loc).Contents) then
-- XXX XXX XXX ideally, we could coexist with a satellite or airborne
-- aircraft, actually
Ui.Error ("Sir, those are our own men. We can't attack them!");
else
Attack.Attack (Obj, Loc);
end if;
end User_Dir_Ship;
-- Here a user wants to move an army to a city. If the city contains a
-- non-full transport, we make the move. Yes, there was more cutesy-ism.
procedure Move_Army_To_City (Obj : in Piece_Info_P; Loc : Location_T) is
Tt : Piece_Info_P;
begin
Tt :=
Objects.Find_Obj_At_Loc
(Loc,
(TRANSPORT => True, others => False),
(USER => True, others => False),
True);
if Tt /= null then
Objects.Move_Obj (Obj, Loc);
else
Ui.Error
("There's no garrison room left in the city at " &
Location_T'Image (Loc) &
" and no transport waiting to load.");
end if;
end Move_Army_To_City;
-- Cancel automove mode
procedure User_Cancel_Auto is
begin
if not Automove then
Ui.Error ("Not in auto mode.");
else
Automove := False;
Ui.Info ("Auto mode cancelled.");
end if;
end User_Cancel_Auto;
-- 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.
procedure Awake (Obj : in Piece_Info_P; Awoken : out Boolean) is
C : Content_Display_T;
begin
if Piece_Attr (Obj.Piece_Type).Class = GROUND and
Mapping.Vmap_At_Sea (View (USER), Obj.Loc)