1. -- 
  2. -- Copyright (c) 2012 Kevin Wellwood 
  3. -- All rights reserved. 
  4. -- 
  5. -- This source code is distributed under the Modified BSD License. For terms and 
  6. -- conditions, see license.txt. 
  7. -- 
  8.  
  9. with Ada.Streams;                       use Ada.Streams; 
  10. with Entities;                          use Entities; 
  11. with Entities.Players;                  use Entities.Players; 
  12. with Events;                            use Events; 
  13. with Events.Corrals;                    use Events.Corrals; 
  14. with Events.Entities;                   use Events.Entities; 
  15. with Events.Listeners;                  use Events.Listeners; 
  16. with Game_States;                       use Game_States; 
  17. with Objects;                           use Objects; 
  18. with Processes;                         use Processes; 
  19. with Scripting;                         use Scripting; 
  20. with Tiles.Libraries;                   use Tiles.Libraries; 
  21. with Values;                            use Values; 
  22.  
  23. private with Ada.Containers; 
  24. private with Ada.Containers.Hashed_Sets; 
  25. private with Ada.Containers.Ordered_Maps; 
  26. private with Ada.Real_Time; 
  27. private with Ada.Strings.Unbounded; 
  28. private with Maps; 
  29. private with Tiles; 
  30.  
  31. package Worlds is 
  32.  
  33.     -- A World is an object which contains a map and entities. It can be 
  34.     -- created, loaded and saved as a simple data object and it can be copied. 
  35.     -- 
  36.     -- Worlds are also Processes and Event_Listeners that can be attached to the 
  37.     -- game framework. Only one World at a time can be attached: the game 
  38.     -- session's currently active world. This is enforced by the Game logic that 
  39.     -- owns the active world. Attaching a World to the game framework means 
  40.     -- giving it a Corral so it can register to receive events, evaluating all 
  41.     -- its entities' OnAttach actions, and queueing events to notify the view of 
  42.     -- the world. An attached World object has behavior; it listens for events, 
  43.     -- queues events, and executes as a Process. 
  44.     -- 
  45.     -- When a world is attached, its On_Attach procedure is called, which queues 
  46.     -- load-time events and register the object as an event listener. When a 
  47.     -- world is detached from the game framework, its On_Detach procedure is 
  48.     -- called to unregister the object as an event listener, etc. A World object 
  49.     -- can't be copied while it is the current active world. 
  50.     -- 
  51.     -- An unattached World is a "dumb" object, not having any autonomous 
  52.     -- behavior. 
  53.     type World_Object is abstract new Object and 
  54.                                       Event_Listener and 
  55.                                       Process and 
  56.                                       Evaluation_Node with private; 
  57.     type A_World is access all World_Object'Class; 
  58.  
  59.     -- Creates a new empty world. An exception is raised on error. 
  60.     function Create_World( width, 
  61.                            height  : Positive; 
  62.                            libName, 
  63.                            domain  : String ) return A_World; 
  64.     pragma Precondition( libName'Length > 0 ); 
  65.     pragma Precondition( domain'Length > 0 ); 
  66.     pragma Postcondition( Create_World'Result /= null ); 
  67.  
  68.     -- Loads a world from disk. An exception is raised on error. 
  69.     function Load_World( name : String ) return A_World; 
  70.     pragma Postcondition( Load_World'Result /= null ); 
  71.  
  72.     -- Attaches the world to the game framework to send and receive events. 
  73.     procedure Attach_To_Framework( this   : not null access World_Object'Class; 
  74.                                    game   : not null A_Game_State; 
  75.                                    corral : not null A_Corral ); 
  76.  
  77.     -- Detaches the world from the game framework, removing itself as an event 
  78.     -- listener and ceasing execution as a process. 
  79.     procedure Detach_From_Framework( this : not null access World_Object'Class ); 
  80.  
  81.     -- Evaluates the script function 'name', given 'arguments'. The value of the 
  82.     -- evaluated function will be returned, or null if the function name is not 
  83.     -- recognized. 
  84.     -- todo: make this private (public due to 6.2.0w bug) 
  85.     function Evaluate_Function( this      : access World_Object; 
  86.                                 name      : String; 
  87.                                 arguments : Value_Array ) return A_Value; 
  88.  
  89.     -- Evaluates the symbol named 'symbol', resolving it as a game session 
  90.     -- variable. null will be returned if the symbol can't be resolved. 
  91.     -- todo: make this private (public due to 6.2.0w bug) 
  92.     function Evaluate_Symbol( this : access World_Object; symbol : String ) return A_Value; 
  93.  
  94.     -- Iterate through all of the entities in the world, examining each. 
  95.     procedure Examine_Entities( this    : not null access World_Object'Class; 
  96.                                 examine : not null access procedure( e : not null A_Entity ) ); 
  97.  
  98.     -- Returns a reference to an entity with the given id, or null if the entity 
  99.     -- doesn't exist. 
  100.     function Get_Entity( this : not null access World_Object'Class; 
  101.                          id   : Entity_Id ) return A_Entity; 
  102.  
  103.     -- The name of the file that this world was loaded from (including the file 
  104.     -- extension), if it was loaded. If this world was not loaded from disk or 
  105.     -- has not been saved to the disk yet then an empty string will be returned. 
  106.     function Get_Filename( this : not null access World_Object'Class ) return String; 
  107.  
  108.     -- Returns the World's attached Game class, if it has been attached to the 
  109.     -- framework. 
  110.     function Get_Game_State( this : not null access World_Object'Class ) return A_Game_State; 
  111.  
  112.     -- Returns the actual map height in pixels. 
  113.     function Get_Height( this : not null access World_Object'Class ) return Positive; 
  114.  
  115.     -- Returns the height of the map in tiles. 
  116.     function Get_Height_Tiles( this : not null access World_Object'Class ) return Positive; 
  117.  
  118.     -- Returns the world's reference to its tile library. Do not modify it. 
  119.     function Get_Library( this : not null access World_Object'Class ) return A_Tile_Library; 
  120.  
  121.     -- Returns the name of the background music track. 
  122.     function Get_Music( this : not null access World_Object'Class ) return String; 
  123.  
  124.     -- Returns a reference to the world's current player entity. Do not modify 
  125.     -- it. 
  126.     function Get_Player( this : not null access World_Object'Class ) return A_Player; 
  127.  
  128.     -- Returns the id of the tile at the specified location. 
  129.     function Get_Tile_Id( this  : not null access World_Object'Class; 
  130.                           layer, 
  131.                           x, y  : Integer ) return Natural; 
  132.  
  133.     -- Returns the title of the world. 
  134.     function Get_Title( this  : not null access World_Object'Class ) return String; 
  135.  
  136.     -- Returns the actual map width in pixels. 
  137.     function Get_Width( this : not null access World_Object'Class) return Positive; 
  138.  
  139.     -- Returns the width of the map in tiles. 
  140.     function Get_Width_Tiles( this : not null access World_Object'Class ) return Positive; 
  141.  
  142.     -- Resizes the map. An exception is raised on error. 
  143.     procedure Resize( this   : not null access World_Object'Class; 
  144.                       width, 
  145.                       height : Positive ); 
  146.  
  147.     -- Writes the world in its current state to a file on disk. An exception is 
  148.     -- raised on error. 
  149.     procedure Save( this      : not null access World_Object'Class; 
  150.                     name      : String; 
  151.                     overwrite : Boolean := True ); 
  152.     pragma Precondition( name'Length > 0 ); 
  153.  
  154.     -- basic properties: 
  155.     -- "filename" : the filename 
  156.     -- "domain"   : determines player movement, physical rules 
  157.     -- "title"    : the readable name of the world 
  158.     -- "music"    : the background music track name 
  159.     procedure Set_Property( this : access World_Object; name, value : String ); 
  160.     pragma Precondition( name'Length > 0 ); 
  161.  
  162.     -- If 'notify' is True, Tile_Changed and World_Modified events will be sent. 
  163.     procedure Set_Tile( this   : not null access World_Object'Class; 
  164.                         layer  : Integer; 
  165.                         x, y   : Integer; 
  166.                         id     : Natural; 
  167.                         notify : Boolean := True ); 
  168.  
  169.     -- x, y are in pixel coordinates, not tile coordinates. If 'notify' is True, 
  170.     -- Tile_Changed and World_Modified events will be sent. 
  171.     procedure Set_Tile( this   : not null access World_Object'Class; 
  172.                         layer  : Integer; 
  173.                         x, y   : Float; 
  174.                         id     : Natural; 
  175.                         notify : Boolean := True ); 
  176.  
  177.     -- Spawns an entity of the given class id into the world. If 'width' or 
  178.     -- 'height' are equal to 0, the entity's natural width and height will be 
  179.     -- used. 
  180.     procedure Spawn_Entity( this   : not null access World_Object'Class; 
  181.                             id     : String; 
  182.                             x, y   : Float; 
  183.                             width, 
  184.                             height : Natural := 0; 
  185.                             xv, yv : Float := 0.0 ); 
  186.     pragma Precondition( id'Length > 0 ); 
  187.  
  188.     -- Returns the width of each tile in the world, in pixels. 
  189.     function Tile_Width( this : not null access World_Object'Class ) return Positive; 
  190.  
  191.     -- Implement this function to Construct and Read instances of the concrete 
  192.     -- subclass from a stream. 
  193.     function Object_Input( stream : access Root_Stream_Type'Class ) return World_Object is abstract; 
  194.  
  195.     -- Worlds can't be copied; Raises COPY_NOT_ALLOWED. 
  196.     function Copy( src : A_World ) return A_World; 
  197.     pragma Postcondition( Copy'Result /= src or else src = null ); 
  198.  
  199.     -- Deletes the World. 
  200.     procedure Delete( this : in out A_World ); 
  201.     pragma Postcondition( this = null ); 
  202.  
  203.     ---------------------------------------------------------------------------- 
  204.  
  205.     -- Returns True if 'domain' is a registered domain name. Case sensitive. 
  206.     function Valid_Domain( domain : String ) return Boolean; 
  207.  
  208.     -- Returns the file extension for World files, without a leading dot. 
  209.     function World_Extension return String; 
  210.     pragma Postcondition( World_Extension'Result'Length > 0 ); 
  211.  
  212.     FILE_NOT_FOUND, 
  213.     READ_EXCEPTION, 
  214.     WRITE_EXCEPTION : exception; 
  215.  
  216. private 
  217.  
  218.     use Ada.Containers; 
  219.     use Ada.Real_Time; 
  220.     use Ada.Strings.Unbounded; 
  221.     use Maps; 
  222.     use Tiles; 
  223.  
  224.     -- Maps Entity_Ids to Entity objects. 
  225.     package Entity_Map is new Ada.Containers.Ordered_Maps( Entity_Id, A_Entity, "<", "=" ); 
  226.  
  227.     -- There are two kinds of tile animation: looping animations and one-shot. 
  228.     -- 
  229.     -- In the case of a one-shot animation, each frame progresses to the next 
  230.     -- after a fixed amount of time, beginning the moment the tile is set into 
  231.     -- the world. Generally, the last frame of a one-shot animation will end by 
  232.     -- progressing to a non-animated tile. It is possible for a frame in a 
  233.     -- one-shot animation to jump back to a previous frame, creating a frame 
  234.     -- cycle, but this is generally not very useful. 
  235.     -- 
  236.     -- Looping animations have a list of frames and a single delay. The biggest 
  237.     -- difference between looping and one-shot animations is that the animation 
  238.     -- frame is globally updated at specific time slices, based on the animation 
  239.     -- frame delay. This means that two world locations with the same tile id 
  240.     -- will always update simultaneously, regardless of being set into the map 
  241.     -- at different times or not. 
  242.     -- 
  243.     -- An animation is looping if the frames field of the Animated_Info is 
  244.     -- non-null, otherwise it's a one-shot. 
  245.     type Animated_Info is 
  246.         record 
  247.             layer      : Integer;           -- layer of animated tile 
  248.             x, y       : Integer;           -- coordinates of animated tile 
  249.             nextUpdate : Time;              -- next time to update the frame 
  250.             frameDelay : Time_Span;         -- (for looping) animation frame delay 
  251.             frames     : A_Tile_Id_Array;   -- (for looping) frame list 
  252.         end record; 
  253.  
  254.     -- Deletes the members of the Animated_Info record so it can be discarded. 
  255.     procedure Delete( ai : in out Animated_Info ); 
  256.  
  257.     -- Returns True if 'l' and 'r' share the same world location. 
  258.     function Equivalent( l, r : Animated_Info ) return Boolean; 
  259.  
  260.     -- Returns a hash value of the Animated_Info record. 
  261.     function Hash( ai : Animated_Info ) return Hash_Type; 
  262.  
  263.     -- Returns True if the animation is played as a loop. 
  264.     function Is_Looping( ai : Animated_Info ) return Boolean; 
  265.  
  266.     function Animated_Info_Input( stream : access Root_Stream_Type'Class ) return Animated_Info; 
  267.     for Animated_Info'Input use Animated_Info_Input; 
  268.  
  269.     procedure Animated_Info_Output( stream : access Root_Stream_Type'Class; info : Animated_Info ); 
  270.     for Animated_Info'Output use Animated_Info_Output; 
  271.  
  272.     package Animated_Set is new Ada.Containers.Hashed_Sets( Animated_Info, 
  273.                                                             Hash, Equivalent, 
  274.                                                             "=" ); 
  275.  
  276.     ---------------------------------------------------------------------------- 
  277.  
  278.     type World_Object is abstract new Object and 
  279.                                       Event_Listener and 
  280.                                       Process and 
  281.                                       Evaluation_Node with 
  282.         record 
  283.             -- the following fields are Not streamed 
  284.             attached  : Boolean := False; 
  285.             game      : A_Game_State; 
  286.             corral    : A_Corral; 
  287.  
  288.             filename : Unbounded_String; 
  289.             lib      : A_Tile_Library := null; 
  290.             player   : Entity_Id := INVALID_ID; 
  291.  
  292.             -- the following fields are streamed 
  293.             map      : A_Map := null; 
  294.             libName  : Unbounded_String; 
  295.             domain   : Unbounded_String; 
  296.             title    : Unbounded_String; 
  297.             music    : Unbounded_String; 
  298.             entities : Entity_Map.Map; 
  299.             animated : Animated_Set.Set; 
  300.         end record; 
  301.  
  302.     procedure Adjust( this : access World_Object ); 
  303.  
  304.     -- Construction with an empty filename, map, libName and domain is allowed 
  305.     -- if the object is being constructed as part of a streaming read operation. 
  306.     -- The fields should be be immediately read in before the World is returned 
  307.     -- to the application for use. 
  308.     procedure Construct( this     : access World_Object; 
  309.                          filename : String; 
  310.                          map      : in out A_Map; 
  311.                          libName, 
  312.                          domain   : String ); 
  313.  
  314.     procedure Delete( this : in out World_Object ); 
  315.  
  316.     -- Returns the name of the World as a Process. 
  317.     function Get_Process_Name( this : access World_Object ) return String; 
  318.     pragma Postcondition( Get_Process_Name'Result'Length > 0 ); 
  319.  
  320.     -- Handles a Delete_Entity event. 
  321.     procedure Handle_Delete_Entity( this : access World_Object; 
  322.                                     evt  : not null A_Delete_Entity_Event ); 
  323.  
  324.     -- Handles an Entity_Face event. 
  325.     procedure Handle_Entity_Face( this : access World_Object; 
  326.                                   evt  : not null A_Entity_Face_Event ); 
  327.  
  328.     -- Handles an Entity_Grounded event. 
  329.     procedure Handle_Entity_Grounded( this : access World_Object; 
  330.                                       evt  : not null A_Entity_Grounded_Event ); 
  331.  
  332.     -- Handles an Entity_Hit_Wall event. 
  333.     procedure Handle_Entity_Hit_Wall( this : access World_Object; 
  334.                                       evt  : not null A_Entity_Hit_Wall_Event ); 
  335.  
  336.     -- Handles an Entity_Moved event. 
  337.     procedure Handle_Entity_Moved( this : access World_Object; 
  338.                                    evt  : not null A_Entity_Moved_Event ); 
  339.  
  340.     -- Handles an Entity_Resized event. 
  341.     procedure Handle_Entity_Resized( this : access World_Object; 
  342.                                      evt  : not null A_Entity_Resized_Event ); 
  343.  
  344.     -- Handles a Set_Entity_Attribute event. 
  345.     procedure Handle_Set_Entity_Attribute( this : access World_Object; 
  346.                                            evt  : not null A_Set_Entity_Attribute_Event ); 
  347.  
  348.     -- Handles a Spawn_Entity event. 
  349.     procedure Handle_Spawn_Entity( this : access World_Object; 
  350.                                    evt  : not null A_Spawn_Entity_Event ); 
  351.  
  352.     -- Handles all events the World receives and dispatches to the specific 
  353.     -- Handle procedure. If this procedure is overridden, the implementation 
  354.     -- should handle 'evt' if it's special. If the subclass doesn't do anything 
  355.     -- special with it, then it should call this implementation to dispatch it. 
  356.     procedure Handle_Event( this : access World_Object; 
  357.                             evt  : in out A_Event; 
  358.                             resp : out Response_Type ); 
  359.     pragma Precondition( evt /= null ); 
  360.  
  361.     -- Performs initialization immediately after load that can't be performed 
  362.     -- during construction, like passing a reference to 'this' to child objects 
  363.     -- (ie: entitites) that need it. An overriding implementation should call 
  364.     -- this first. 
  365.     procedure Initialize( this : access World_Object ); 
  366.  
  367.     procedure Object_Read( stream : access Root_Stream_Type'Class; obj : out World_Object ); 
  368.     for World_Object'Read use Object_Read; 
  369.  
  370.     procedure Object_Write( stream : access Root_Stream_Type'Class; obj : World_Object ); 
  371.     for World_Object'Write use Object_Write; 
  372.  
  373.     -- This is called when the World is attached to the Game framework, to 
  374.     -- register for events and execute entities' On_Load handlers. Override this 
  375.     -- procedure to register the world to listen for special events or to queue 
  376.     -- any special events on load. An overriding implementation should call this 
  377.     -- first. 
  378.     procedure On_Attach( this : access World_Object ); 
  379.  
  380.     -- This is called when the World is removed from the Game framework, to 
  381.     -- unregister for the events it previously registered for with On_Attach. 
  382.     -- If On_Attach is overridden, On_Detach should be overridden too. The 
  383.     -- world can't remain registered as a listener for any event type on detach. 
  384.     -- An overriding implementation should call this first. 
  385.     procedure On_Detach( this : access World_Object ); 
  386.  
  387.     -- Queues world loaded events in order to send the world to the game views. 
  388.     -- This includes the events World_Loaded, World_Property_Changed, 
  389.     -- Entity_Created, and Follow_Entity. This procedure is called as part of 
  390.     -- On_Attach. An overriding implementation should call this first. 
  391.     procedure Queue_Load_Events( this : access World_Object ); 
  392.  
  393.     -- Executes one frame of the world behavior; updates entities and animated 
  394.     -- tiles. An overriding implementation should call this first. 
  395.     procedure Tick( this : access World_Object; time : Tick_Time ); 
  396.  
  397.     -- Verifies a file format header before reading the World representation. 
  398.     -- If the header is invalid, READ_EXCEPTION will be raised. Raises an 
  399.     -- exception on streaming error. 
  400.     function A_World_Input( stream : access Root_Stream_Type'Class ) return A_World; 
  401.     for A_World'Input use A_World_Input; 
  402.  
  403.     -- Writes a file format header and then the World representation. 
  404.     procedure A_World_Output( stream : access Root_Stream_Type'Class; world : A_World ); 
  405.     for A_World'Output use A_World_Output; 
  406.  
  407.     -- Reads the concrete World tag and instantiates it. A Constraint_Error will 
  408.     -- be raised if the class is unknown. Raises an exception on streaming 
  409.     -- error. 
  410.     procedure A_World_Read( stream : access Root_Stream_Type'Class; world : out A_World ); 
  411.     for A_World'Read use A_World_Read; 
  412.  
  413.     -- Writes the conrete World tag. 
  414.     procedure A_World_Write( stream : access Root_Stream_Type'Class; world : A_World ); 
  415.     for A_World'Write use A_World_Write; 
  416.  
  417.     ---------------------------------------------------------------------------- 
  418.  
  419.     -- An allocator function for Creating a new World. 'width' and 'height' are 
  420.     -- the size of the world in tiles. 'libName' is the library that contains 
  421.     -- the tiles used in the world. 'domain' is the world domain, which 
  422.     -- determines physics constants, the player entity, and available enemy 
  423.     -- types. 
  424.     type Allocator is 
  425.         access function( width, 
  426.                          height  : Positive; 
  427.                          libName, 
  428.                          domain  : String ) return A_World; 
  429.  
  430.     -- Registers all the information pertinent to the application's concrete 
  431.     -- World implementation: an allocator for creating a new World instance, 
  432.     -- the world format identifier string, and the World format version number. 
  433.     procedure Register( allocate      : not null Allocator; 
  434.                         identifier    : String; 
  435.                         read_version, 
  436.                         write_version : Positive ); 
  437.     pragma Precondition( identifier'Length > 0 ); 
  438.  
  439.     -- Registers 'domain' as a valid domain for Worlds. Case sensitive. 
  440.     procedure Register_Domain( domain : String ); 
  441.     pragma Precondition( domain'Length > 0 ); 
  442.  
  443.     -- Returns a string that identifies the world index file format. It is the 
  444.     -- first data found in the file. 
  445.     function World_Identifier return String; 
  446.  
  447. end Worlds;