-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathempire-math.adb
48 lines (40 loc) · 1.38 KB
/
empire-math.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
-- empire-math.adb -- various mathematical routines.
--
-- This file contains routines used to create random integers. The
-- initialization routine 'rand_init' should be called at program startup.
-- The flavors of random integers that can be generated are:
--
-- rand_long (n) -- returns a random integer in the range 0..n-1
--
-- Other routines include:
--
-- dist (a, b) -- returns the straight-line distance between two locations.
with Empire.Locations;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
-- with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
package body Empire.Math is
function Dist (A : in Location_T; B : in Location_T) return Integer is
Ax, Ay, Bx, By : Integer;
begin
Ax := Locations.Loc_Row (A);
Ay := Locations.Loc_Col (A);
Bx := Locations.Loc_Row (B);
By := Locations.Loc_Col (B);
-- XXX use an integer equivalent, possibly isqrt from cempire.
return Integer
(Sqrt
(Float ((Ax - Bx) * (abs Ax - Bx)) +
Float ((Ay - By) * (Ay - By))));
end Dist;
procedure Rand_Init is
begin
Reset (G);
end Rand_Init;
function Rand_Long (High : Integer) return Integer is
begin
if High < 2 then
return 0;
end if;
return Integer (Float (High) * Random (G)) mod High;
end Rand_Long;
end Empire.Math;