From 7cef684e71b64f00b41da66ba7bc581568d78f51 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sun, 6 Aug 2017 22:17:54 +1000 Subject: Factored out some trivial bits into Misc, made coding style more consistent --- src/displays.adb | 26 ++++++++------------------ src/displays.ads | 8 +++++++- src/misc.adb | 31 +++++++++++++++++++++++++++++++ src/misc.ads | 33 +++++++++++++++++++++++++++++++++ src/moves.adb | 8 -------- src/moves.ads | 11 ++++++----- src/pathfinding.adb | 24 ++++++++++++------------ src/sokoban.adb | 7 ++----- src/sokoban.ads | 5 ++++- src/squares.adb | 26 ++++++++++++-------------- src/squares.ads | 25 ++++++++++++------------- src/things.adb | 20 ++++++++++---------- src/things.ads | 20 ++++++++------------ 13 files changed, 145 insertions(+), 99 deletions(-) create mode 100644 src/misc.adb create mode 100644 src/misc.ads diff --git a/src/displays.adb b/src/displays.adb index 6931db9..0a3df2b 100644 --- a/src/displays.adb +++ b/src/displays.adb @@ -3,7 +3,8 @@ with FLTK.Screen, - FLTK.Event; + FLTK.Event, + Misc; package body Displays is @@ -15,26 +16,13 @@ package body Displays is - function Max (A, B : in Integer) return Integer is - begin - if B > A then return B; else return A; end if; - end Max; - - function Min (A, B : in Integer) return Integer is - begin - if B < A then return B; else return A; end if; - end Min; - - - - function Create (X, Y, W, H : in Integer; Text : in String) return Display is - My_Width : Integer := Max (Message_Box_Width, W); - My_Height : Integer := Max (Message_Box_Height + Stat_Box_Height, H); + My_Width : Integer := Misc.Max (Message_Box_Width, W); + My_Height : Integer := Misc.Max (Message_Box_Height + Stat_Box_Height, H); begin return This : Display := (WD.Double_Window'(WD.Create (X, Y, My_Width, My_Height, Text)) with @@ -55,6 +43,8 @@ package body Displays is This.Message_Box.Set_Label_Size (Text_Size); This.Level_Box.Set_Label_Size (Text_Size); This.Move_Box.Set_Label_Size (Text_Size); + + This.Set_Icon (Logo); end return; end Create; @@ -103,11 +93,11 @@ package body Displays is (This : in out Display) is New_Width : Integer := - Max (Message_Box_Width, This.Current_Grid.Get_W); + Misc.Max (Message_Box_Width, This.Current_Grid.Get_W); New_Height : Integer := Message_Box_Height + This.Current_Grid.Get_H + Stat_Box_Height; Grid_X : Integer := - Max (0, (Message_Box_Width - This.Current_Grid.Get_W) / 2); + Misc.Max (0, (Message_Box_Width - This.Current_Grid.Get_W) / 2); begin This.Current_Grid.Reposition (Grid_X, 0); This.Message_Box.Resize (New_Width, This.Message_Box.Get_H); diff --git a/src/displays.ads b/src/displays.ads index c52a058..89e4542 100644 --- a/src/displays.ads +++ b/src/displays.ads @@ -7,7 +7,9 @@ with private with - FLTK.Widgets.Boxes; + FLTK.Widgets.Boxes, + FLTK.Images.RGB.PNG, + Misc; package Displays is @@ -106,6 +108,10 @@ private end record; + Logo : FLTK.Images.RGB.PNG.PNG_Image := + FLTK.Images.RGB.PNG.Create (Misc.Origin & "/../share/sokoban/img/man.png"); + + Text_Size : constant FLTK.Font_Size := 12; Message_Box_Width : constant Integer := 440; Message_Box_Height : constant Integer := 80; diff --git a/src/misc.adb b/src/misc.adb new file mode 100644 index 0000000..d1aa7ca --- /dev/null +++ b/src/misc.adb @@ -0,0 +1,31 @@ + + +package body Misc is + + + function Max + (A, B : in Integer) + return Integer is + begin + if B > A then + return B; + else + return A; + end if; + end Max; + + + function Min + (A, B : in Integer) + return Integer is + begin + if B < A then + return B; + else + return A; + end if; + end Min; + + +end Misc; + diff --git a/src/misc.ads b/src/misc.ads new file mode 100644 index 0000000..fb99d95 --- /dev/null +++ b/src/misc.ads @@ -0,0 +1,33 @@ + + +with + + Ada.Command_Line, + Ada.Directories; + + +package Misc is + + + Origin : constant String; + + + function Max + (A, B : in Integer) + return Integer; + + + function Min + (A, B : in Integer) + return Integer; + + +private + + + Origin : constant String := Ada.Directories.Containing_Directory + (Ada.Directories.Full_Name (Ada.Command_Line.Command_Name)); + + +end Misc; + diff --git a/src/moves.adb b/src/moves.adb index 391b35c..4024a4d 100644 --- a/src/moves.adb +++ b/src/moves.adb @@ -11,8 +11,6 @@ package body Moves is end Add; - - procedure Add (This : in out Path; List : in Path) is @@ -21,8 +19,6 @@ package body Moves is end Add; - - procedure Prefix (This : in out Path; Item : in Move) is @@ -41,8 +37,6 @@ package body Moves is end Latest; - - procedure Drop_Latest (This : in out Path) is begin @@ -60,8 +54,6 @@ package body Moves is end Length; - - procedure Total_Delta (This : in Path; DX, DY : out Integer) is diff --git a/src/moves.ads b/src/moves.ads index 728526c..3e86847 100644 --- a/src/moves.ads +++ b/src/moves.ads @@ -18,39 +18,40 @@ package Moves is type Path is tagged private; - Empty_Path : constant Path; + + procedure Add (This : in out Path; Item : in Move); - procedure Add (This : in out Path; List : in Path); - procedure Prefix (This : in out Path; Item : in Move); + + function Latest (This : in Path) return Move; - procedure Drop_Latest (This : in out Path); + + function Length (This : in Path) return Natural; - procedure Total_Delta (This : in Path; DX, DY : out Integer); diff --git a/src/pathfinding.adb b/src/pathfinding.adb index 1435ecf..9e45ec1 100644 --- a/src/pathfinding.adb +++ b/src/pathfinding.adb @@ -143,6 +143,9 @@ package body Pathfinding is Open_Set : Node_Sets.Set := Node_Sets.To_Set (Start); Came_From : Node_To_Node_Maps.Map := Node_To_Node_Maps.Empty_Map; + + New_G_Score : G_Score; + New_F_Score : F_Score; begin G_Scores := G_Score_Maps.Empty_Map; G_Scores.Insert (Start, 0); @@ -162,18 +165,15 @@ package body Pathfinding is if not Open_Set.Contains (N) then Open_Set.Insert (N); end if; - declare - New_G_Score : G_Score := G_Scores.Element (Current) + 1; - New_F_Score : F_Score := New_G_Score + Heuristic (N, Goal); - begin - if not G_Scores.Contains (N) or else - New_G_Score < G_Scores.Element (N) - then - Came_From.Insert (N, Current); - G_Scores.Insert (N, New_G_Score); - F_Scores.Insert (N, New_F_Score); - end if; - end; + New_G_Score := G_Scores.Element (Current) + 1; + New_F_Score := New_G_Score + Heuristic (N, Goal); + if not G_Scores.Contains (N) or else + New_G_Score < G_Scores.Element (N) + then + Came_From.Insert (N, Current); + G_Scores.Insert (N, New_G_Score); + F_Scores.Insert (N, New_F_Score); + end if; end if; end loop; end loop; diff --git a/src/sokoban.adb b/src/sokoban.adb index e70d1d6..de5601d 100644 --- a/src/sokoban.adb +++ b/src/sokoban.adb @@ -9,8 +9,7 @@ with Things, Moves, Pathfinding, - Ada.Command_Line, - Ada.Directories, + Misc, Ada.Text_IO, Ada.Strings.Fixed, Ada.Strings.Maps; @@ -41,9 +40,7 @@ package body Sokoban is type Game_State is (Loading, Play, Complete); - Origin : String := Ada.Directories.Containing_Directory - (Ada.Directories.Full_Name (Ada.Command_Line.Command_Name)); - Level_Dir : String := Origin & "/../share/sokoban/level"; + Level_Dir : String := Misc.Origin & "/../share/sokoban/level"; Loading_Message : String := "Loading..."; diff --git a/src/sokoban.ads b/src/sokoban.ads index ec88cc6..1f7fa17 100644 --- a/src/sokoban.ads +++ b/src/sokoban.ads @@ -6,13 +6,16 @@ package Sokoban is type LevelID is new Integer range 0 .. 50; + + procedure Load_Level (Number : in LevelID); - procedure Show; + procedure Show; + procedure Hide; diff --git a/src/squares.adb b/src/squares.adb index 673f425..91b1bd1 100644 --- a/src/squares.adb +++ b/src/squares.adb @@ -3,6 +3,18 @@ package body Squares is + function "=" + (A, B : in Square) + return Boolean is + begin + return + A.Walkable = B.Walkable and + A.Self_Image = B.Self_Image; + end "="; + + + + function Is_Walkable (This : in Square) return Boolean is @@ -21,8 +33,6 @@ package body Squares is end Get_Contents; - - procedure Set_Contents (This : in out Square; Item : in Things.Thing) is @@ -42,17 +52,5 @@ package body Squares is end Draw; - - - function "=" - (A, B : in Square) - return Boolean is - begin - return - A.Walkable = B.Walkable and - A.Self_Image = B.Self_Image; - end "="; - - end Squares; diff --git a/src/squares.ads b/src/squares.ads index 7f9b090..5aed2fb 100644 --- a/src/squares.ads +++ b/src/squares.ads @@ -7,8 +7,7 @@ with private with FLTK.Images.RGB.PNG, - Ada.Command_Line, - Ada.Directories; + Misc; package Squares is @@ -16,43 +15,45 @@ package Squares is type Square is tagged private; - Void : constant Square; - Wall : constant Square; Space : constant Square; Empty : constant Square; Goal : constant Square; + function "=" + (A, B : in Square) + return Boolean; + + + function Is_Walkable (This : in Square) return Boolean; + + function Get_Contents (This : in Square) return Things.Thing with Pre => Is_Walkable (This); - procedure Set_Contents (This : in out Square; Item : in Things.Thing) with Pre => Is_Walkable (This); + + procedure Draw (This : in Square; X, Y : in Integer); - function "=" - (A, B : in Square) - return Boolean; - - private @@ -63,9 +64,7 @@ private end record; - Origin : String := Ada.Directories.Containing_Directory - (Ada.Directories.Full_Name (Ada.Command_Line.Command_Name)); - Image_Dir : String := Origin & "/../share/sokoban/img"; + Image_Dir : String := Misc.Origin & "/../share/sokoban/img"; Wall_Image : aliased FLTK.Images.RGB.PNG.PNG_Image := diff --git a/src/things.adb b/src/things.adb index efd8b63..66cc07e 100644 --- a/src/things.adb +++ b/src/things.adb @@ -3,6 +3,16 @@ package body Things is + function "=" + (A, B : in Thing) + return Boolean is + begin + return A.Self_Image = B.Self_Image; + end "="; + + + + procedure Draw (This : in Thing; X, Y : in Integer) is @@ -13,15 +23,5 @@ package body Things is end Draw; - - - function "=" - (A, B : in Thing) - return Boolean is - begin - return A.Self_Image = B.Self_Image; - end "="; - - end Things; diff --git a/src/things.ads b/src/things.ads index c8da450..3babe65 100644 --- a/src/things.ads +++ b/src/things.ads @@ -3,8 +3,7 @@ private with FLTK.Images.RGB.PNG, - Ada.Command_Line, - Ada.Directories; + Misc; package Things is @@ -12,24 +11,23 @@ package Things is type Thing is tagged private; - Nothing : constant Thing; - Man : constant Thing; Treasure : constant Thing; + function "=" + (A, B : in Thing) + return Boolean; + + + procedure Draw (This : in Thing; X, Y : in Integer); - function "=" - (A, B : in Thing) - return Boolean; - - private @@ -38,9 +36,7 @@ private end record; - Origin : String := Ada.Directories.Containing_Directory - (Ada.Directories.Full_Name (Ada.Command_Line.Command_Name)); - Image_Dir : String := Origin & "/../share/sokoban/img"; + Image_Dir : String := Misc.Origin & "/../share/sokoban/img"; Man_Image : aliased FLTK.Images.RGB.PNG.PNG_Image := -- cgit