/*
* Building.cs
* Authors: August Zinsser
*
* Copyright Matthew Belmonte 2007
*/
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using tAC_Engine;
using Pina3D;
namespace Astropolis.ColonySimulator
{
///
/// A building that outputs the same commodities it inputs, providing a place to cache commodities between buildings or the player.
/// These are special because the game treats them differently than standard buildings, although they behave in the same fashion.
///
public class Warehouse : Building
{
///
/// In warehouses, the output and input inventories are the same
///
public override Dictionary InputInventory { get { return ((WarehouseState)mState).Inventory; } }
///
/// In warehouses, the output and input inventories are the same
///
public override Dictionary OutputInventory { get { return ((WarehouseState)mState).Inventory; } }
///
/// Create a new Warehouse
///
public Warehouse(WarehouseType type, StructureState state)
{
mType = type;
mState = new WarehouseState(state);
Pina.AddRenderable(type.Model);
// Flesh out the inventory
foreach (string commodity in type.CanHold)
((WarehouseState)mState).Inventory.Add(commodity, 0);
}
///
/// Attempts to add some quantity of a commodity to the warehouse
///
/// True if successfully added, false otherwise
public override bool Deliver(string commodity, int quantity)
{
// Must be enough space in the inventory
if (HaveEnoughSpace(commodity, quantity))
{
((WarehouseState)mState).Inventory[commodity] += quantity;
return true;
}
return false;
}
///
/// Attempts to subtract the specified quantity of the specified commodity from this warehouse's inventory
///
/// True if successfully removed, false otherwise
public override bool Ship(string commodity, int quantity)
{
if (((WarehouseState)mState).Inventory.ContainsKey(commodity) &&
((WarehouseState)mState).Inventory[commodity] >= quantity)
{
((WarehouseState)mState).Inventory[commodity] -= quantity;
return true;
}
return false;
}
///
/// Return true if there is enough space in the inventory to store the specified commodities
///
/// False if this warehouse type cannot store the given commodity or the total of all commodities plus the given parameter exceeds this type's capacity
protected bool HaveEnoughSpace(string commodity, int quantity)
{
if (!((WarehouseType)mType).CanHold.Contains(commodity))
return false;
int runningTotal = 0;
foreach (string commodityName in ((WarehouseType)mType).CanHold)
{
runningTotal += ((WarehouseState)mState).Inventory[commodityName];
}
if (runningTotal + quantity > ((WarehouseType)mType).Capacity)
return false;
return true;
}
///
/// Current quantity / max capacity
///
public float PercentageInventory()
{
int runningTotal = 0;
foreach (string commodity in ((WarehouseType)mType).CanHold)
{
runningTotal += ((WarehouseState)mState).Inventory[commodity];
}
return (float)runningTotal/(float)((WarehouseType)mType).Capacity;
}
///
/// Perform self-maintanance
///
///
public override void Update(float dT)
{
base.Update(dT);
}
protected class WarehouseState : StructureState
{
///
/// The commodities this building is currently holding
///
public Dictionary Inventory = new Dictionary();
public WarehouseState(StructureState state)
: base(state.Matrix, state.BoundingBox)
{
}
}
}
public class WarehouseType : BuildingType
{
///
/// The commodities this warehouse can store
///
public List CanHold;
///
/// The amount of commodities that this warehouse can store (cumulative of all stored commodity types)
///
public int Capacity;
public WarehouseType(
PinaModel model,
int footprintWidth,
int footprintHeight,
List canHold,
int capacity)
: base(
model,
footprintWidth,
footprintHeight,
new Dictionary(),
new Dictionary())
{
this.CanHold = canHold;
this.Capacity = capacity;
// Set the inputs/outputs to accurately describe this warehouse
foreach (string commodity in CanHold)
{
Inputs.Add(commodity, capacity);
Outputs.Add(commodity, capacity);
}
}
}
}