private void ParseRuleMap()
    {
        string line = GetNextLine();

        string[] lineSplit = SplitLine(line);

        if (lineSplit.Length == 1)
        {
            ThrowInvalidLine("ParseRuleMap()");
        }

        SimRuleMap ruleMap = new SimRuleMap();

        ruleMap.id = lineSplit[1];

        line = GetNextLine();

        List <SimRuleCommand> commands = new List <SimRuleCommand>();

        while (line != "end")
        {
            lineSplit = SplitLine(line);

            switch (lineSplit[0])
            {
            case "rate":
                ruleMap.rate = ParseInt(lineSplit[1]);
                break;

            case "randomTiles":
                ruleMap.randomTiles = ParseBool(lineSplit[1]);
                break;

            case "randomTilesPercent":
                ruleMap.randomTilesPercent = ParseInt(lineSplit[1]);
                break;

            default:
                SimRuleCommand command = ParseCommand(line);
                if (command == null)
                {
                    ThrowInvalidLine("ParseRuleMap()");
                }
                commands.Add(command);
                break;
            }

            line = GetNextLine();
        }

        ruleMap.commands = commands.ToArray();

        this.rules.Add(ruleMap.id, ruleMap);
    }
    private void ParseRuleUnit()
    {
        string line = GetNextLine();

        string[] lineSplit = SplitLine(line);

        if (lineSplit.Length == 1)
        {
            ThrowInvalidLine("ParseRuleUnit()");
        }

        SimRuleUnit ruleUnit = new SimRuleUnit();

        ruleUnit.id = lineSplit[1];

        line = GetNextLine();

        List <SimRuleCommand> commands = new List <SimRuleCommand>();

        while (line != "end")
        {
            lineSplit = SplitLine(line);

            switch (lineSplit[0])
            {
            case "rate":
                ruleUnit.rate = ParseInt(lineSplit[1]);
                break;

            case "onFail":
                ruleUnit.onFail = FindRule <SimRuleUnit>(lineSplit[1]);
                break;

            default:
                SimRuleCommand command = ParseCommand(line);
                if (command == null)
                {
                    ThrowInvalidLine("ParseRuleUnit()");
                }
                commands.Add(command);
                break;
            }

            line = GetNextLine();
        }

        ruleUnit.commands = commands.ToArray();

        this.rules.Add(ruleUnit.id, ruleUnit);
    }
    private SimRuleCommand ParseCommand(string line)
    {
        string[] lineSplit       = SplitLine(line);
        int      lineSplitOffset = 0;

        SimRuleCommand command = null;

        //Find target
        SimRuleValue target = null;

        switch (lineSplit[lineSplitOffset])
        {
        case "local":
            target = new SimRuleValueLocal();
            ((SimRuleValueLocal)target).resource = definition.GetResource(lineSplit[1]);
            lineSplitOffset += 2;
            break;

        case "global":
            target = new SimRuleValueGlobal();
            ((SimRuleValueGlobal)target).resource = definition.GetResource(lineSplit[1]);
            lineSplitOffset += 2;
            break;

        case "map":
            target = new SimRuleValueMap();
            ((SimRuleValueMap)target).mapId = lineSplit[1];
            lineSplitOffset += 2;
            break;

        case "agent":
            command = new SimRuleCommandAgent();
            ((SimRuleCommandAgent)command).agentType = definition.GetAgentType(lineSplit[1]);
            lineSplitOffset += 2;

            while (lineSplitOffset < lineSplit.Length)
            {
                switch (lineSplit[lineSplitOffset++])
                {
                case "to":
                    ((SimRuleCommandAgent)command).searchTarget = lineSplit[lineSplitOffset++];
                    break;

                case "add":
                    ((SimRuleCommandAgent)command).resources = ParseResourcesArray(lineSplit, ref lineSplitOffset);
                    break;

                default:
                    ThrowInvalidLine("ParseCommand() - Invalid agent parameter");
                    break;
                }
            }
            break;
        }

        if (target != null)
        {
            switch (lineSplit[lineSplitOffset])
            {
            case "add":
                command = new SimRuleCommandAdd();
                ((SimRuleCommandAdd)command).target = target;
                ((SimRuleCommandAdd)command).amount = ParseInt(lineSplit[lineSplitOffset + 1]);
                break;

            case "remove":
                command = new SimRuleCommandRemove();
                ((SimRuleCommandRemove)command).target = target;
                ((SimRuleCommandRemove)command).amount = ParseInt(lineSplit[lineSplitOffset + 1]);
                break;

            case "greater":
                command = new SimRuleCommandTest();
                ((SimRuleCommandTest)command).target     = target;
                ((SimRuleCommandTest)command).comparison = SimRuleCommandTest.Comparison.Greater;
                ((SimRuleCommandTest)command).amount     = ParseInt(lineSplit[lineSplitOffset + 1]);
                break;

            case "less":
                command = new SimRuleCommandTest();
                ((SimRuleCommandTest)command).target     = target;
                ((SimRuleCommandTest)command).comparison = SimRuleCommandTest.Comparison.Less;
                ((SimRuleCommandTest)command).amount     = ParseInt(lineSplit[lineSplitOffset + 1]);
                break;

            case "equals":
                command = new SimRuleCommandTest();
                ((SimRuleCommandTest)command).target     = target;
                ((SimRuleCommandTest)command).comparison = SimRuleCommandTest.Comparison.Equals;
                ((SimRuleCommandTest)command).amount     = ParseInt(lineSplit[lineSplitOffset + 1]);
                break;
            }
        }

        return(command);
    }