//Updates file for given neuron public static void NeuronParametersChanged(Controller neuron) { NeuronFile toReplace = null; //neuron.PrepareRefractoryVariables(); float[] refractoryVars = neuron.GetRefractoryVariables(); //NeuronFile newFile = new NeuronFile(neuron.GetTableNum(), neuron.GetSeatNum(), neuron.GetThreshold(), neuron.GetHighThreshold(), neuron.GetAbsRefractoryPd(), neuron.GetRelRefractoryPd()); NeuronFile newFile = new NeuronFile(neuron.GetTableNum(), neuron.GetSeatNum(), refractoryVars[0], refractoryVars[1], refractoryVars[2], refractoryVars[3]); foreach (NeuronFile file in adjustedNeuronSettings) { if (file.GetTableNum().Equals(neuron.GetTableNum()) && file.GetSeatNum().Equals(neuron.GetSeatNum())) { //Neuron found! Update file toReplace = file; } } if (toReplace != null) { //File removed. adjustedNeuronSettings.Remove(toReplace); } //Then add new file adjustedNeuronSettings.Add(newFile); }
//Updates file for given neuron public static void NeuronParametersChanged(Controller neuron, float thresh, float recovery, float absolute, float relative) { NeuronFile toReplace = null; NeuronFile newFile = new NeuronFile(neuron.GetTableNum(), neuron.GetSeatNum(), thresh, recovery, absolute, relative); foreach (NeuronFile file in adjustedNeuronSettings) { if (file.GetTableNum().Equals(neuron.GetTableNum()) && file.GetSeatNum().Equals(neuron.GetSeatNum())) { //Neuron found! Update file toReplace = file; } } if (toReplace != null) { //File removed. adjustedNeuronSettings.Remove(toReplace); } //Then add new file adjustedNeuronSettings.Add(newFile); }
public static void PlayerEntered(GameObject obj, int table, int seat, bool preloaded) { //Save players so connections can load in later NeuronDesignation nu = new NeuronDesignation(obj, table, seat); //Ensure no duplicates if (!preloaded) { loadedPlayers.Add(nu); //Reset all connections RefreshConnections(); //Respawn already-spawned connections. //Only done on connect/reconnect, not load. for (int i = 0; i < spawnedConnections.Count; i++) { SpawnConnection(spawnedConnections[i]); } for (int i = 0; i < spawnedSensorConnections.Count; i++) { SpawnConnection(spawnedSensorConnections[i]); } for (int i = 0; i < spawnedMuscleConnections.Count; i++) { SpawnConnection(spawnedMuscleConnections[i]); } } else { //Load game is occurring. Do NOT reset connections. Has already been done in Load and should only be done once. } //Set parameters NeuronFile remove = null; foreach (NeuronFile file in neuronSettingsToLoad) { //Check table and seat if (file.GetTableNum().Equals(table) && file.GetSeatNum().Equals(seat)) { //Neuron found! Set parameters bool[] applyAll = { true, true, true, true }; obj.GetComponent <Controller>().SetNeuronParameters(obj, file.GetRestingThreshold(), file.GetRecoveryThreshold(), file.GetAbsoluteRefractoryPeriod(), file.GetRelativeRefractoryPeriod(), false, applyAll); remove = file; } } //Also check already created ones if nothing found if (remove == null) { foreach (NeuronFile created in adjustedNeuronSettings) { if (created.GetTableNum().Equals(table) && created.GetSeatNum().Equals(seat)) { bool[] applyAll = { true, true, true, true }; obj.GetComponent <Controller>().SetNeuronParameters(obj, created.GetRestingThreshold(), created.GetRecoveryThreshold(), created.GetAbsoluteRefractoryPeriod(), created.GetRelativeRefractoryPeriod(), false, applyAll); remove = created; } } } //Move completed file if (remove != null) { adjustedNeuronSettings.Add(remove); neuronSettingsToLoad.Remove(remove); } else { //No file found. No need to make one for default values. } //Check waiting connections for new player List <ConnectionFile> toRemove = new List <ConnectionFile>(); foreach (ConnectionFile file in waitingConnections) { //Check table and seat vs. start and end if (file.isEndpoint(table, seat)) { //Connection found! Instantiate SpawnConnection(file); toRemove.Add(file); } //Otherwise, ignore connection for now } //Delete instantiated connections foreach (ConnectionFile file in toRemove) { waitingConnections.Remove(file); } //Check loaded connections for new player toRemove.Clear(); foreach (ConnectionFile file in connectionsToLoad) { //Check table and seat vs. start and end if (file.isEndpoint(table, seat)) { //Connection found! Wait for other end point waitingConnections.Add(file); toRemove.Add(file); } } //Delete moved connections foreach (ConnectionFile file in toRemove) { connectionsToLoad.Remove(file); } //Check sensor connections for new player List <SensorConnectionFile> sensorsToRemove = new List <SensorConnectionFile>(); foreach (SensorConnectionFile file in sensorConnectionsToLoad) { //Check table and seat vs. end if (file.isEndpoint(table, seat)) { //Connection found! Instantiate SpawnConnection(file); sensorsToRemove.Add(file); } //Otherwise, ignore for now } //Delete instantiated connections foreach (SensorConnectionFile file in sensorsToRemove) { sensorConnectionsToLoad.Remove(file); } //Check muscle connections for new player List <MuscleConnectionFile> musclesToRemove = new List <MuscleConnectionFile>(); foreach (MuscleConnectionFile file in muscleConnectionsToLoad) { //Check table and seat vs. start if (file.isStartpoint(table, seat)) { //Connection found! Instantiate SpawnConnection(file); musclesToRemove.Add(file); } //Otherwise, ignore for now } //Delete instantiated connections foreach (MuscleConnectionFile file in musclesToRemove) { muscleConnectionsToLoad.Remove(file); } }
public static void LoadGame(string file) { StreamReader reader = new StreamReader(file); while (!reader.EndOfStream) { string line = reader.ReadLine(); //Check if neural connection, sensor connection, muscle connection, or neuron parameter string checkNeural = line.Substring(0, 5); string checkSensor = line.Substring(0, 6); string checkMuscle = line.Substring(0, 6); string checkParam = line.Substring(0, 6); if (checkNeural.Equals("Start")) { //Neural connection, move ahead ConnectionFile con; //Strip off the formatting to get to numbers line = line.Substring(9); //Start table number int startTable; if (!int.TryParse(line.Substring(0, 1), out startTable)) { Debug.LogError("Start Table format incorrect"); continue; } line = line.Substring(6); //Start seat number int startSeat; if (!int.TryParse(line.Substring(0, 1), out startSeat)) { Debug.LogError("Start Seat format incorrect"); continue; } line = line.Substring(10); //End table number int endTable; if (!int.TryParse(line.Substring(0, 1), out endTable)) { Debug.LogError("End Table format incorrect"); continue; } line = line.Substring(6); //End seat number int endSeat; if (!int.TryParse(line.Substring(0, 1), out endSeat)) { Debug.LogError("End Seat format incorrect"); continue; } line = line.Substring(6); //Strength float strength; if (!float.TryParse(line, out strength)) { Debug.LogError("Strength format incorrect"); continue; } con = new ConnectionFile(startTable, endTable, startSeat, endSeat, strength); connectionsToLoad.Add(con); } else if (checkSensor.Equals("Sensor")) { //Sensor connection, move ahead SensorConnectionFile sen; //Strip off the formatting to get to numbers line = line.Substring(10); //Sensor table number int sensorTable; if (!int.TryParse(line.Substring(0, 1), out sensorTable)) { Debug.LogError("Sensor Table format incorrect"); continue; } line = line.Substring(11); //Sensor location Sensor.SensorLocation location; switch (line.Substring(0, 1)) { case "P": { //Backward location = Sensor.SensorLocation.Backward; break; } case "A": { //Forward location = Sensor.SensorLocation.Forward; break; } default: { //Incorrect Debug.LogError("Sensor Location format incorrect"); continue; } } line = line.Substring(10); //End table number int endTable; if (!int.TryParse(line.Substring(0, 1), out endTable)) { Debug.LogError("End Table format incorrect"); continue; } line = line.Substring(6); //End seat number int endSeat; if (!int.TryParse(line.Substring(0, 1), out endSeat)) { Debug.LogError("End Seat format incorrect"); continue; } sen = new SensorConnectionFile(sensorTable, location, endTable, endSeat); sensorConnectionsToLoad.Add(sen); } else if (checkMuscle.Equals("Muscle")) { //Muscle connection, move ahead MuscleConnectionFile mus; //Strip off the formatting to get to numbers line = line.Substring(10); //Muscle table number int muscleTable; if (!int.TryParse(line.Substring(0, 1), out muscleTable)) { Debug.LogError("Muscle Table format incorrect"); continue; } line = line.Substring(7); //Muscle type MuscleType type; switch (line.Substring(0, 1)) { case "B": { //Backward type = MuscleType.Backward; break; } case "F": { //Forward type = MuscleType.Forward; break; } case "D": { //Stance type = MuscleType.Stance; break; } default: { //Incorrect Debug.LogError("Muscle Type format incorrect"); continue; } } line = line.Substring(12); //Start table number int startTable; if (!int.TryParse(line.Substring(0, 1), out startTable)) { Debug.LogError("Start Table format incorrect"); continue; } line = line.Substring(6); //Start seat number int startSeat; if (!int.TryParse(line.Substring(0, 1), out startSeat)) { Debug.LogError("Start Seat format incorrect"); continue; } mus = new MuscleConnectionFile(muscleTable, type, startTable, startSeat); muscleConnectionsToLoad.Add(mus); } else if (checkParam.Equals("Neuron")) { //Neuron parameters, move ahead NeuronFile neu; //Strip off the formatting to get to numbers line = line.Substring(10); //Table number int table; if (!int.TryParse(line.Substring(0, 1), out table)) { Debug.LogError("Table format incorrect"); continue; } line = line.Substring(6); //Seat number int seat; if (!int.TryParse(line.Substring(0, 1), out seat)) { Debug.LogError("Seat format incorrect"); continue; } line = line.Substring(7); //Resting threshold float rest; string number = Regex.Match(line, @"[0-9\.]+").Value; if (!float.TryParse(number, out rest)) { Debug.LogError("Resting Threshold format incorrect"); continue; } line = line.Substring(4 + number.Length); //Recovery threshold float rec; number = Regex.Match(line, @"[0-9\.]+").Value; if (!float.TryParse(number, out rec)) { Debug.LogError("Recovery Threshold format incorrect"); continue; } line = line.Substring(4 + number.Length); //Absolute refractory period float abs; number = Regex.Match(line, @"[0-9\.]+").Value; if (!float.TryParse(number, out abs)) { Debug.LogError("Absolute Refractory Period format incorrect"); continue; } line = line.Substring(4 + number.Length); //Relative refractory period float rel; if (!float.TryParse(line, out rel)) { Debug.LogError("Relative Refractory Period format incorrect"); continue; } neu = new NeuronFile(table, seat, rest, rec, abs, rel); neuronSettingsToLoad.Add(neu); } else if (line.Equals(string.Empty)) { //Empty file, don't bother reading return; } else { //Incorrect formatting, send error and stop Debug.LogError("Save file incorrect. Please fix before loading."); return; } } reader.Close(); Debug.Log("Game Loaded!"); //Reset all connections. Done once here so it isn't done several times in PlayerEntered. ResetConnections(); spawnedConnections = new List <ConnectionFile>(); //Must be reset on load so ghost connections don't pop back up. spawnedSensorConnections = new List <SensorConnectionFile>(); spawnedMuscleConnections = new List <MuscleConnectionFile>(); adjustedNeuronSettings = new List <NeuronFile>(); //Spawn connections for players already in scene. foreach (NeuronDesignation nd in loadedPlayers) { if (nd.GetObject() != null) { PlayerEntered(nd.GetObject(), nd.GetTableNum(), nd.GetSeatNum(), true); } } }