Merge branch 'Mario/teams_bitflag' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / bot.qc
index 9418074..308cd95 100644 (file)
@@ -346,31 +346,28 @@ void bot_endgame()
 
 void bot_relinkplayerlist()
 {
 
 void bot_relinkplayerlist()
 {
-       entity e;
-       entity prevbot;
        player_count = 0;
        currentbots = 0;
        player_count = 0;
        currentbots = 0;
-       player_list = e = findchainflags(flags, FL_CLIENT);
        bot_list = NULL;
        bot_list = NULL;
-       prevbot = NULL;
-       while (e)
+
+       entity prevbot = NULL;
+       FOREACH_CLIENT(true,
        {
        {
-               player_count = player_count + 1;
-               e.nextplayer = e.chain;
-               if (IS_BOT_CLIENT(e))
+               ++player_count;
+
+               if(IS_BOT_CLIENT(it))
                {
                {
-                       if (prevbot)
-                               prevbot.nextbot = e;
+                       if(prevbot)
+                               prevbot.nextbot = it;
                        else
                        {
                        else
                        {
-                               bot_list = e;
+                               bot_list = it;
                                bot_list.nextbot = NULL;
                        }
                                bot_list.nextbot = NULL;
                        }
-                       prevbot = e;
-                       currentbots = currentbots + 1;
+                       prevbot = it;
+                       ++currentbots;
                }
                }
-               e = e.chain;
-       }
+       });
        LOG_TRACE(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
        bot_strategytoken = bot_list;
        bot_strategytoken_taken = true;
        LOG_TRACE(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
        bot_strategytoken = bot_list;
        bot_strategytoken_taken = true;
@@ -427,70 +424,84 @@ void bot_clientconnect(entity this)
 
 void bot_removefromlargestteam()
 {
 
 void bot_removefromlargestteam()
 {
-       float besttime, bestcount, thiscount;
-       entity best, head;
        CheckAllowedTeams(NULL);
        GetTeamCounts(NULL);
        CheckAllowedTeams(NULL);
        GetTeamCounts(NULL);
-       head = findchainfloat(isbot, true);
-       if (!head)
-               return;
-       best = head;
-       besttime = head.createdtime;
-       bestcount = 0;
-       while (head)
+
+       entity best = NULL;
+       float besttime = 0;
+       int bestcount = 0;
+
+       int bcount = 0;
+       FOREACH_ENTITY_FLOAT(isbot, true,
        {
        {
-               if(head.team == NUM_TEAM_1)
-                       thiscount = c1;
-               else if(head.team == NUM_TEAM_2)
-                       thiscount = c2;
-               else if(head.team == NUM_TEAM_3)
-                       thiscount = c3;
-               else if(head.team == NUM_TEAM_4)
-                       thiscount = c4;
-               else
-                       thiscount = 0;
-               if (thiscount > bestcount)
+               ++bcount;
+
+               if(!best)
+               {
+                       best = it;
+                       besttime = it.createdtime;
+               }
+
+               int thiscount = 0;
+
+               switch(it.team)
+               {
+                       case NUM_TEAM_1: thiscount = c1; break;
+                       case NUM_TEAM_2: thiscount = c2; break;
+                       case NUM_TEAM_3: thiscount = c3; break;
+                       case NUM_TEAM_4: thiscount = c4; break;
+               }
+
+               if(thiscount > bestcount)
                {
                        bestcount = thiscount;
                {
                        bestcount = thiscount;
-                       besttime = head.createdtime;
-                       best = head;
+                       besttime = it.createdtime;
+                       best = it;
                }
                }
-               else if (thiscount == bestcount && besttime < head.createdtime)
+               else if(thiscount == bestcount && besttime < it.createdtime)
                {
                {
-                       besttime = head.createdtime;
-                       best = head;
+                       besttime = it.createdtime;
+                       best = it;
                }
                }
-               head = head.chain;
-       }
+       });
+       if(!bcount)
+               return; // no bots to remove
        currentbots = currentbots - 1;
        dropclient(best);
 }
 
 void bot_removenewest()
 {
        currentbots = currentbots - 1;
        dropclient(best);
 }
 
 void bot_removenewest()
 {
-       float besttime;
-       entity best, head;
-
        if(teamplay)
        {
                bot_removefromlargestteam();
                return;
        }
 
        if(teamplay)
        {
                bot_removefromlargestteam();
                return;
        }
 
-       head = findchainfloat(isbot, true);
-       if (!head)
-               return;
-       best = head;
-       besttime = head.createdtime;
-       while (head)
+       float besttime = 0;
+       entity best = NULL;
+       int bcount = 0;
+
+       FOREACH_ENTITY_FLOAT(isbot, true,
        {
        {
-               if (besttime < head.createdtime)
+               ++bcount;
+
+               if(!best)
                {
                {
-                       besttime = head.createdtime;
-                       best = head;
+                       best = it;
+                       besttime = it.createdtime;
                }
                }
-               head = head.chain;
-       }
+
+               if(besttime < it.createdtime)
+               {
+                       besttime = it.createdtime;
+                       best = it;
+               }
+       });
+
+       if(!bcount)
+               return; // no bots to remove
+
        currentbots = currentbots - 1;
        dropclient(best);
 }
        currentbots = currentbots - 1;
        dropclient(best);
 }
@@ -659,9 +670,11 @@ void bot_serverframe()
                else
                {
                        // TODO: Make this check cleaner
                else
                {
                        // TODO: Make this check cleaner
-                       entity wp = findchain(classname, "waypoint");
-                       if(time - wp.nextthink > 10)
+                       IL_EACH(g_waypoints, time - it.nextthink > 10,
+                       {
                                waypoint_save_links();
                                waypoint_save_links();
+                               break;
+                       });
                }
        }
        else
                }
        }
        else