void ViewPortWindow(int windowID) { GUI.contentColor = Color.white; //GUILayout.BeginHorizontal(); if (GUI.Button(new Rect(0, 0, 18, 18), new GUIContent("x"))) //if (GUILayout.Button(new GUIContent("X"))) { //Debug.Log("resetting"); GlobalVariables.viewControlsVisible = false; //} //GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if (GUILayout.Button(new GUIContent("Zoom -"))) camera.orthographicSize = (camera.orthographicSize >= 89.3f)?125f:camera.orthographicSize + camera.orthographicSize*0.4f; if (GUILayout.Button(new GUIContent("Zoom +"))) camera.orthographicSize = (camera.orthographicSize <= 41.7f)?20f:camera.orthographicSize - camera.orthographicSize*0.4f; GUILayout.EndHorizontal(); float scrollFactor = 5f; //larger means it scrolls less GUILayout.BeginHorizontal(); if (GUILayout.Button(new GUIContent("/\\"))) camera.transform.Translate(0, camera.orthographicSize/scrollFactor, 0); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if (GUILayout.Button(new GUIContent("<"))) camera.transform.Translate(-(camera.orthographicSize/scrollFactor), 0, 0); if (GUILayout.Button(new GUIContent(">"))) camera.transform.Translate(camera.orthographicSize/scrollFactor, 0, 0); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if (GUILayout.Button(new GUIContent("\\/"))) camera.transform.Translate(0, -camera.orthographicSize/scrollFactor, 0); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GlobalVariables.centerCamera = GUILayout.Toggle(GlobalVariables.centerCamera, "Keep PAGI Guy Centered"); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if (GUILayout.Button(new GUIContent("test"))) { AIMessage a = AIMessage.fromString("createItem,myname,bill.jpeg,5,10,1.5,3,3.14,0,5"); GlobalVariables.messageQueue.Add(a); } if (GUILayout.Button(new GUIContent("test2"))) { AIMessage a = AIMessage.fromString("addForceToItem,myname,0,200,0"); GlobalVariables.messageQueue.Add(a); } GUILayout.EndHorizontal(); GUI.DragWindow(windowRect); }
void OnReceive(SocketRead read, byte[] data) { string clientSaid = Encoding.ASCII.GetString(data, 0, data.Length); /*string toPrint = "SAID: "; * for (int i=0; i<clientSaid.Length; i++) * toPrint = toPrint + ((int)clientSaid[i]).ToString() + ","; * Debug.Log(toPrint);*/ string message = "Client " + clients.IndexOf(read.Socket) + " says: " + clientSaid; Debug.Log(message); //process the incoming text and store in the message queue clientSaid = bufferUnfinished + clientSaid; //Debug.Log("new message is " + clientSaid); bufferUnfinished = ""; //did it end in a return? List <string> allCommands = new List <string>(clientSaid.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)); if (message[message.Length - 1] != '\n') { bufferUnfinished = allCommands[allCommands.Count - 1]; allCommands.RemoveAt((allCommands.Count) - 1); Debug.Log(allCommands.Count); } foreach (String cmd in allCommands) { AIMessage a; try { a = AIMessage.fromString(cmd); } catch (Exception e) { Debug.Log("Could not parse message due to error. Skipping."); GlobalVariables.outgoingMessages.Add("ERR,formattingError\n"); continue; } bodyInterface.messageQueue.Add(a); //Debug.Log("added to message queue: " + a.messageType.ToString() + ", " + a.stringContent); } }
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); }