Exemplo n.º 1
0
        void CheckForBattleExit()
        {
            if (spring.IsRunning && (DateTime.Now - spring.GameStarted) > TimeSpan.FromSeconds(20))
            {
                TasClient.Battle b = tas.GetBattle();
                int count          = 0;
                foreach (TasClient.UserBattleStatus p in b.Users)
                {
                    if (p.IsSpectator)
                    {
                        continue;
                    }

                    TasClient.User u;
                    if (!tas.GetExistingUser(p.name, out u))
                    {
                        continue;
                    }
                    if (u.isInGame)
                    {
                        count++;
                    }
                }
                if (count < 1)
                {
                    tas.Say(TasClient.SayPlace.Battle, "", "closing game, " + count + " active player left in game", true);
                    spring.ExitGame();
                }
            }
        }
Exemplo n.º 2
0
        public void Start(string modname, string mapname)
        {
            Stop();

            config.BattleDetails.Validate();
            if (String.IsNullOrEmpty(modname))
            {
                modname = config.DefaultMod;
            }
            if (String.IsNullOrEmpty(mapname))
            {
                mapname = config.DefaultMap;
            }

            if (!spring.UnitSync.HasMap(mapname))
            {
                mapname = spring.UnitSync.MapList[0];
            }
            if (!spring.UnitSync.HasMod(modname))
            {
                IEnumerator <ModInfo> enu = spring.UnitSync.ModList.Values.GetEnumerator();
                enu.MoveNext();
                modname = enu.Current.Name;
            }

            TasClient.Battle b = new TasClient.Battle(config.Password, config.HostingPort, config.MaxPlayers, config.MinRank, mapname, config.GameTitle.Replace("%1", MainConfig.SpringieVersion), spring.UnitSync.GetModInfo(modname), config.BattleDetails);

            for (int i = 0; i < config.DefaultRectangles.Count; ++i)
            {
                b.Rectangles.Add(i, config.DefaultRectangles[i]);
            }
            tas.OpenBattle(b);
            unsyncKicker = new UnSyncKicker(tas);
        }
Exemplo n.º 3
0
 void tas_UserRemoved(object sender, TasEventArgs e)
 {
     if (spring.IsRunning)
     {
         TasClient.Battle b = tas.GetBattle();
         if (e.ServerParams[0] != tas.UserName && b.ContainsUser(e.ServerParams[0]))
         {
             CheckForBattleExit();
         }
     }
 }
Exemplo n.º 4
0
        internal static int FilterUsers(string[] words, TasClient tas, Spring spring, out string[] vals, out int[] indexes)
        {
            TasClient.Battle b    = tas.GetBattle();
            string[]         temp = new string[b.Users.Count];
            int i = 0;

            foreach (TasClient.UserBattleStatus u in b.Users)
            {
                temp[i++] = u.name;
            }
            return(Filter(temp, words, out vals, out indexes));
        }
Exemplo n.º 5
0
        // SPRING.EXE functions
        public void StartGame(TasClient.Battle battle)
        {
            if (!IsRunning)
            {
                joinedPlayers = new List <string>();
                lostPlayers   = new List <string>();

                ConfigMaker.Generate(path + "springie.txt", battle);
                PrepareRegistry();
                process = new Process();
                process.StartInfo.CreateNoWindow = true;

                if (Program.main.config.SpringStartsHidden)
                {
                    process.StartInfo.Arguments = "/minimise ";
                }
                else
                {
                    process.StartInfo.Arguments = "";
                }
                process.StartInfo.Arguments += "springie.txt";

                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.WorkingDirectory       = path;
                process.StartInfo.FileName        = path + ExecutableName;
                process.StartInfo.UseShellExecute = false;
                process.Start();

                gameStarted     = DateTime.Now;
                process.Exited += new EventHandler(springProcess_Exited);
                process.WaitForInputIdle();

                SpringWindowHidden = Program.main.config.SpringStartsHidden;
                Thread.Sleep(1000);

                RestoreRegistry();

                outputReader      = new Thread(SpringOutputReader);
                outputReader.Name = "SpringOutputReader";
                outputReader.Start(this);

                talker = new Talker(process);

                if (!process.HasExited && SpringStarted != null)
                {
                    SpringStarted(this, new EventArgs());
                }

                ProcessPriority = Program.main.config.HostingProcessPriority;
            }
        }
Exemplo n.º 6
0
        public void ComFix(TasSayEventArgs e, string[] words)
        {
            TasClient.Battle b = tas.GetBattle();
            int cnt            = 0;

            foreach (TasClient.UserBattleStatus u in b.Users)
            {
                if (!u.IsSpectator)
                {
                    tas.ForceTeam(u.name, cnt);
                    cnt++;
                }
            }
            tas.Say(TasClient.SayPlace.Battle, "", "team numbers fixed", true);
        }
Exemplo n.º 7
0
        public void ComRandom(TasSayEventArgs e, string[] words)
        {
            ComFix(e, words);
            TasClient.Battle b = tas.GetBattle();

            List <TasClient.UserBattleStatus> actUsers = new List <TasClient.UserBattleStatus>();

            foreach (TasClient.UserBattleStatus u in b.Users)
            {
                if (!u.IsSpectator)
                {
                    actUsers.Add(u);
                }
            }

            int teamCount = 0;

            if (words.Length > 0)
            {
                int.TryParse(words[0], out teamCount);
            }
            else
            {
                teamCount = 2;
            }
            if (teamCount < 2)
            {
                teamCount = 2;
            }
            if (teamCount > actUsers.Count)
            {
                teamCount = 2;
            }
            Random r = new Random();

            int al = 0;

            while (actUsers.Count > 0)
            {
                int index = r.Next(actUsers.Count);
                tas.ForceAlly(actUsers[index].name, al);
                actUsers.RemoveAt(index);
                al++;
                al = al % teamCount;
            }
            tas.Say(TasClient.SayPlace.Battle, "", "players assigned to " + teamCount + " random teams", true);
        }
Exemplo n.º 8
0
        public static void Generate(string filename, TasClient.Battle b)
        {
            Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

            StringBuilder s = new StringBuilder();

            s.AppendLine("[GAME]");
            s.AppendLine("{");
            s.AppendFormat("  Mapname={0};\n", b.Map);
            s.AppendFormat("  StartMetal={0};\n", b.Details.StartingMetal);
            s.AppendFormat("  StartEnergy={0};\n", b.Details.StartingEnergy);
            s.AppendFormat("  MaxUnits={0};\n", b.Details.MaxUnits);
            s.AppendFormat("  StartPosType={0};\n", (int)b.Details.StartPos);
            s.AppendFormat("  GameMode={0};\n", (int)b.Details.EndCondition);
            s.AppendFormat("  GameType={0};\n", b.Mod.ArchiveName);
            s.AppendFormat("  LimitDGun={0};\n", b.Details.LimitDgun);
            s.AppendFormat("  DiminishingMMs={0};\n", b.Details.DiminishingMM);
            s.AppendFormat("  GhostedBuildings={0};\n", b.Details.GhostedBuildings);
            s.AppendLine();
            s.AppendFormat("  HostIP={0};\n", "localhost");
            s.AppendFormat("  HostPort={0};\n", b.HostPort);
            s.AppendFormat("  MinSpeed={0};\n", 1);
            s.AppendFormat("  MaxSpeed={0};\n", 1);
            s.AppendLine();
            s.AppendFormat("  MyPlayerNum={0};\n", 0);

            List <TasClient.Battle.GrPlayer> players;
            List <TasClient.Battle.GrTeam>   teams;
            List <TasClient.Battle.GrAlly>   alliances;

            b.GroupData(out players, out teams, out alliances);

            s.AppendLine();
            s.AppendFormat("  NumPlayers={0};\n", players.Count);
            s.AppendFormat("  NumTeams={0};\n", teams.Count);
            s.AppendFormat("  NumAllyTeams={0};\n", alliances.Count);
            s.AppendLine();

            // PLAYERS
            for (int i = 0; i < players.Count; ++i)
            {
                TasClient.UserBattleStatus u = players[i].user;
                s.AppendFormat("  [PLAYER{0}]\n", i);
                s.AppendLine("  {");
                s.AppendFormat("     name={0};\n", u.name);
                s.AppendFormat("     Spectator={0};\n", u.IsSpectator ? 1 : 0);
                if (!u.IsSpectator)
                {
                    s.AppendFormat("     team={0};\n", u.TeamNumber);
                }
                s.AppendLine("  }");
            }

            // TEAMS
            s.AppendLine();
            for (int i = 0; i < teams.Count; ++i)
            {
                s.AppendFormat("  [TEAM{0}]\n", i);
                s.AppendLine("  {");
                s.AppendFormat("     TeamLeader={0};\n", teams[i].leader);
                TasClient.UserBattleStatus u = players[teams[i].leader].user;
                s.AppendFormat("     AllyTeam={0};\n", u.AllyNumber);
                s.AppendFormat("     AllyTeam={0};\n", u.AllyNumber);
                s.AppendFormat("     RGBColor={0:F5} {1:F5} {2:F5};\n", (u.TeamColor & 255) / 255.0, ((u.TeamColor >> 8) & 255) / 255.0, ((u.TeamColor >> 16) & 255) / 255.0);
                s.AppendFormat("     Side={0};\n", b.Mod.Sides[u.Side]);
                s.AppendFormat("     Handicap={0};\n", 0);
                s.AppendLine("  }");
            }


            // ALLYS
            s.AppendLine();
            for (int i = 0; i < alliances.Count; ++i)
            {
                s.AppendFormat("[ALLYTEAM{0}]\n", i);
                s.AppendLine("{");
                s.AppendFormat("     NumAllies={0};\n", 0);
                double left, top, right, bottom;
                alliances[i].rect.ToFractions(out left, out top, out right, out bottom);
                s.AppendFormat("     StartRectLeft={0};\n", left);
                s.AppendFormat("     StartRectTop={0};\n", top);
                s.AppendFormat("     StartRectRight={0};\n", right);
                s.AppendFormat("     StartRectBottom={0};\n", bottom);
                s.AppendLine("}");
            }

            s.AppendLine();
            s.AppendFormat("  NumRestrictions={0};\n", 0);
            s.AppendLine("  [RESTRICT]");
            s.AppendLine("  {");
            s.AppendLine("  }");
            s.AppendLine("}");

            StreamWriter f = File.CreateText(filename);

            f.Write(s.ToString());
            f.Flush();
            f.Close();
        }
Exemplo n.º 9
0
        public void ComBalance(TasSayEventArgs e, string[] words)
        {
            List <TasClient.UserBattleStatus> actUsers = new List <TasClient.UserBattleStatus>();
            List <UsRank> ranker = new List <UsRank>();

            TasClient.Battle b = tas.GetBattle();

            foreach (TasClient.UserBattleStatus u in b.Users)
            {
                if (!u.IsSpectator)
                {
                    actUsers.Add(u);
                    TasClient.User p;
                    if (tas.GetExistingUser(u.name, out p))
                    {
                        ranker.Add(new UsRank(ranker.Count, p.rank));
                    }
                    else
                    {
                        ranker.Add(new UsRank(ranker.Count, 0));                                                                              // cannot find user, assume rank 0
                    }
                }
            }

            ComFix(e, words);

            Random rand = new Random();

            List <UsRank> tempList = new List <UsRank>(ranker);

            ranker.Clear();
            while (tempList.Count > 0)
            {
                // find max rank value
                int maxval = int.MinValue;
                foreach (UsRank u in tempList)
                {
                    if (u.rank > maxval)
                    {
                        maxval = u.rank;
                    }
                }

                List <UsRank> l2 = new List <UsRank>(); // pick pieces with max rank to l2
                int           j  = 0;
                while (j < tempList.Count)
                {
                    if (tempList[j].rank == maxval)
                    {
                        l2.Add(tempList[j]);
                        tempList.RemoveAt(j);
                        j = 0;
                    }
                    j++;
                }

                while (l2.Count > 0) // randomly add pieces from l2 to ranker
                {
                    int ind = rand.Next(l2.Count);
                    ranker.Add(l2[ind]);
                    l2.RemoveAt(ind);
                }
            }

            int teamCount = 2;

            if (words.Length > 0)
            {
                int.TryParse(words[0], out teamCount);
            }
            else
            {
                teamCount = 2;
            }
            if (teamCount < 2 || teamCount > ranker.Count)
            {
                teamCount = 2;
            }

            List <UsRank>[] teamUsers = new List <UsRank> [teamCount];
            for (int i = 0; i < teamUsers.Length; ++i)
            {
                teamUsers[i] = new List <UsRank>();
            }
            int[] teamSums = new int[teamCount];

            int cnt = 0;

            while (ranker.Count > 0)
            {
                int minsum = int.MaxValue;
                int minid  = 0;
                for (int i = 0; i < teamCount; ++i)
                {
                    List <UsRank> l = teamUsers[i];
                    // pick only current "row" and find the one with least sum
                    if (l.Count == cnt / teamCount)
                    {
                        if (teamSums[i] < minsum)
                        {
                            minid  = i;
                            minsum = teamSums[i];
                        }
                    }
                }

                teamUsers[minid].Add(ranker[0]);
                teamSums[minid] += ranker[0].rank;
                ranker.RemoveAt(0);

                cnt++;
            }


            // alliances for allinace permutations
            List <int> allys = new List <int>();

            for (int i = 0; i < teamCount; ++i)
            {
                allys.Add(i);
            }

            for (int i = 0; i < teamCount; ++i)
            {
                // permute one alliance
                int rdindex = rand.Next(allys.Count);
                int allynum = allys[rdindex];
                allys.RemoveAt(rdindex);

                foreach (UsRank u in teamUsers[i])
                {
                    tas.ForceAlly(actUsers[u.id].name, allynum);
                }
            }

            string t = string.Format("{0} players balanced to {1} teams (ranks ", actUsers.Count, teamCount);

            for (int i = 0; i < teamSums.Length; ++i)
            {
                if (i > 0)
                {
                    t += ":";
                }
                t += teamSums[i].ToString();
            }
            t += ")";

            tas.Say(TasClient.SayPlace.Battle, "", t, true);
        }