Beispiel #1
0
 private static void CreateDefinition(TechnologyBase tech)
 {
     nodeDefintions[GetKey(tech)] =
         string.Format(
             "[label=\"{0}\", labelloc=\"b\", height=0.8, shape=none, image=\"paper-scroll.png\"]",
             lang[tech.Name + "_TECHNOLOGY_NAME"]);
 }
Beispiel #2
0
        public Technology GetTechnology(uint type, byte level)
        {
            TechnologyBase tbase = GetTechnologyBase(type, level);

            if (tbase == null)
            {
                return(null);
            }
            var t = new Technology(tbase);

            return(t);
        }
Beispiel #3
0
        public override Error Execute()
        {
            if (obj == null)
            {
                return(Error.ObjectNotFound);
            }

            TechnologyBase techBase = technologyFactory.GetTechnologyBase(techId, lvl);

            if (techBase == null)
            {
                return(Error.ObjectNotFound);
            }

            Technology tech;

            if (!obj.Technologies.TryGetTechnology(techBase.Techtype, out tech))
            {
                tech = new Technology(techBase);
                obj.Technologies.BeginUpdate();
                obj.Technologies.Add(tech);
                obj.Technologies.EndUpdate();
                callbackProcedure.OnTechnologyUpgrade(obj, tech.TechBase);
            }
            else if (tech.Level != techBase.Level)
            {
                obj.Technologies.BeginUpdate();
                obj.Technologies.Upgrade(new Technology(techBase));
                obj.Technologies.EndUpdate();
                callbackProcedure.OnTechnologyUpgrade(obj, tech.TechBase);
            }

            StateChange(ActionState.Completed);

            return(Error.Ok);
        }
Beispiel #4
0
 private static void WriteNode(IStructureBaseStats from, TechnologyBase to)
 {
     nodeConnections.WriteLine("{0} -> {1};", GetKey(from), GetKey(to));
 }
Beispiel #5
0
 private static string GetKey(TechnologyBase tech)
 {
     return("TECH_" + tech.TechnologyHash);
 }
Beispiel #6
0
        private static Result ProcessStructure(IStructureBaseStats structureBaseStats, bool skipUpgrades)
        {
            int hash = structureBaseStats.Type * 100 + structureBaseStats.Lvl;

            if (processedStructures.Contains(hash))
            {
                return(Result.AlreadyProcessed);
            }

            Console.Out.WriteLine("Parsing " + structureBaseStats.Name + " " + structureBaseStats.Lvl);

            ActionRequirementFactory.ActionRecord record =
                kernel.Get <ActionRequirementFactory>().GetActionRequirementRecord(structureBaseStats.WorkerId);

            processedStructures.Add(hash);

            bool hadConnection = false;

            if (structureBaseStats.Lvl == 1)
            {
                CreateDefinition(structureBaseStats);
            }

            if (record == null)
            {
                return(Result.Empty);
            }

            // First pass
            foreach (var action in record.List)
            {
                switch (action.Type)
                {
                case ActionType.StructureBuildActive:
                case ActionType.StructureChangeActive:
                    IStructureBaseStats building = kernel.Get <IStructureCsvFactory>().GetBaseStats(ushort.Parse(action.Parms[0]), 1);
                    Result result = ProcessStructure(building, false);
                    if (result != Result.AlreadyProcessed)
                    {
                        if (action.Type == ActionType.StructureBuildActive)
                        {
                            WriteNode(structureBaseStats, building);
                        }
                        else if (action.Type == ActionType.StructureChangeActive)
                        {
                            WriteNode(structureBaseStats, building, "dashed", "Converts To");
                        }

                        hadConnection = true;
                    }
                    break;

                case ActionType.UnitTrainActive:
                    IBaseUnitStats training =
                        kernel.Get <UnitFactory>().GetUnitStats(ushort.Parse(action.Parms[0]), 1);
                    if (!processedUnits.Contains(training.UnitHash))
                    {
                        WriteNode(structureBaseStats, training);
                        CreateDefinition(training);
                        hadConnection = true;
                        processedUnits.Add(training.UnitHash);
                    }
                    break;

                case ActionType.TechnologyUpgradeActive:
                    TechnologyBase tech =
                        kernel.Get <TechnologyFactory>().GetTechnologyBase(ushort.Parse(action.Parms[0]), 1);
                    if (!processedTechnologies.Contains(tech.TechnologyHash))
                    {
                        WriteNode(structureBaseStats, tech);
                        CreateDefinition(tech);
                        hadConnection = true;
                        processedTechnologies.Add(tech.TechnologyHash);
                    }
                    break;
                }
            }

            // Second pass
            foreach (var action in record.List)
            {
                switch (action.Type)
                {
                case ActionType.StructureUpgradeActive:
                    if (!skipUpgrades)
                    {
                        byte maxLvl = byte.Parse(action.Parms[0]);
                        IStructureBaseStats from = structureBaseStats;
                        var newRank = new List <String> {
                            GetKey(from)
                        };

                        for (int i = from.Lvl; i < maxLvl; i++)
                        {
                            IStructureBaseStats to = kernel.Get <IStructureCsvFactory>()
                                                     .GetBaseStats(from.Type, (byte)(i + 1));
                            Result result = ProcessStructure(to, true);
                            if (result == Result.Ok || i == maxLvl - 1)
                            {
                                WriteNode(from, to);
                                CreateDefinition(to);
                                hadConnection = true;
                                newRank.Add(GetKey(to));
                                from = to;
                            }
                        }

                        if (newRank.Count > 1)
                        {
                            ranks.Add(newRank);
                        }
                    }
                    break;
                }
            }

            return(hadConnection ? Result.Ok : Result.Empty);
        }
Beispiel #7
0
 public virtual void OnTechnologyDelete(IStructure structure, TechnologyBase technologyBase)
 {
     cityTriggerManager.Process(cityEventFactory.CreateTechnologyDeleteEvent(structure, technologyBase.Techtype, technologyBase.Level));
 }
Beispiel #8
0
        public void Init(string technologyFilename, string technologyEffectsFilename)
        {
            using (
                var reader =
                    new CsvReader(
                        new StreamReader(new FileStream(technologyFilename,
                                                        FileMode.Open,
                                                        FileAccess.Read,
                                                        FileShare.ReadWrite))))
            {
                var      col = new Dictionary <string, int>();
                String[] toks;

                for (int i = 0; i < reader.Columns.Length; ++i)
                {
                    col.Add(reader.Columns[i], i);
                }

                while ((toks = reader.ReadRow()) != null)
                {
                    if (toks[0].Length <= 0)
                    {
                        continue;
                    }

                    uint time;
                    if (toks[col["Time"]].Length > 0)
                    {
                        time = uint.Parse(toks[col["Time"]]);
                    }
                    else
                    {
                        time = (uint)GetTime(byte.Parse(toks[col["Lvl"]]), int.Parse(toks[col["BuildType"]]), int.Parse(toks[col["BuildLvl"]]));
                    }
                    var tech = new TechnologyBase
                    {
                        Techtype  = uint.Parse(toks[col["TechType"]]),
                        Name      = toks[col["Name"]],
                        Level     = byte.Parse(toks[col["Lvl"]]),
                        Time      = time,
                        Resources =
                            GetResource(byte.Parse(toks[col["Lvl"]]),
                                        int.Parse(toks[col["BuildType"]]),
                                        int.Parse(toks[col["BuildLvl"]])),
                        Effects = new List <Effect>()
                    };

                    technologies[tech.Techtype * 100 + tech.Level] = tech;
                }
            }

            using (
                var reader =
                    new CsvReader(
                        new StreamReader(new FileStream(technologyEffectsFilename,
                                                        FileMode.Open,
                                                        FileAccess.Read,
                                                        FileShare.ReadWrite))))
            {
                var            col = new Dictionary <string, int>();
                String[]       toks;
                TechnologyBase tech;
                for (int i = 0; i < reader.Columns.Length; ++i)
                {
                    col.Add(reader.Columns[i], i);
                }

                while ((toks = reader.ReadRow()) != null)
                {
                    if (toks[0].Length <= 0)
                    {
                        continue;
                    }

                    if (
                        !technologies.TryGetValue(
                            uint.Parse(toks[col["TechType"]]) * 100 +
                            byte.Parse(toks[col["Lvl"]]),
                            out tech))
                    {
                        continue;
                    }

                    var effect = new Effect
                    {
                        Id        = (EffectCode)Enum.Parse(typeof(EffectCode), toks[col["Effect"]], true),
                        IsPrivate = bool.Parse(toks[col["IsPrivate"]])
                    };

                    for (int i = 0; i < 5; ++i)
                    {
                        string str = toks[col[string.Format("P{0}", i + 1)]];
                        int    tmp;
                        if (str.StartsWith("{"))
                        {
                        }
                        else if (int.TryParse(str, out tmp))
                        {
                            effect.Value[i] = tmp;
                        }
                        else
                        {
                            effect.Value[i] = str;
                        }
                    }

                    effect.Location = (EffectLocation)Enum.Parse(typeof(EffectLocation), toks[col["Location"]], true);
                    tech.Effects.Add(effect);
                }
            }
        }
Beispiel #9
0
        private static void WriteTechnologies()
        {
            #region Get Tech Effects

            List <TechnologyEffects> techEffects = new List <TechnologyEffects>();

            using (
                CsvReader techEffectsReader =
                    new CsvReader(
                        new StreamReader(File.Open(csvDataFolder + "technology_effects.csv",
                                                   FileMode.Open,
                                                   FileAccess.Read,
                                                   FileShare.ReadWrite))))
            {
                while (true)
                {
                    string[] obj = techEffectsReader.ReadRow();
                    if (obj == null)
                    {
                        break;
                    }

                    if (obj[0].Length <= 0)
                    {
                        continue;
                    }

                    TechnologyEffects techEffect = new TechnologyEffects
                    {
                        techid   = obj[0],
                        lvl      = obj[1],
                        effect   = ((int)((EffectCode)Enum.Parse(typeof(EffectCode), obj[2], true))).ToString(),
                        location =
                            ((int)((EffectLocation)Enum.Parse(typeof(EffectLocation), obj[3], true))).ToString(),
                        isprivate = obj[4],
                        param1    = obj[5],
                        param2    = obj[6],
                        param3    = obj[7],
                        param4    = obj[8],
                        param5    = obj[9]
                    };

                    techEffects.Add(techEffect);
                }
            }

            #endregion

            writer.WriteStartElement("Technologies");

            using (
                CsvReader techReader =
                    new CsvReader(
                        new StreamReader(File.Open(csvDataFolder + "technology.csv",
                                                   FileMode.Open,
                                                   FileAccess.Read,
                                                   FileShare.ReadWrite))))
            {
                while (true)
                {
                    string[] obj = techReader.ReadRow();
                    if (obj == null)
                    {
                        break;
                    }

                    if (obj[0] == string.Empty)
                    {
                        continue;
                    }

                    writer.WriteStartElement("Tech");

                    TechnologyBase techBase = Ioc.Kernel.Get <TechnologyFactory>()
                                              .GetTechnologyBase(uint.Parse(obj[0]), byte.Parse(obj[3]));

                    writer.WriteAttributeString("techtype", obj[0]);
                    writer.WriteAttributeString("name", obj[1]);
                    writer.WriteAttributeString("spriteclass", obj[2]);
                    writer.WriteAttributeString("level", obj[3]);
                    writer.WriteAttributeString("crop", techBase.Resources.Crop.ToString());
                    writer.WriteAttributeString("gold", techBase.Resources.Gold.ToString());
                    writer.WriteAttributeString("iron", techBase.Resources.Iron.ToString());
                    writer.WriteAttributeString("wood", techBase.Resources.Wood.ToString());
                    writer.WriteAttributeString("labor", techBase.Resources.Labor.ToString());
                    writer.WriteAttributeString("time", techBase.Time.ToString());

                    IEnumerable <TechnologyEffects> effects = FindAllTechEffects(techEffects,
                                                                                 Int32.Parse(obj[0]),
                                                                                 Int32.Parse(obj[3]));
                    foreach (TechnologyEffects effect in effects)
                    {
                        //TechType,Lvl,Effect,Location,IsPrivate,P1,P2,P3,P4,P5
                        writer.WriteStartElement("Effect");
                        writer.WriteAttributeString("effect", effect.effect);
                        writer.WriteAttributeString("location", effect.location);
                        writer.WriteAttributeString("private", effect.isprivate);
                        writer.WriteAttributeString("param1", effect.param1);
                        writer.WriteAttributeString("param2", effect.param2);
                        writer.WriteAttributeString("param3", effect.param3);
                        writer.WriteAttributeString("param4", effect.param4);
                        writer.WriteAttributeString("param5", effect.param5);
                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                }
            }

            writer.WriteEndElement();
        }
Beispiel #10
0
        private static void Main()
        {
            string settings = string.Empty;

            try
            {
                var p = new OptionSet {
                    { "output=", v => output = v }, { "settings=", v => settings = v }
                };

                p.Parse(Environment.GetCommandLineArgs());
            }
            catch (Exception)
            {
            }

            if (!string.IsNullOrEmpty(settings))
            {
                Config.LoadConfigFile(settings);
            }

            kernel = Engine.CreateDefaultKernel();
            kernel.Get <FactoriesInitializer>().CompileAndInit();

            LoadLanguages();

            // Get types
            structureTypes = from structure in kernel.Get <IStructureCsvFactory>().AllStructures()
                             group structure by structure.Type
                             into types orderby types.Key select types.Key;

            technologyTypes = from technology in kernel.Get <TechnologyFactory>().AllTechnologies()
                              group technology by technology.Techtype
                              into types orderby types.Key select types.Key;

            unitTypes = from unit in kernel.Get <UnitFactory>().AllUnits()
                        group unit by unit.Type
                        into types orderby types.Key select types.Key;

            // Process structures
            Directory.CreateDirectory(output);
            using (var writer = new StreamWriter(File.Create(Path.Combine(output, "structure_listing.inc.php"))))
            {
                writer.Write(@"<?php
                    $structures = array(
                ");

                foreach (var type in structureTypes)
                {
                    if (kernel.Get <IObjectTypeFactory>().IsObjectType("DatabaseIgnoreStructures", type))
                    {
                        continue;
                    }

                    ProcessStructure(type);

                    IStructureBaseStats stats = kernel.Get <IStructureCsvFactory>().GetBaseStats(type, 1);

                    var sprite = stats.SpriteClass;

                    // Sorry this is a bit of a hack, it's a CropField then we append the Mature status to it :)
                    if (kernel.Get <IObjectTypeFactory>().IsObjectType("CropField", type))
                    {
                        sprite =
                            kernel.Get <IStructureCsvFactory>()
                            .AllStructures()
                            .First(structure => structure.Lvl == 1 && structure.Name == "MATURE_" + stats.Name)
                            .SpriteClass;
                    }

                    writer.WriteLine("'{2}_STRUCTURE' => array('name' => '{1}', 'sprite' => '{0}'),",
                                     sprite,
                                     lang[stats.Name + "_STRUCTURE_NAME"],
                                     stats.Name);
                }

                writer.Write(@");");
            }

            // Process units
            using (var writer = new StreamWriter(File.Create(Path.Combine(output, "unit_listing.inc.php"))))
            {
                writer.Write(@"<?php
                    $units = array(
                ");
                foreach (var type in unitTypes)
                {
                    ProcessUnit(type);

                    IBaseUnitStats stats = kernel.Get <UnitFactory>().GetUnitStats(type, 1);
                    writer.WriteLine("'{2}_UNIT' => array('name' => '{1}', 'sprite' => '{0}'),",
                                     stats.SpriteClass,
                                     lang[stats.Name + "_UNIT"],
                                     stats.Name);
                }
                writer.Write(@");");
            }

            // Process technologies
            // Process units
            using (var writer = new StreamWriter(File.Create(Path.Combine(output, "technology_listing.inc.php"))))
            {
                writer.Write(@"<?php
                    $technologies = array(
                ");
                foreach (var type in technologyTypes)
                {
                    if (kernel.Get <IObjectTypeFactory>().IsObjectType("DatabaseIgnoreTech", (ushort)type))
                    {
                        continue;
                    }

                    ProcessTechnology(type);

                    TechnologyBase stats = kernel.Get <TechnologyFactory>().GetTechnologyBase(type, 1);
                    writer.WriteLine("'{0}_TECHNOLOGY' => array('name' => '{1}'),",
                                     stats.Name,
                                     lang[stats.Name + "_TECHNOLOGY_NAME"]);
                }
                writer.Write(@");");
            }
        }
Beispiel #11
0
        // Technologies
        private static void ProcessTechnology(uint type)
        {
            string generalTemplate = @"<?php
                // Generated by DatabaseGenerator program on #DATE#
	            $techKey = '#TECH#';
	            $techName = '#TECH_NAME#';
	
	            $trainedBy = array('name' => '#TRAINED_BY_NAME#', 'key' => '#TRAINED_BY#', 'level' => '#TRAINED_BY_LEVEL#');
	
	            // Levels array should contain:
	            $levels = array(
		            #LEVELS#
	            );

                include $includeLocation . 'technology_view.ctp';
            ";

            const string levelTemplate =
                @"array('description' => '#DESCRIPTION#', 'time' => '#TIME#', 'gold' => #GOLD#, 'crop' => #CROP#, 'iron' => #IRON#, 'labor' => #LABOR#, 'wood' => #WOOD#, 'requirements' => array(#REQUIREMENTS#)),";

            string requirementTemplate = @"'#REQUIREMENT#',";

            // Get basic information
            TechnologyBase tech = kernel.Get <TechnologyFactory>().GetTechnologyBase(type, 1);

            generalTemplate = generalTemplate.Replace("#DATE#", DateTime.Now.ToString());
            generalTemplate = generalTemplate.Replace("#TECH#", tech.Name + "_TECHNOLOGY");
            generalTemplate = generalTemplate.Replace("#TECH_NAME#", lang[tech.Name + "_TECHNOLOGY_NAME"]);

            // Builder info
            IStructureBaseStats trainer;

            FindTechnologyTrainer(type, 1, out trainer);
            generalTemplate = generalTemplate.Replace("#TRAINED_BY_NAME#",
                                                      trainer != null ? lang[trainer.Name + "_STRUCTURE_NAME"] : "");
            generalTemplate = generalTemplate.Replace("#TRAINED_BY#", trainer != null ? trainer.Name + "_STRUCTURE" : "");
            generalTemplate = generalTemplate.Replace("#TRAINED_BY_LEVEL#",
                                                      trainer != null ? trainer.Lvl.ToString() : "");

            // Level info
            byte           level        = 1;
            var            levelsWriter = new StringWriter(new StringBuilder());
            TechnologyBase currentStats = tech;

            do
            {
                string currentLevel = levelTemplate.Replace("#DESCRIPTION#",
                                                            lang[currentStats.Name + "_TECHNOLOGY_LVL_" + level].Replace
                                                                ("'", "\\'"));
                currentLevel = currentLevel.Replace("#TIME#", currentStats.Time.ToString());
                currentLevel = currentLevel.Replace("#GOLD#", currentStats.Resources.Gold.ToString());
                currentLevel = currentLevel.Replace("#CROP#", currentStats.Resources.Crop.ToString());
                currentLevel = currentLevel.Replace("#IRON#", currentStats.Resources.Iron.ToString());
                currentLevel = currentLevel.Replace("#LABOR#", currentStats.Resources.Labor.ToString());
                currentLevel = currentLevel.Replace("#WOOD#", currentStats.Resources.Wood.ToString());

                // Get requirements
                var requirementsWriter = new StringWriter(new StringBuilder());

                if (trainer != null)
                {
                    foreach (var requirement in GetTechnologyRequirements(type, level, trainer))
                    {
                        requirementsWriter.WriteLine(requirementTemplate.Replace("#REQUIREMENT#", requirement));
                    }
                }

                currentLevel = currentLevel.Replace("#REQUIREMENTS#", requirementsWriter.ToString());

                levelsWriter.WriteLine(currentLevel);

                FindTechnologyTrainer(type, level, out trainer);
                level++;
            }while ((currentStats = kernel.Get <TechnologyFactory>().GetTechnologyBase(type, level)) != null);

            generalTemplate = generalTemplate.Replace("#LEVELS#", levelsWriter.ToString());

            using (
                var writer =
                    new StreamWriter(
                        File.Create(Path.Combine(output, string.Format("{0}_TECHNOLOGY.ctp", tech.Name)))))
            {
                writer.Write(generalTemplate);
            }
        }