/*
* Building.cs
* Authors: Bradley Blankenship
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using System.Xml;
namespace ColonySim.Buildings
{
///
/// Represents a building in the system.
///
internal abstract class Building
{
#region Fields
// The name of the building
string mName;
// The name of the model for the building
string mModelName;
// The name of the texture for the model
string mTextureName;
// The name of the icon for the building
string mIconName;
// The footprint size of the building
int mFootPrintSize;
// The amount of people it takes to run this
int mRequiredUnskilledWorkers;
int mRequiredSkilledWorkers;
int mRequiredScientists;
// The number of actual workers in the building
int mUnskilledWorkers;
int mSkilledWorkers;
int mScientists;
// The map of resource names and costs
Resource mCosts;
// The map of resources and costs for upkeep
Resource mUpkeep;
// The upgrade costs for upgrading
Resource mUpgradeCosts;
// The target building to upgrade to
string mTargetUpgrade;
// The upgrade technologies for upgrading
List mUpgradeTechnologies;
// The costs string used for initializing the costs of the building
protected string mCostsString;
// The costs string used for initializing the upkeep costs of the building
protected string mUpkeepString;
// The upgrade string used for initializing the upgrade requirements
protected string mUpgradeString;
#endregion
#region Properties
///
/// Allows access to the name of the building.
///
public string Name
{
get { return mName; }
set { mName = value; }
}
///
/// Allows access to the name of the building's model.
///
public string ModelName
{
get { return mModelName; }
set { mModelName = value; }
}
///
/// Allows access to the name of the building's texture.
///
public string TextureName
{
get { return mTextureName; }
set { mTextureName = value; }
}
///
/// Allows access to the name of the icon for the building
///
public string IconName
{
get { return mIconName; }
set { mIconName = value; }
}
///
/// Allows access to the footprint size of the building.
///
public int FootPrintSize
{
get { return mFootPrintSize; }
set { mFootPrintSize = value; }
}
///
/// Allows the viewing and setting of the costs for the Building.
///
public Resource Costs
{
get { return mCosts; }
set { mCosts = value; }
}
///
/// Allows access to the required number of unskilled workers.
///
public int RequiredUnskilledWorkers
{
get { return mRequiredUnskilledWorkers; }
set { mRequiredUnskilledWorkers = value; }
}
///
/// Allows access to the required number of skilled workers.
///
public int RequiredSkilledWorkers
{
get { return mRequiredSkilledWorkers; }
set { mRequiredSkilledWorkers = value; }
}
///
/// Allows access to the required number of scientists.
///
public int RequiredScientists
{
get { return mRequiredScientists; }
set { mRequiredScientists = value; }
}
///
/// Allows access to the actual numbers of unskilled workers in the
/// building.
///
public int UnskilledWorkers
{
get { return mUnskilledWorkers; }
set { mUnskilledWorkers = value; }
}
///
/// Allows access to the actual numbers of skilled workers in the
/// building.
///
public int SkilledWorkers
{
get { return mSkilledWorkers; }
set { mSkilledWorkers = value; }
}
///
/// Allows access to the actual numbers of scientists in the
/// building.
///
public int Scientists
{
get { return mScientists; }
set { mScientists = value; }
}
///
/// Allows access to the Upkeep resource.
///
public Resource Upkeep
{
get { return mUpkeep; }
set { mUpkeep = value; }
}
///
/// Allows access to the upgrade costs
///
public Resource UpgradeCost
{
get { return mUpgradeCosts; }
set { mUpgradeCosts = value; }
}
///
/// Allows viewing and use of the upgrade technology list.
///
public List UpgradeTechnologies
{
get { return mUpgradeTechnologies; }
}
///
/// Allows access to the building this can be upgraded to.
///
public string TargetUpgrade
{
get { return mTargetUpgrade; }
set { mTargetUpgrade = value; }
}
#endregion
#region Constructs
///
/// Base constructor
///
public Building()
{
// Initialize all values needed
mCosts = new Resource();
mUpkeep = new Resource();
mUpgradeCosts = new Resource();
mUpgradeTechnologies = new List();
}
#endregion
#region Initialization
///
/// Iinitialize the building, loading the content required if needed.
///
public virtual void Initialize()
{
// Get the array of pairs for the resources for the costs
string[] stringArray = mCostsString.Split(';');
// Now generate the resources based on the pairs
foreach (string pair in stringArray)
{
// Get the string for the resource name and the cost value
string name = pair.Split(',')[0].Trim();
float value = float.Parse(pair.Split(',')[1].Trim());
// Add the new resource
mCosts += new SimpleResource((Resources)Enum.Parse(typeof(Resources), name), value);
}
// Get the array of pairs for the resources for the upkeep
stringArray = mUpkeepString.Split(';');
// Now generate the resources based on the pairs
foreach (string pair in stringArray)
{
// Get the string for the resource name and the upkeep value
string name = pair.Split(',')[0].Trim();
float value = float.Parse(pair.Split(',')[1].Trim());
// Add the new resource
mUpkeep += new SimpleResource((Resources)Enum.Parse(typeof(Resources), name), value);
}
// Get the array of strings for the upgrade and associated costs
stringArray = mUpgradeString.Split(':');
// Proceed only if the string has a size of 3
if (stringArray.Length == 3)
{
// Set the target upgrade string
mTargetUpgrade = stringArray[0].Trim();
// Now get the array of technologies required to upgrade
string[] technologies = stringArray[1].Split(',');
// Add each of the technologies to the list of upgrade technologies
foreach (string tech in technologies)
{
// Add the technology
mUpgradeTechnologies.Add(tech.Trim());
}
// Now get the array of resource pairs required for the upgrade costs
stringArray = stringArray[2].Split(';');
// Now generate the resources based on the pairs
foreach (string pair in stringArray)
{
// Get the string for the resource name and the upgrade value
string name = pair.Split(',')[0].Trim();
float value = float.Parse(pair.Split(',')[1].Trim());
// Add the new resource
mUpgradeCosts += new SimpleResource((Resources)Enum.Parse(typeof(Resources), name), value);
}
}
}
#endregion
#region Update
///
/// Update the internal data of the building.
///
/// The current game time.
public abstract void Update(GameTime gameTime);
#endregion
#region Utility
///
/// Reads in the base data for a given building into the values. Requires
/// that the input text reader is already at the proper position for reading the data.
///
/// The reader to read the data.
protected virtual void ReadData(XmlTextReader input)
{
// Read the base building values
this.Name = input.GetAttribute("Name");
this.ModelName = @"Models\" + input.GetAttribute("ModelName");
this.TextureName = @"Textures\" + input.GetAttribute("TextureName");
this.IconName = @"Textures\" + input.GetAttribute("IconName");
this.FootPrintSize = Int32.Parse(input.GetAttribute("FootPrintSize"));
this.mCostsString = input.GetAttribute("Costs");
this.RequiredUnskilledWorkers = Int32.Parse(input.GetAttribute("RequiredUnskilledWorkers"));
this.RequiredSkilledWorkers = Int32.Parse(input.GetAttribute("RequiredSkilledWorkers"));
this.RequiredScientists = Int32.Parse(input.GetAttribute("RequiredScientists"));
this.mUpkeepString = input.GetAttribute("Upkeep");
this.mUpgradeString = input.GetAttribute("Upgrade");
}
///
/// Return the information of the building in string form.
///
/// Returns the basic building information.
public override string ToString()
{
// Get the name, model name, and footprint on one line
StringBuilder retVal = new StringBuilder();
retVal.Append(Name);
retVal.Append(", ");
retVal.Append(ModelName);
retVal.Append(", ");
retVal.Append(FootPrintSize);
retVal.Append('\n');
// Now the worker requirements
retVal.Append("Unskilled Workers: ");
retVal.Append(mUnskilledWorkers);
retVal.Append(" / ");
retVal.Append(mRequiredUnskilledWorkers);
retVal.Append("\n");
retVal.Append("Skilled Workers: ");
retVal.Append(mSkilledWorkers);
retVal.Append(" / ");
retVal.Append(mRequiredSkilledWorkers);
retVal.Append("\n");
// Now do costs
retVal.Append("Costs:\n");
retVal.Append(mCosts.ToString());
// Now do upkeep
retVal.Append("Upkeep:\n");
retVal.Append(mUpkeep.ToString());
// Now do the upgrade
retVal.Append("Upgrade:\n");
// Do this if we have an upgrade target
if (mTargetUpgrade != null)
{
// Add the building name to upgrade to
retVal.Append(mTargetUpgrade);
retVal.Append('\n');
// Add the technologies to it
foreach (string tech in mUpgradeTechnologies)
{
retVal.Append(tech);
retVal.Append(", ");
}
// Add an endline
retVal.Append('\n');
// Add each of the resource costs for upgrading
retVal.Append(mUpgradeCosts.ToString());
}
// Return the basic information
return retVal.ToString();
}
#endregion
}
}