示例#1
0
    private bool EvaluateTopicConditions(Stack <ConditionToken> conditions)
    {
        bool finalResult = false;

        if (conditions.Count <= 0)
        {
            return(true);
        }

        //TODO evaluate RPN
        Stack <bool> values = new Stack <bool>();

        while (conditions.Count > 0)
        {
            ConditionToken token = conditions.Pop();
            if (token.IsOperator)
            {
                //Debug.Log("operator : " + ((DialogueConditionOperator)token).Op);
                //pop two values from values and calculate
                DialogueConditionOperator op = (DialogueConditionOperator)token;
                bool value1, value2;
                try
                {
                    value1 = values.Pop();
                    value2 = values.Pop();
                }
                catch (Exception e)
                {
                    //Debug.Log("pop failed");
                    return(false);
                }

                if (op.Op == LogicOperator.And)
                {
                    values.Push(value1 && value2);
                }
                else if (op.Op == LogicOperator.Or)
                {
                    values.Push(value1 || value2);
                }
            }
            else
            {
                //Debug.Log("condition : " + ((DialogueCondition)token).ID);
                //evaluate and then push into values
                bool result = EvaluateCondition((DialogueCondition)token);
                values.Push(result);
            }
        }

        if (values.Count != 1)
        {
            //Debug.Log("there are not 1 value left in values");
            return(false);
        }
        else
        {
            return(values.Pop());
        }
    }
    private void GetTokens(XmlNode nodeItem, Stack <ConditionToken> theStack)
    {
        if (nodeItem.Name == "condition")
        {
            XmlAttributeCollection attributes = nodeItem.Attributes;
            Debug.Log("found condition " + attributes["story"].Value);
            DialogueCondition condition = new DialogueCondition();

            condition.ID = attributes["name"].Value;
            if (condition.ID.Length > 0)
            {
                if (attributes["story"] != null)
                {
                    condition.StoryTriggerID = attributes["story"].Value;
                }

                if (attributes["compare"] != null)
                {
                    condition.StoryTriggerCompare = Convert.ToInt32(attributes["compare"].Value);
                }

                if (attributes["op"] != null)
                {
                    condition.StoryTriggerCompareOp = Convert.ToInt32(attributes["op"].Value);
                }

                theStack.Push(condition);
            }
        }
        else if (nodeItem.Name == "logic")
        {
            DialogueConditionOperator op         = new DialogueConditionOperator();
            XmlAttributeCollection    attributes = nodeItem.Attributes;
            op.Op = (LogicOperator)Enum.Parse(typeof(LogicOperator), attributes["type"].Value);
            theStack.Push(op);

            XmlNodeList nodeContent = nodeItem.ChildNodes;
            if (nodeContent.Count > 0)
            {
                foreach (XmlNode child in nodeContent)
                {
                    Debug.Log("Found logic child");
                    GetTokens(child, theStack);
                }
            }
        }
    }