예제 #1
0
        /// <summary>
        /// starts the world
        /// </summary>
        private void Start()
        {
            world            = new World();
            PlayerDictionary = new Dictionary <int, Cube>();
            AllSockets       = new Dictionary <Socket, Cube>();
            SplitDictionary  = new Dictionary <int, HashSet <Cube> >();
            HighestRanks     = new Dictionary <int, Cube>();

            foodCount  = 0;
            VirusCount = 0;

            world = new World();
            timer = new Timer();
            //(1 second/25 heartbeats) * 1000 = 40 miliseconds
            timer.Interval = (double)(1.0 / world.Heartbeat * 1000);
            //the heartbeats of the world new timer and set seconds to update
            timer.Elapsed += Timer_Elapsed;

            lock (world)
            {
                if (foodCount < world.MaxFood)
                {
                    for (int i = foodCount; i < world.MaxFood; i++)
                    {
                        Cube food = world.CreateFood();
                        world.cubeList.Add(food.uid, food);
                    }
                }
                if (VirusCount < world.MaxVirus)
                {
                    for (int i = VirusCount; i < world.MaxVirus; i++)
                    {
                        Cube virus = world.CreateVirus();
                        world.cubeList.Add(virus.uid, virus);
                    }
                }
            }
            foodCount  = world.MaxFood;
            VirusCount = world.MaxVirus;

            timer.Start();

            //Awaiting Network client connections
            NetworkingCode.Server_Awaiting_Client_Loop(Handle_New_Client_Connections, 11000);
            NetworkingCode.Server_Awaiting_Client_Loop(Web_Browser_Connection, 11100);
        }
예제 #2
0
        /// <summary>
        /// updates the world on all changes
        /// </summary>
        private void Update()
        {
            List <Cube> eatenCubes = new List <Cube>();

            lock (world)
            {
                if (foodCount < world.MaxFood)
                {
                    for (int i = foodCount; i < world.MaxFood; i++)
                    {
                        Cube food = world.CreateFood();
                        world.cubeList.Add(food.uid, food);
                        foodCount++;
                        SendCube(food);
                    }
                }
                if (VirusCount < world.MaxVirus)
                {
                    for (int i = VirusCount; i < world.MaxVirus; i++)
                    {
                        Cube virus = world.CreateVirus();
                        world.cubeList.Add(virus.uid, virus);
                        VirusCount++;
                        SendCube(virus);
                    }
                }

                foreach (Cube playerCube in PlayerDictionary.Values)
                {
                    double Player_Top    = playerCube.loc_y - (playerCube.Width / 2);
                    double Player_Bottom = playerCube.loc_y + (playerCube.Width / 2);
                    double Player_Left   = playerCube.loc_x - (playerCube.Width / 2);
                    double Player_Right  = playerCube.loc_x + (playerCube.Width / 2);

                    foreach (Cube cube in world.cubeList.Values)
                    {
                        if ((playerCube.uid != cube.uid))
                        {
                            if ((Player_Left < cube.loc_x) && (cube.loc_x < Player_Right) && (Player_Top < cube.loc_y) &&
                                (cube.loc_y < Player_Bottom))
                            {
                                //beginning of team split check
                                if ((playerCube.team_id > 0 && playerCube.team_id == cube.team_id))
                                {
                                    //setting the timer to the current time to check split time
                                    TimeSpan timerLapse = DateTime.Now - playerCube.trackCube;

                                    //if time not up to 5s bounce off each other
                                    if (timerLapse.TotalSeconds < 5)
                                    {
                                        double Cube_Top    = cube.loc_y - (cube.Width / 2);
                                        double Cube_Bottom = cube.loc_y + (cube.Width / 2);
                                        double Cube_Left   = cube.loc_x - (cube.Width / 2);
                                        double Cube_Right  = cube.loc_x + (cube.Width / 2);

                                        if (Cube_Top < Player_Bottom)
                                        {
                                            double result = Player_Bottom - Cube_Top;
                                            cube.loc_y += result;
                                            if (cube.loc_y + (cube.Width / 2) > world.WorldHeight)
                                            {
                                                cube.loc_y = world.WorldHeight - (cube.Width / 2);
                                            }
                                        }

                                        else if (Cube_Bottom > Player_Top)
                                        {
                                            double result = Cube_Bottom - Player_Top;
                                            cube.loc_y -= result;
                                            if (cube.loc_y - (cube.Width / 2) < 0)
                                            {
                                                cube.loc_y = 0 + (cube.Width / 2);
                                            }
                                        }

                                        if (Cube_Right > Player_Left)
                                        {
                                            double result = Cube_Right - Player_Left;
                                            cube.loc_x -= result;
                                            if (cube.loc_x - (cube.Width / 2) < 0)
                                            {
                                                cube.loc_x = 0 + (cube.Width / 2);
                                            }
                                        }

                                        else if (Cube_Left < Player_Right)
                                        {
                                            double result = Player_Right - Cube_Left;
                                            cube.loc_x += result;
                                            if (cube.loc_x + (cube.Width / 2) > world.WorldWidth)
                                            {
                                                cube.loc_x = world.WorldWidth - (cube.Width / 2);
                                            }
                                        }

                                        foreach (Cube s in SplitDictionary[cube.team_id])
                                        {
                                            if (cube.uid == s.uid)
                                            {
                                                s.loc_x = cube.loc_x;
                                                s.loc_y = cube.loc_y;
                                            }
                                        }
                                    }

                                    //merge team cubes after time elapse
                                    else
                                    {
                                        //have to make sure that the 'cube' isn't the main player cube otherwise it will get eaten by one of
                                        //it's own splits
                                        if (!AllSockets.Values.Contains(cube))
                                        {
                                            playerCube.Mass += cube.Mass;
                                            cube.Mass        = 0;

                                            //checks to see if the merged player cube's mass is greater than it's maxMass and if it is changes it
                                            if (playerCube.Mass > playerCube.maxMass)
                                            {
                                                playerCube.maxMass = playerCube.Mass;
                                            }

                                            //adding splitted cube's stats to player cube's
                                            playerCube.countOfEatenCubes += cube.countOfEatenCubes;

                                            if (cube.namesOfEatenPlayers.Count > 0)
                                            {
                                                foreach (string name in cube.namesOfEatenPlayers)
                                                {
                                                    playerCube.namesOfEatenPlayers.Add(name);
                                                }
                                            }

                                            //trying to fix bug where player will merge together and get bigger than world
                                            if (playerCube.Width >= world.WorldHeight || playerCube.Width >= world.WorldWidth)
                                            {
                                                playerCube.Mass = Math.Pow(playerCube.Mass, 0.5);
                                            }

                                            SplitDictionary[cube.team_id].Remove(cube);
                                            eatenCubes.Add(cube);

                                            if (SplitDictionary[playerCube.team_id].Count <= 1)
                                            {
                                                SplitDictionary.Remove(playerCube.team_id);
                                                playerCube.team_id = 0;
                                            }
                                            SendCube(cube);
                                        }
                                    }
                                } //end of function if two cubes are on the same team

                                //eating food and viruses
                                else
                                {
                                    if (cube.IsVirus == true)
                                    {
                                        VirusHit(playerCube);
                                        VirusCount--;
                                    }
                                    //eating food and other players and checking to make sure the player doesn't get larger than the world.
                                    else
                                    {
                                        playerCube.Mass += cube.Mass;
                                        cube.Mass        = 0;

                                        //checks to see if player's mass now is greater than it's max mass and if it is, changes it
                                        if (playerCube.Mass > playerCube.maxMass)
                                        {
                                            playerCube.maxMass = playerCube.Mass;
                                        }

                                        //trying to fix bug where the player will grow bigger than the world
                                        if (playerCube.Width >= world.WorldHeight || playerCube.Width >= world.WorldWidth)
                                        {
                                            playerCube.Mass = Math.Pow(playerCube.Mass, 0.5);
                                        }
                                        //adds dead cube to a list to be taken off of player or world dictionary
                                        eatenCubes.Add(cube);

                                        //adds 1 to count of cubes eaten for the player
                                        playerCube.countOfEatenCubes++;

                                        //adds name of eaten player to player's playersEaten list.
                                        if (cube.food != true)
                                        {
                                            playerCube.namesOfEatenPlayers.Add(cube.Name);
                                            cube.deathOfCube = DateTime.Now;
                                            cube.timeAlive   = cube.deathOfCube - cube.startOfCube;

                                            //send dead cube to database now?
                                        }
                                        SendCube(cube);
                                    }
                                }
                            }
                        }
                    } //end of going through world with player

                    //handles Attrition
                    if (playerCube.Mass > world.AttritionRate)
                    {
                        //double n = Math.Pow(playerCube.Mass, 1.2) / 10000;
                        double n = Math.Pow(playerCube.Mass, 1.3) / 10000; //Gets rid of more mass to prevent huge cubes that swallow up the world.
                        playerCube.Mass = playerCube.Mass - n;
                    }

                    //Sends player cube to all sockets
                    SendCube(playerCube);
                } //end of going through playerDictionary


                //for splitted cubes to help with ranking
                int    splittedCubesTeamID;
                double totalMassOfPlayer;

                //hasSet stores splitted cubes and update world
                foreach (HashSet <Cube> cubeHashSet in SplitDictionary.Values)
                {
                    totalMassOfPlayer   = 0;
                    splittedCubesTeamID = 0;

                    foreach (Cube splittedCube in cubeHashSet)
                    {
                        splittedCubesTeamID = splittedCube.team_id; //keeps track of what team id we are on

                        double splittedCube_Top    = splittedCube.loc_y - (splittedCube.Width / 2);
                        double splittedCube_Bottom = splittedCube.loc_y + (splittedCube.Width / 2);
                        double splittedCube_Left   = splittedCube.loc_x - (splittedCube.Width / 2);
                        double splittedCube_Right  = splittedCube.loc_x + (splittedCube.Width / 2);

                        foreach (Cube cube in world.cubeList.Values)
                        {
                            if ((splittedCube_Left < cube.loc_x) && (cube.loc_x < splittedCube_Right) &&
                                (splittedCube_Top < cube.loc_y) && (cube.loc_y < splittedCube_Bottom))
                            {
                                if (splittedCube.team_id != cube.team_id)
                                {
                                    splittedCube.Mass += cube.Mass;
                                    cube.Mass          = 0;

                                    //checks to see if splitted cube's mass is greater than it's maxMass and update maxMass if it is.
                                    if (splittedCube.Mass > splittedCube.maxMass)
                                    {
                                        splittedCube.maxMass = splittedCube.Mass;
                                    }

                                    //updates count of food eated
                                    splittedCube.countOfEatenCubes++;

                                    //adds dead cube to list to be taken out of world and/or player list
                                    eatenCubes.Add(cube);

                                    //player has eaten another player
                                    if (cube.IsVirus != true && cube.Name != string.Empty)
                                    {
                                        splittedCube.namesOfEatenPlayers.Add(cube.Name);
                                        cube.deathOfCube = DateTime.Now;
                                        cube.timeAlive   = cube.deathOfCube - cube.startOfCube;

                                        //send cube to database now?
                                    }
                                    SendCube(cube);
                                }
                            }
                        }
                        SendCube(splittedCube);
                        //keeps track of players total mass;
                        totalMassOfPlayer += splittedCube.Mass;
                    }
                    //sets maxMass of player if player is split up
                    foreach (Cube player in AllSockets.Values)
                    {
                        if (splittedCubesTeamID == player.team_id)
                        {
                            if (totalMassOfPlayer > player.maxMass)
                            {
                                player.maxMass = totalMassOfPlayer;

                                //updates other list with players new max mass value
                                world.cubeList[player.uid].maxMass   = player.maxMass;
                                PlayerDictionary[player.uid].maxMass = player.maxMass;
                            }
                        }
                    }
                }

                //goes through high list ranking
                if (AllSockets.Count >= 1)
                {
                    List <Cube> playerMaxMassList = new List <Cube>();
                    foreach (Cube player in AllSockets.Values)
                    {
                        playerMaxMassList.Add(player);
                    }
                    if (playerMaxMassList.Count == 1)
                    {
                        //HighestRanks = new Dictionary<int, Cube>();
                        HighestRanks[1] = playerMaxMassList[0];

                        world.cubeList[playerMaxMassList[0].uid].highestRank   = 1;
                        PlayerDictionary[playerMaxMassList[0].uid].highestRank = 1;
                    }
                    else
                    {
                        //sorts cubes by thier maxMass
                        playerMaxMassList.Sort(delegate(Cube x, Cube y)
                        {
                            return(x.maxMass.CompareTo(y.maxMass));
                        });
                        //if list is less than 5, sets number of times it will add a person to the high rank
                        int numOfRanks;
                        if (playerMaxMassList.Count >= 5)
                        {
                            numOfRanks = 5;
                        }
                        else
                        {
                            numOfRanks = playerMaxMassList.Count;
                        }
                        //puts highest maxMasses in the high rank dictionary
                        for (int i = 1; i <= numOfRanks; i++)
                        {
                            HighestRanks[i] = playerMaxMassList[i - 1];
                            if (playerMaxMassList[playerMaxMassList.Count - i].highestRank < i)
                            {
                                playerMaxMassList[i - 1].highestRank = i;
                            }
                            if (playerMaxMassList[playerMaxMassList.Count - i].Mass != 0)
                            {
                                world.cubeList[playerMaxMassList[playerMaxMassList.Count - i].uid].highestRank   = i;
                                PlayerDictionary[playerMaxMassList[playerMaxMassList.Count - i].uid].highestRank = i;
                            }
                        }
                    }
                }


                //deletes eaten cubes from player dictionary and world list
                foreach (Cube deadCube in eatenCubes)
                {
                    if (PlayerDictionary.ContainsKey(deadCube.uid))
                    {
                        PlayerDictionary.Remove(deadCube.uid);



                        if (AllSockets.Values.Contains(deadCube))
                        {
                            Socket temp = new Socket(SocketType.Stream, ProtocolType.Tcp);
                            foreach (KeyValuePair <Socket, Cube> playerCube in AllSockets)
                            {
                                if (playerCube.Value.uid == deadCube.uid)
                                {
                                    SendCube(deadCube);

                                    //adding dead player to database
                                    SQLDatabase.AddPlayerToDatabase(deadCube);

                                    //Close socket
                                    temp = playerCube.Key;
                                    playerCube.Key.Close();
                                }
                            }
                            AllSockets.Remove(temp);
                        }
                    }
                    if (world.cubeList.ContainsKey(deadCube.uid))
                    {
                        if (deadCube.food == true)
                        {
                            foodCount--;
                        }
                        world.cubeList.Remove(deadCube.uid);
                    }
                }
            }
        }