/*
	Rogue Trader.
	Copyright (c) 2007, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef GALAXY_H
#define GALAXY_H

//##############################################################################
// C Includes
//##############################################################################

#include <cstdlib>
#include <cmath>
#include "MathLibrary.h"

//##############################################################################
// Stuff that cannot be forward referenced
//##############################################################################

#include "StarSystem.h"

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   Galaxy
// Purpose:      Class that describes a galaxy full of stars. The galaxy is more
//               or less infinite (well as far as the player is concerned) and
//               generated using predictably random numbers. The galaxy also
//               maintains a stack of recently visited systems and simulates
//               econonmy between those systems.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

class Galaxy
{

 public:

        //----------------------------------------------------------------------
        // Constants
        //----------------------------------------------------------------------

        static const int        SECTOR_SIZE;                    // size of each sector in a galaxy. only one system may occupy a sector
        static const unsigned   SYSTEM_MEMORY_SIZE = 128;       // the number of star systems the galaxy can remember the state of

        //----------------------------------------------------------------------
        // Functions
        //----------------------------------------------------------------------

        // Constructor for the galaxy: takes a seed for generating random numbers

        Galaxy( unsigned seed );

        // Picks a valid random starting system for the player:

        StarSystem getStartSystem();

        // Get the star system in a particular sector. returns a 'null' system with no name if there is no system in the sector:

        StarSystem getSystem ( unsigned sectorX , unsigned sectorY ) const;

        // Adds the specified system into the galaxys memory of recently visited systems:

        void rememberSystem( StarSystem system );

        // Gets the seed for the galaxy:

        inline unsigned getSeed(){ return this->seed; }

        // Gets a system from the internal memory bank of start systems:

        inline StarSystem & getMemorisedSystem( unsigned slot )
        {
            if ( slot >= SYSTEM_MEMORY_SIZE )
            {
                return systemMemory[ SYSTEM_MEMORY_SIZE - 1 ];
            }
            else
            {
                return systemMemory[slot];
            }
        }

        // Sets a system in the internal memory bank:

        inline void setMemorisedSystem( StarSystem & system , unsigned slot )
        {
            if ( slot >= SYSTEM_MEMORY_SIZE )
            {
                systemMemory[ SYSTEM_MEMORY_SIZE - 1 ] = system;
            }
            else
            {
                systemMemory[slot] = system;
            }
        }

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

 protected:

        //----------------------------------------------------------------------
        // Constants
        //----------------------------------------------------------------------

        static const float BASE_STAR_FREQUENCY;     // frequency of a star appearing in a sector at the galaxy center. frequency decreases as we go from the center.

        //----------------------------------------------------------------------
        // Variables
        //----------------------------------------------------------------------

        // seed for the galaxy:

        unsigned seed;

        // Memory of star systems recently visited: most recent system is at forefront of array

        StarSystem systemMemory[SYSTEM_MEMORY_SIZE];

}; // end of: class Galaxy{}

#endif // end of: ifndef GALAXY_H
