示例#1
0
    public static AIMessage fromString(string s)
    {
        if (s.Trim() == "")
        {
            throw new Exception("ERR: Received string that was nothing but whitespace!");
        }

        string[]  clientArgs = s.Split(',');
        AIMessage a          = new AIMessage(AIMessage.AIMessageType.other, "ERR: Unrecognized Command. Received string:\"" + s + "\"\n", 100f, "");

        clientArgs[0] = clientArgs[0].Trim();
        if (clientArgs[0] == "sensorRequest")
        {
            a.messageType = AIMessage.AIMessageType.sensorRequest;
            if (clientArgs.Length == 2)
            {
                a.stringContent = clientArgs[1];
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        else if (clientArgs[0] == "print")
        {
            a.messageType = AIMessageType.print;
            //the content should be everything following the print, command"
            a.stringContent = s.Substring(s.IndexOf(',') + 1);
        }
        else if (clientArgs[0] == "loadTask")
        {
            //FileSaving f = new FileSaving(clientArgs[1]);
            a.messageType = AIMessage.AIMessageType.loadTask;
            if (clientArgs.Length == 2)
            {
                a.stringContent = clientArgs[1];
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        else if (clientArgs[0] == "findObj")
        {
            //FileSaving f = new FileSaving(clientArgs[1]);
            a.messageType = AIMessage.AIMessageType.findObj;
            if (clientArgs.Length == 3)
            {
                a.stringContent = clientArgs[1];
                a.detail        = clientArgs[2];
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        else if (clientArgs[0] == "addForce")
        {
            /*addForce,e,v
             *              e - The code of the effector to send force to.
             *              v - The amount of force to add to the effector.*/
            a.messageType = AIMessage.AIMessageType.addForce;
            if (clientArgs.Length == 3)
            {
                a.stringContent = clientArgs[1];
                //the arguments at indices 2 and 3 can be either float values, or
                //if they are in square brackets, a function that evaluates to one
                a.function1 = fnNode.parseFloat(clientArgs[2]);
            }
            else if (clientArgs.Length == 4)
            {
                a.stringContent = clientArgs[1];
                a.function1     = fnNode.parseFloat(clientArgs[2]);
                a.function2     = fnNode.parseFloat(clientArgs[3]);
                //a.vectorContent = new Vector2(evaluateFloat(clientArgs[2]), evaluateFloat(clientArgs[3]));
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        else if (clientArgs[0] == "dropItem")
        {
            a.messageType = AIMessage.AIMessageType.dropItem;
            if (clientArgs.Length == 4 || clientArgs.Length == 5)
            {
                a.stringContent = clientArgs[1];
                a.vectorContent = new Vector2(float.Parse(clientArgs[2]), float.Parse(clientArgs[3]));
                if (clientArgs.Length == 5)
                {
                    a.detail = clientArgs[4];
                }
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        else if (clientArgs[0] == "setState")
        {
            a.messageType = AIMessageType.setState;
            if (clientArgs.Length == 3)
            {
                a.stringContent = clientArgs[1];
                a.floatContent  = float.Parse(clientArgs[2]);
                int   stateLife = int.Parse(clientArgs[2]);
                State st;
                if (stateLife == 0)                 //this is actually a command to kill the state
                {
                    st = new State(clientArgs[1], TimeSpan.Zero);
                }
                else if (stateLife < 0)
                {
                    st = new State(clientArgs[1], new TimeSpan(Int64.MaxValue));
                }
                else
                {
                    st = new State(clientArgs[1], new TimeSpan(0, 0, 0, 0, stateLife));
                }
                a.detail = st;
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        else if (clientArgs[0] == "setReflex")
        {
            a.messageType = AIMessageType.setReflex;
            if (clientArgs.Length == 3 || clientArgs.Length == 4)
            {
                a.stringContent = clientArgs[1];
                //clientArgs[2] is a list of conditions
                //clientArgs[3] (optional) is a list of actions to execute, each of which should be AIMessages.
                //create reflex object
                Reflex r = new Reflex(clientArgs[1]);
                //parse conditions
                foreach (string strCon in clientArgs[2].Split(new char[] { ';' }))
                {
                    //each condition is either sensory (sensorAspectCode|operator|value) or state ([-]s)
                    if (strCon.Contains("|"))
                    {                     //treat it as sensory condition
                        string[] args = strCon.Split(new char[] { '|' });
                        if (args.Length != 3)
                        {
                            throw new Exception("Incorrect # of arguments in sensory condition " + strCon);
                        }
                        r.addCondition(args[0], args[1][0], float.Parse(args[2]));
                    }
                    else
                    {                    //treat it as state condition
                        if (strCon.StartsWith("-"))
                        {
                            r.addCondition(strCon.Substring(1), true);
                        }
                        else
                        {
                            r.addCondition(strCon, false);
                        }
                    }
                }
                //parse actions
                if (clientArgs.Length > 3)
                {
                    foreach (string strAct in clientArgs[3].Split(new char[] { ';' }))
                    {
                        //each is a standard AIMessage except for setReflex.
                        string aiMsgString = strAct.Replace('|', ',');
                        r.addAction(AIMessage.fromString(aiMsgString));
                    }
                }
                a.detail = r;
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        else if (clientArgs[0] == "removeReflex")
        {
            a.messageType = AIMessageType.removeReflex;
            if (clientArgs.Length == 2)
            {
                a.stringContent = clientArgs[1];
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        else if (clientArgs[0] == "getActiveStates")
        {
            a.messageType = AIMessageType.getStates;
            a.detail      = "";
        }
        else if (clientArgs[0] == "getActiveReflexes")
        {
            a.messageType = AIMessageType.getReflexes;
        }
        else if (clientArgs[0] == "createItem")
        {
            //createItem,name,filePath,x,y,mass,friction,rotation,endorphins,disappear,kinematic
            a.messageType = AIMessageType.createItem;
            Dictionary <string, System.Object> args = new Dictionary <string, System.Object>();
            if (clientArgs.Length == 10)
            {
                try
                {
                    args.Add("name", clientArgs[1]);
                    args.Add("filePath", clientArgs[2]);
                    args.Add("x", float.Parse(clientArgs[3]));
                    args.Add("y", float.Parse(clientArgs[4]));
                    args.Add("mass", float.Parse(clientArgs[5]));
                    args.Add("friction", int.Parse(clientArgs[6]));
                    if (!(new List <String> {
                        "0", "1", "2", "3", "4", "5"
                    }).Contains(clientArgs[6].Trim()))
                    {
                        throw new Exception("Friction must be an integer from 0 to 5");
                    }
                    args.Add("rotation", float.Parse(clientArgs[7]));
                    args.Add("endorphins", float.Parse(clientArgs[8]));
                    args.Add("kinematic", int.Parse(clientArgs[9]));
                    if (!(new List <String> {
                        "0", "1", "2", "3", "4", "5"
                    }).Contains(clientArgs[9].Trim()))
                    {
                        throw new Exception("Friction must be an integer from 0 to 5");
                    }
                }
                catch (Exception e)
                {
                    a.messageType   = AIMessageType.other;
                    a.stringContent = "ERROR: Error parsing one or more values in command: \""
                                      + s + "\" (" + e.Message + ")\n";
                    //throw e;
                    return(a);
                }
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }

            a.detail = args;
        }
        else if (clientArgs[0] == "addForceToItem")
        {
            a.messageType = AIMessageType.addForceToItem;
            if (clientArgs.Length == 5)
            {
                a.stringContent = clientArgs[1];
                try
                {
                    a.vectorContent = new Vector2(float.Parse(clientArgs[2]), float.Parse(clientArgs[3]));
                    a.floatContent  = float.Parse(clientArgs[4]);
                }
                catch (Exception e)
                {
                    a.messageType   = AIMessageType.other;
                    a.stringContent = "ERROR: Error parsing one or more values in command: \""
                                      + s + "\" (" + e.Message + ")\n";
                    //throw e;
                    return(a);
                }
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        else if (clientArgs[0] == "getInfoAboutItem")
        {
            a.messageType = AIMessageType.getInfoAboutItem;
            if (clientArgs.Length == 2)
            {
                a.stringContent = clientArgs[1];
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        else if (clientArgs[0] == "destroyItem")
        {
            a.messageType = AIMessageType.destroyItem;
            if (clientArgs.Length == 2)
            {
                a.stringContent = clientArgs[1];
            }
            else
            {
                throw new Exception("Incorrect # of arguments given in client message: " + s);
            }
        }
        //else if (clientArgs[0] == "establishConnection")
        //{}
        //else if (clientArgs[0] == "removeConnection")
        //{}

        return(a);
    }