/* * TechManager.cs * Authors: Mike Dapiran * 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.Xml; namespace ColonySim { class TechManager { private readonly List mCompletedTechs = new List(); public List CompletedTechs { get { return mCompletedTechs; } } public bool IsTechCompleted(String pTechName) { foreach (Technology t in mCompletedTechs) { if (t.Name == pTechName) { return true; } } return false; } private readonly List mIdleTechs = new List(); public List IdleTechs { get { return mIdleTechs; } } public bool IsTechIdle(String pTechName) { foreach (Technology t in mIdleTechs) { if (t.Name == pTechName) { return true; } } return false; } private readonly List mInprogressTechs = new List(); public List InprogressTechs { get { return mInprogressTechs; } } public bool IsTechInProgress(String pTechName) { foreach (Technology t in mInprogressTechs) { if (t.Name == pTechName) { return true; } } return false; } public Technology CreateTechnology(String pName, float pAmountPerTick, List mPrerequisites) { Technology returnTech = new Technology(); returnTech.Name = pName; returnTech.AmountCompleted = 0f; returnTech.AmountPerTick = pAmountPerTick; returnTech.Prerequisites = mPrerequisites; AddTech(returnTech); return returnTech; } public void AddTech(Technology pTech) { mIdleTechs.Add(pTech); } public bool StartResearchOnTech(String pName, bool RequirePrereqs) { for (int i = 0; i < mIdleTechs.Count; i++ ) { if (mIdleTechs[i].Name == pName) { if (RequirePrereqs) { foreach (Technology t in mIdleTechs[i].Prerequisites) { if (!mCompletedTechs.Contains(t)) return false; } } mInprogressTechs.Add(mIdleTechs[i]); mIdleTechs.RemoveAt(i); return true; } } return false; } public void IncrementResearch(ref List pCompletedTechs) { pCompletedTechs.Clear(); foreach (Technology t in mInprogressTechs) { t.AmountCompleted += t.AmountPerTick; if (Technology.IsCompleted(t)) { mCompletedTechs.Add(t); pCompletedTechs.Add(t); } } mInprogressTechs.RemoveAll(Technology.IsCompleted); } public List IncrementResearch() { List returnList = new List(); IncrementResearch(ref returnList); return returnList; } public void LoadTechnology(string pFileName) { XmlTextReader reader = new XmlTextReader(pFileName); Dictionary techDictionary = new Dictionary(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name != "TechList") { int amountPerTick = int.Parse(reader.GetAttribute("AmountPerTick")); string name = reader.Name; List prereqs = new List(); try { string[] listOfPrereqs = reader.GetAttribute("Prerequisite").Split(','); for (int i = 0; i < listOfPrereqs.GetLength(0); i++) { if (techDictionary.ContainsKey(listOfPrereqs[i].Trim())) { prereqs.Add(techDictionary[listOfPrereqs[i].Trim()]); } else { throw new Exception("Error: Technology has Prerequisite that has not be defined yet." + " Define this Tech earlier in this file to correct."); } } } catch (Exception e) { Console.WriteLine(e); } techDictionary.Add(name.Trim(), CreateTechnology(name.Trim(), amountPerTick, prereqs)); } } } } class Technology { public float AmountCompleted; public float AmountPerTick; public string Name; public List Prerequisites; public static bool IsCompleted(Technology pTech) { return pTech.AmountCompleted >= 100f; } } }