/* * Outpost.cs * Authors: Bradley R. 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; namespace ColonySim.Structures { /// /// Represents an outpost in outer space. Outposts can be used to harvest /// resources from nearby Resource Containers, transport resources via /// connected Tubes, etc. /// class Outpost : Structure { #region Fields // The array of connection tubes this can have List mConnections; // The input resource Resource mInputResource; // The output resource Resource mOutputResource; // The temp storage this has for resources int mStorage; #endregion #region Properties /// /// Allows access to the connections this Outpost has. /// public List Connections { get { return mConnections; } } /// /// Allows access to the input resource. /// public Resource InputResource { get { return mInputResource; } set { mInputResource = value; } } /// /// Allows access to the output resource. /// public Resource OutputResource { get { return mOutputResource; } set { mOutputResource = value; } } #endregion #region Constructs /// /// Create the given structure. /// /// The content manager /// The name of our model /// The name of our texture public Outpost(ContentManager pContent, string pModelName, string pTextureName) : base(pContent,pModelName,pTextureName) { // Create our list mConnections = new List(); } #endregion #region Behavior /// /// Process the current turn. /// public override void Tick() { // Process our current resources ProcessResources(); } /// /// Receive the given carrier. /// /// The current carrier. /// Whether we received it or not. public override bool ReceiveCarrier(Carrier pCarrier) { // Make the carrier halt pCarrier.Direction = Carrier.Directions.None; // The return value bool retVal = false; // If the resource it has is the same as ours if (InputResource!= null && pCarrier.Resource.ResourceCode == InputResource.ResourceCode) { // Store the value mStorage += pCarrier.Resource.Amount; // Now set the retval to be true retVal = true; } // Report back the gains of the carrier GridReference.StatusInfo.Money += pCarrier.Value; // Dispose of the Carrier pCarrier.Dispose(); // Return true as the base is return retVal; } /// /// Processes the resources that this Outpost has. /// private void ProcessResources() { // If we have enough resources in storage or don't require an input if (mInputResource == null || mInputResource.Amount < mStorage) { // Then, if we have a production item if (mOutputResource != null && mConnections.Count > 0) { // The output tubes List outputs = new List(); // Get the output tubes foreach (Tube tube in mConnections) { // See if it is facing the right way to be an output tube, and it's not null if ((tube.GridPosition.X < this.GridPosition.X && tube.Running == Tube.Runnings.Negative_X) || (tube.GridPosition.X > this.GridPosition.X && tube.Running == Tube.Runnings.Positive_X) || (tube.GridPosition.Y < this.GridPosition.Y && tube.Running == Tube.Runnings.Negative_Y) || (tube.GridPosition.Y > this.GridPosition.Y && tube.Running == Tube.Runnings.Positive_Y)) { // Add the tube outputs.Add(tube); } } // See if we have any output tubes if (outputs.Count > 0) { // Set the value appropriately Resource output = mOutputResource.Clone(); output.Amount /= outputs.Count; // Now for each of the connections, do the following foreach (Tube tube in outputs) { // Output the given resource, with a new carrier Carrier carrier = new Carrier( this.Content, @"Models\TestCarrier", @"Textures\BluePixel"); carrier.Resource = output.Clone(); carrier.TransformationMatrix = Matrix.CreateTranslation( tube.TransformationMatrix.Translation); carrier.GridPosition = tube.GridPosition; carrier.DrawOrder = tube.DrawOrder - 1; // Tell the tube to receive the connection tube.ReceiveCarrier(carrier); // Add the carrier to the grid GridReference.AddCarrier(carrier); } } } // If we have an input resource if (mInputResource != null) { // Consume resources from the storage mStorage -= mInputResource.Amount; } } } #endregion #region Utility /// /// Returns a clone of this structure. /// /// The cloned structure. public override Structure Clone() { // Create the return value Outpost retVal = new Outpost(this.Content, this.ModelPath, this.TexturePath); // Set the values to ours retVal.Name = this.Name; retVal.FootPrintSize = this.FootPrintSize; retVal.Cost = this.Cost; retVal.DrawOrder = this.DrawOrder; retVal.TechniqueName = this.TechniqueName; // If we have an input resource if (mInputResource != null) { // Copy the input resource retVal.InputResource = this.InputResource.Clone(); } // If we have an output resource if (mOutputResource != null) { // Copy the output resource retVal.OutputResource = this.OutputResource.Clone(); } // Return the retval return retVal; } #endregion } }