-
Notifications
You must be signed in to change notification settings - Fork 0
/
waypoints.adb
693 lines (601 loc) · 22.4 KB
/
waypoints.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
-- File name : waypoints.adb
-- Purpose : Project 2 Final Submission Waypoints File
-- Author : erl
-- Course : CS301
-- Hour/section : G4
with Ada.Text_Io, Ada.Integer_Text_Io, Ada.Float_Text_Io,
Unchecked_Deallocation;
with Ada.Numerics.Elementary_Functions;
use Ada.Text_Io, Ada.Integer_Text_Io, Ada.Float_Text_Io;
package body Waypoints is
package Threat_Code_Io is new Enumeration_Io(Threat_Code);
use Threat_Code_Io;
package File_Control_Io is new Enumeration_Io(File_Control);
use File_Control_Io;
procedure Dispose is
new Unchecked_Deallocation (Waypoint, Way_Ptr);
--function to get one coordinate from user
function Get_A_Coord (
Xory : in Choice )
return Map_Range is
Coord : Map_Range := 100;
begin
loop
begin
case Xory is
when 1 =>
Put("Enter the X coordinate: ");
when 2=>
Put("Enter the Y coordinate: ");
when others =>
null;
end case;
Ada.Integer_Text_Io.Get(Coord);
exit;
exception
when Constraint_Error | Data_Error =>
--if input is outside Map_Range
Skip_Line;
New_Line;
Put_Line("ERROR: That input was no good. Use 100-999.");
end;
end loop;
return Coord;
end Get_A_Coord;
--function where user decides how to enter data for further operations
function Enter_Data_Type return Choice is
Input_Choice : Choice := 1;
begin
loop -- determine how the user will enter a point
begin
New_Line;
Put_Line("How will you enter your data?");
Put_Line(" 1. Coordinates");
Put_Line(" 2. Name");
Ada.Integer_Text_Io.Get(Input_Choice);
New_Line;
exit;
exception
when Constraint_Error | Data_Error =>
Skip_Line;
New_Line;
Put_Line(
"ERROR: That choice was no good. Try again with 1 or 2.");
end;
end loop;
return Input_Choice;
end Enter_Data_Type;
--function for user to input threat level
function Enter_Threat_Code return Threat_Code is
Threat_Input : Threat_Code := Unknown;
begin
loop
begin
Put("Enter the threat code: ");
Get(Threat_Input);
exit;
exception
when Constraint_Error | Data_Error =>
Skip_Line;
New_Line;
Put(
"ERROR: That input was no good. Try again with UNKNOWN,");
Put_Line("GREEN, AMBER, or RED");
end;
end loop;
return Threat_Input;
end Enter_Threat_Code;
--function for user to enter a waypoint name
function Enter_Waypoint_Name return Name_String is
Instring : Name_String := (others => ' ');
Lastchar : Integer;
begin
loop
begin
Put("Enter the name of the waypoint: ");
Ada.Text_Io.Get_Line(Instring,Lastchar);
exit;
exception
when Constraint_Error | Data_Error =>
Skip_Line;
New_Line;
Put_Line("ERROR: That input was no good. Try again.");
end;
end loop;
return Instring;
end Enter_Waypoint_Name;
--procedure that finds the Coordinates of a waypoint given the name
procedure Find_Coordinates (
Map : in Maparray;
Instring : in out Name_String;
Xcor,
Ycor : out Map_Range ) is
begin
for I in 100..999 loop --look through all points for name
for J in 100..999 loop
if Map(I,J).Name = Instring then
Xcor := I;
Ycor := J;
exit;
elsif I = 999 and J = 999 then
Instring := " ";
end if;
end loop;
if Map(I,Ycor).Name = Instring then
exit;
end if;
end loop;
end Find_Coordinates;
--procedure that gets users data(name or coords.) and finds whats not given
procedure Enter_Data (
Map : in Maparray;
Xcor,
Ycor : out Map_Range;
Instring : out Name_String ) is
K : Choice := 1;
begin
K := Enter_Data_Type; --determine how user wants to enter data
case K is
when 1 => --user wants to search by coordinate
Xcor := Get_A_Coord(1);
Ycor := Get_A_Coord(2);
Instring := Map(Xcor,Ycor).Name;
when 2=> --user wants to search by waypoint name
Skip_Line;
Instring:=Enter_Waypoint_Name;
Find_Coordinates(Map,Instring,Xcor,Ycor);
end case;
end Enter_Data;
-----------------------------------------------------------------
-- Allows user to create a new path up to maximum of 8 paths.
--
--
-- --------
-- Map----->| |----> Map
-- Head---->| Create |----> Head
-- | New |
-- | Path |
-- --------
-----------------------------------------------------------------
procedure Create_New_Path (
Head : in out Headarray;
Map : in out Maparray ) is
I : Integer := 1;
Current_Ptr : Way_Ptr;
Another : Character := 'y';
Instring : Name_String;
begin
while Head(I) /=null and I < 9 loop --find first head to start with
I := I +1;
end loop;
Head(I) := new Waypoint;
Current_Ptr:=Head(I);
while Another = 'y' loop --user enters data and assigns it to waypoints
Skip_Line;
Instring := Enter_Waypoint_Name;
Current_Ptr.Xcor := Get_A_Coord(1);
Current_Ptr.Ycor := Get_A_Coord(2);
Map(Current_Ptr.Xcor,Current_Ptr.Ycor).Name:=Instring;
Map(Current_Ptr.Xcor,Current_Ptr.Ycor).Threat_Level:=
Enter_Threat_Code;
Put("Enter another waypoint (y/n)? ");
Get(Another);
if Another = 'y' then
Current_Ptr.Next := new Waypoint;
Current_Ptr := Current_Ptr.Next;
end if;
end loop;
end Create_New_Path;
-----------------------------------------------------------------
-- Calculates shortest path between a coordinate or name of a
-- waypoint from any depot
--
-- --------
-- Map----->| |
-- Head---->| Short |
-- | est |
-- | Path |
-- --------
-----------------------------------------------------------------
procedure Shortest_Path (
Head : in Headarray;
Map : in Maparray ) is
type Distarray is array (0 .. 8) of Float;
Dist : Distarray := (others => 10000000.0); --path distance
Dmin : Integer := 0; --which path is shortest
Dcalc : Float := 0.0;
Xcor,
Ycor : Map_Range := 100;
Instring : Name_String;
Current_Ptr : Way_Ptr;
Yval,
Xval : Float := 0.0;
begin
Enter_Data(Map, Xcor,Ycor,Instring); --Get data to search on
if Instring = " " then
Put("That is not a valid waypoint.");
else
for I in 1..8 loop
Dist(I) := 0.0;
Current_Ptr:=Head(I);
while Current_Ptr /= null loop
if Current_Ptr.Next /= null then --add up the distances
Yval := Float((Current_Ptr.Next.Ycor - Current_Ptr.Ycor)**2);
Xval := Float((Current_Ptr.Next.Xcor - Current_Ptr.Xcor)**2);
Dcalc := Ada.Numerics.Elementary_Functions.Sqrt(Xval+
Yval);
Dist(I) := Dist(I) + Dcalc;
if Current_Ptr.Next.Xcor = Xcor and --if on the point then finish
Current_Ptr.Next.Ycor = Ycor then
if Dist(I) < Dist(Dmin) then --save shortest distance and path
Dmin := I;
end if;
exit;
end if;
end if;
Current_Ptr:=Current_Ptr.Next;
end loop;
end loop;
New_Line; --display output
if Dist(Dmin) = 10000000.0 then
Put("That waypoint is the start point of the path.");
else
Put("The shortest path is: ");
Ada.Integer_Text_Io.Put(Dmin,2);
Put(", with a distance of: ");
Ada.Float_Text_Io.Put(Dist(Dmin),4,2,0);
end if;
end if;
end Shortest_Path;
-----------------------------------------------------------------
-- Given destination coord or the name of waypoint
-- find the least dangerous path
--
-- --------
-- Map----->| |
-- Head---->| Safe |
-- | est |
-- | Path |
-- --------
-----------------------------------------------------------------
procedure Safest_Path (
Head : in Headarray;
Map : in Maparray ) is
type Safearray is array (0 .. 8) of Threat_Code;
Safe : Safearray := (others => Green);
Smin : Integer := 0; --which path is safest
Xcor,
Ycor : Map_Range := 100;
Instring : Name_String;
Current_Ptr : Way_Ptr;
Tl : Threat_Code;
begin
Enter_Data(Map, Xcor,Ycor,Instring);
if Instring = " " then
Put("That is not a valid waypoint.");
else
for I in 1..8 loop
Current_Ptr:=Head(I);
while Current_Ptr /= null loop --trace path and set safety level
Tl := Map(Current_Ptr.Xcor,Current_Ptr.Ycor).Threat_Level;
if Tl = Red or Tl = Unknown then
Safe(I) := Red;
elsif Safe(I) /= Red and Tl = Amber then
Safe(I) := Amber;
elsif Safe(I) /=Red and Safe(I) /=Amber then
Safe(I) := Green;
end if;
if Current_Ptr.Xcor = Xcor and Current_Ptr.Ycor = Ycor then
for J in 1..8 loop --determine which path is safest
if Safe(I) = Green and (Safe(J) = Red or Safe(J) =
Amber) then
Smin :=I;
elsif Safe(I) = Amber and Safe(J) = Red then
Smin := I;
else
Smin := I;
end if;
end loop;
exit;
end if;
Current_Ptr:=Current_Ptr.Next;
end loop;
end loop;
New_Line; --display output
Put("The safest path is: ");
Ada.Integer_Text_Io.Put(Smin,1);
Put(", with a threat level of: ");
Threat_Code_Io.Put(Safe(Smin));
end if;
end Safest_Path;
-----------------------------------------------------------------
-- Add a point to an existing path at any location in the path
--
--
-- --------
-- Map----->| |----> Map
-- Head---->| Add |----> Head
-- | Way |
-- | Point |
-- --------
-----------------------------------------------------------------
procedure Add_Waypoint (
Map : in out Maparray;
Head : in out Headarray ) is
Current_Ptr : Way_Ptr;
Temp_Ptr : Way_Ptr;
Instring : Name_String;
subtype Path is Integer range 1..8;
Add_Path : Path := 1;
Inchar : Character := 'n';
begin
Temp_Ptr := new Waypoint;
Skip_Line;
Instring := Enter_Waypoint_Name;
Temp_Ptr.Xcor := Get_A_Coord(1);
Temp_Ptr.Ycor := Get_A_Coord(2);
Map(Temp_Ptr.Xcor,Temp_Ptr.Ycor).Name:=Instring;
Map(Temp_Ptr.Xcor,Temp_Ptr.Ycor).Threat_Level:=Enter_Threat_Code;
loop
begin
New_Line;
Put("Enter the path you want to put this waypoint on: ");
Ada.Integer_Text_Io.Get(Add_Path);
exit;
exception
when Constraint_Error | Data_Error =>
Skip_Line;
New_Line;
Put_Line("ERROR: That input was no good. Use 1-8.");
end;
end loop;
if Head(Add_Path) = null then
Head(Add_Path) := Temp_Ptr;
else
Put("Do you want this point to be a new depot (y/n)? ");
Get(Inchar);
if Inchar = 'y' then --add point at the head of list
Temp_Ptr.Next := Head(Add_Path);
Head(Add_Path) := Temp_Ptr;
else
Current_Ptr := Head(Add_Path);
while Current_Ptr /= null loop --ask user where to put point
Put(Map(Current_Ptr.Xcor,Current_Ptr.Ycor).Name);
Put(" => Do you want to add the point after this one (y/n)? ");
Get(Inchar);
if Inchar = 'y' or Current_Ptr.Next = null then
Temp_Ptr.Next := Current_Ptr.Next; --add in temp_ptr
Current_Ptr.Next := Temp_Ptr;
exit;
end if;
Current_Ptr := Current_Ptr.Next; --traverse list
end loop;
end if;
end if;
end Add_Waypoint;
-----------------------------------------------------------------
-- Remove a point from an existing path
--
--
-- --------
-- Head---->| |----> Head
-- Map----->| Remove |----> Map
-- | Way |
-- | Point |
-- --------
-----------------------------------------------------------------
procedure Remove_Waypoint (
Head : in out Headarray;
Map : in out Maparray ) is
Xcor, --coordinate variables
Ycor : Map_Range := 100;
I : Integer := 1; --loop control variable
Instring : Name_String; -- := (others => ' ');
Current_Ptr : Way_Ptr;
Last_Ptr : Way_Ptr;
Erase : Character := 'n';
begin
Enter_Data(Map, Xcor,Ycor,Instring);
if Instring = " " then
Put("That is not a valid waypoint.");
else
Current_Ptr:=Head(I); --look for waypoint in all the arrays
while Current_Ptr /= null loop
if Current_Ptr.Xcor = Xcor and Current_Ptr.Ycor = Ycor then
Put("Do you want to remove this waypoint from path ");
Ada.Integer_Text_Io.Put(I,1);
Put(" (y/n)? ");
Get(Erase);
if Erase = 'y' then
if Current_Ptr.Next = null then --erases last point in list
Dispose(Current_Ptr);
Current_Ptr := Last_Ptr;
Current_Ptr.Next := null;
elsif Current_Ptr = Head(I) and Head(I) /= null then
Head(I) := Head(I).Next; --erases head of list
elsif Head(I).Next = null then
Dispose(Head(I)); --if there is only one point in list
else
Last_Ptr.Next := Current_Ptr.Next; --point in middle of list
Dispose(Current_Ptr);
Current_Ptr := Last_Ptr.Next;
end if;
end if;
end if;
if Current_Ptr.Next /= null then
Last_Ptr := Current_Ptr;
Current_Ptr :=Current_Ptr.Next;
else
I := I + 1;
Current_Ptr:=Head(I);
end if;
end loop;
end if;
end Remove_Waypoint;
-----------------------------------------------------------------
-- Change threat code of an existing waypoint
--
-- --------
-- Map----->| |----> Map
-- Head---->| Change |
-- | Threat |
-- | Code |
-- --------
-----------------------------------------------------------------
procedure Change_Threat_Code (
Head : in Headarray;
Map : in out Maparray ) is
Xcor : Map_Range := 100;
Ycor : Map_Range := 100;
Instring : Name_String := (others => ' ');
begin
Enter_Data(Map, Xcor,Ycor,Instring); --get users data
if Instring = " " then
Put("That is not a valid waypoint.");
else --get new threat code
Map(Xcor,Ycor).Threat_Level := Enter_Threat_Code;
end if;
end Change_Threat_Code;
-----------------------------------------------------------------
-- Print all paths
--
-- --------
-- Map----->| |
-- Head---->| Print |
-- | Paths |
-- | |
-- --------
-----------------------------------------------------------------
procedure Print_Paths (
Head : in Headarray;
Map : in Maparray ) is
Current_Ptr : Way_Ptr;
begin
for I in 1..8 loop
Current_Ptr:=Head(I);
while Current_Ptr /= null loop --loop for displaying
New_Line(2);
Put("Path number: " );
Ada.Integer_Text_Io.Put(I,1);
New_Line;
Put("Name X Y Threat");
while Current_Ptr /= null loop --display each path
New_Line;
Ada.Text_Io.Put(Map(Current_Ptr.Xcor,Current_Ptr.Ycor).
Name);
Ada.Integer_Text_Io.Put(Current_Ptr.Xcor, 4);
Ada.Integer_Text_Io.Put(Current_Ptr.Ycor,4 );
Put(" ");
Threat_Code_Io.Put(Map(Current_Ptr.Xcor,Current_Ptr.Ycor).
Threat_Level);
Current_Ptr :=Current_Ptr.All.Next;
end loop;
end loop;
end loop;
end Print_Paths;
-----------------------------------------------------------------
-- Load a set of paths from paths.txt
--
-- --------
-- | |
-- | Load |-->Map
-- | Paths |-->Head
-- | |
-- --------
-----------------------------------------------------------------
procedure Load_Paths (
Head : out Headarray;
Map : out Maparray ) is
I : Integer := 0;
Path_File : File_Type;
In_Control : File_Control := Beginpath;
Instring : Name_String; --Name Input String From File
Current_Ptr : Way_Ptr; --first pointer
begin
Head := (others => null);
Open(Path_File, In_File, "paths.txt");
Get(Path_File,In_Control);
while not End_Of_File(Path_File) loop --read file until it is empty
case In_Control is --case for each file control
when Beginpath =>
I := I + 1;
if I = 9 then
exit;
end if;
Head(I) := new Waypoint;
Current_Ptr := new Waypoint;
when Beginpoint => --add stuff to linked list
if Current_Ptr.Next = null then
Current_Ptr.All.Next := new Waypoint;
Current_Ptr := Current_Ptr.All.Next;
end if;
Get(Path_File,Instring); --get name and save for map
Ada.Integer_Text_Io.Get(Path_File,Current_Ptr.All.Xcor);
Ada.Integer_Text_Io.Get(Path_File,Current_Ptr.All.Ycor);
Map(Current_Ptr.Xcor,Current_Ptr.All.Ycor).Name := Instring;
Threat_Code_Io.Get(Path_File,Map(Current_Ptr.All.Xcor,
Current_Ptr.All.Ycor).Threat_Level);
if Head(I).Next = null then
Head(I) := Current_Ptr;
end if;
when Endpoint =>
null;
when Endpath =>
null;
when others =>
Put("Incorrect Input");
end case;
Get(Path_File,In_Control);
end loop;
Close(Path_File);
exception --exception handling for file
when Name_Error =>
New_Line;
Put ("ERROR: Input file for paths does not exist.");
when End_Error | Data_Error =>
New_Line;
Put("ERROR: Data file no good.");
end Load_Paths;
-----------------------------------------------------------------
-- Save current paths to paths.txt
--
-- --------
-- Head---->| |
-- Map----->| Save |-->paths.txt
-- | Paths |
-- | |
-- --------
-----------------------------------------------------------------
procedure Save_Paths (
Head : in Headarray;
Map : in Maparray ) is
Path_File : File_Type;
Current_Ptr : Way_Ptr;
begin
Open(Path_File,Out_File,"paths.txt");
for I in 1..8 loop --loop for each path
Current_Ptr := Head(I);
while Current_Ptr /=null loop
Ada.Text_Io.Put_Line(Path_File,"BEGINPATH");
while Current_Ptr /= null loop
Ada.Text_Io.Put_Line(Path_File,"BEGINPOINT");
Ada.Text_Io.Put_Line(Path_File,Map(Current_Ptr.Xcor,
Current_Ptr.Ycor).Name);
Ada.Integer_Text_Io.Put(Path_File,Current_Ptr.Xcor,3);
New_Line(Path_File);
Ada.Integer_Text_Io.Put(Path_File,Current_Ptr.Ycor,3);
New_Line(Path_File);
Threat_Code_Io.Put(Path_File,Map(Current_Ptr.Xcor,
Current_Ptr.Ycor).Threat_Level);
New_Line(Path_File);
Ada.Text_Io.Put_Line(Path_File,"ENDPOINT");
Current_Ptr := Current_Ptr.Next;
end loop;
Ada.Text_Io.Put_Line(Path_File,"ENDPATH");
end loop;
end loop;
Close(Path_File);
end Save_Paths;
begin
null;
end Waypoints;