Exemple #1
0
    //Helper method to spawn sensor connections based on given connection file
    private static void SpawnConnection(SensorConnectionFile file)
    {
        //Find start and end gameObjects
        GameObject start = null, end = null;

        foreach (NeuronDesignation nd in loadedPlayers)
        {
            //Find end
            if (nd.isGivenNeuron(file.GetEnd()[0], file.GetEnd()[1]))
            {
                end = nd.GetObject();
            }
        }
        foreach (SensorConnector sc in sensors)
        {
            //Find start
            if (sc.table.Equals(file.GetStart().table) && sc.sensor.location.Equals(file.GetStart().location))
            {
                start = sc.gameObject;
            }
        }
        if (start && end)
        {
            GameObject player = NetworkManager.singleton.client.connection.playerControllers[0].gameObject;
            player.GetComponent <ConnectionManager>().SensorConnection(end, start);
        }
    }
Exemple #2
0
        public override bool Equals(object obj)
        {
            if (obj.GetType() == typeof(SensorConnectionFile))
            {
                SensorConnectionFile file = (SensorConnectionFile)obj;
                if (!file.GetStart().table.Equals(sensorTable))
                {
                    return(false);
                }
                if (!file.GetStart().location.Equals(sensorLocation))
                {
                    return(false);
                }
                if (!file.GetEnd()[0].Equals(endTable))
                {
                    return(false);
                }
                if (!file.GetEnd()[1].Equals(endSeat))
                {
                    return(false);
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #3
0
    public static void SensorConnectionRemoved(SensorConnection sen)
    {
        SensorConnectionFile file;
        SensorConnector      start = sen.GetStart().GetComponent <SensorConnector>();
        Controller           end   = sen.GetEnd().GetComponent <Controller>();

        file = new SensorConnectionFile(start.table, start.sensor.location, end.GetTableNum(), end.GetSeatNum());

        int endpoint = sensorConnectionsToReset.Count;          //Avoid changing list while iterating.

        for (int i = endpoint - 1; i >= 0; i--)
        {
            if (sensorConnectionsToReset[i].Equals(sen.gameObject))
            {
                sensorConnectionsToReset.Remove(sen.gameObject);
            }
        }

        endpoint = spawnedSensorConnections.Count;
        for (int i = endpoint - 1; i >= 0; i--)
        {
            if (spawnedSensorConnections[i].Equals(file))
            {
                spawnedSensorConnections.Remove(file);
            }
        }
    }
Exemple #4
0
    public static void SensorConnectionMade(SensorConnection sen)
    {
        SensorConnectionFile file;
        SensorConnector      start = sen.GetStart().GetComponent <SensorConnector>();
        Controller           end   = sen.GetEnd().GetComponent <Controller>();

        file = new SensorConnectionFile(start.table, start.sensor.location, end.GetTableNum(), end.GetSeatNum());
        sensorConnectionsToReset.Add(sen.gameObject);
        if (!spawnedSensorConnections.Contains(file))
        {
            spawnedSensorConnections.Add(file);
        }
    }
Exemple #5
0
    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);
            }
        }
    }