]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/command/vote.qc
Update the types of some voting related globals and fields
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / vote.qc
index 7ff8d2a133e16abd24425031214f7f11201c35aa..4b42b5667ea90e682fe74b5e3996ea4ff73e596c 100644 (file)
@@ -11,6 +11,7 @@
 #include <common/stats.qh>
 #include <common/util.qh>
 #include <common/weapons/_all.qh>
+#include <server/campaign.qh>
 #include <server/client.qh>
 #include <server/command/banning.qh>
 #include <server/command/common.qh>
@@ -34,8 +35,7 @@
 //  Nagger for players to know status of voting
 bool Nagger_SendEntity(entity this, entity to, float sendflags)
 {
-       int nags, i, f, b;
-       entity e;
+       int nags = 0;
        WriteHeader(MSG_ENTITY, ENT_CLIENT_NAGGER);
 
        // bits:
@@ -48,25 +48,23 @@ bool Nagger_SendEntity(entity this, entity to, float sendflags)
        //  64 = vote counts
        // 128 = vote string
 
-       nags = 0;
-       if (readycount)
+       if (warmup_stage)
        {
-               nags |= BIT(0);
-               if (to.ready == 0) nags |= BIT(1);
+               if (readycount)
+               {
+                       nags |= BIT(0);
+                       if (!to.ready) nags |= BIT(1);
+               }
+               nags |= BIT(4);
        }
+
        if (vote_called)
        {
                nags |= BIT(2);
                if (to.vote_selection == 0) nags |= BIT(3);
+               nags |= sendflags & BIT(6);
+               nags |= sendflags & BIT(7);
        }
-       if (warmup_stage) nags |= BIT(4);
-
-       if (sendflags & BIT(6)) nags |= BIT(6);
-
-       if (sendflags & BIT(7)) nags |= BIT(7);
-
-       if (!(nags & 4))  // no vote called? send no string
-               nags &= ~(BIT(6) | BIT(7));
 
        WriteByte(MSG_ENTITY, nags);
 
@@ -80,13 +78,14 @@ bool Nagger_SendEntity(entity this, entity to, float sendflags)
 
        if (nags & BIT(7)) WriteString(MSG_ENTITY, vote_called_display);
 
-       if (nags & 1)
+       if (nags & BIT(0))
        {
-               for (i = 1; i <= maxclients; i += 8)
+               for (int i = 1; i <= maxclients;)
                {
-                       for (f = 0, e = edict_num(i), b = BIT(0); b < BIT(8); b <<= 1, e = nextent(e))
-                               if (!IS_REAL_CLIENT(e) || e.ready)
-                                       f |= b;
+                       int f = 0;
+                       for (int b = 0; b < 8 && i <= maxclients; ++b, ++i)
+                               if (edict_num(i).ready)
+                                       f |= BIT(b);
                        WriteByte(MSG_ENTITY, f);
                }
        }
@@ -159,7 +158,7 @@ void VoteAccept()
 {
        bprint("\{1}^2* ^3", OriginalCallerName(), "^2's vote for ^1", vote_called_display, "^2 was accepted\n");
 
-       if ((vote_called == VOTE_MASTER) && vote_caller) vote_caller.vote_master = 1;
+       if ((vote_called == VOTE_MASTER) && vote_caller) vote_caller.vote_master = true;
        else localcmd(strcat(vote_called_command, "\n"));
 
        if (vote_caller)   vote_caller.vote_waittime = 0;  // people like your votes, you don't need to wait to vote again
@@ -418,7 +417,8 @@ void reset_map(bool dorespawn, bool is_fake_round_start)
 // Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown is set)
 void ReadyRestart_think(entity this)
 {
-       reset_map(true, false);
+       if (!warmup_stage) // if the countdown was not aborted
+               reset_map(true, false);
        delete(this);
 }
 
@@ -427,8 +427,8 @@ void ReadyRestart_force(bool is_fake_round_start)
 {
        if (time <= game_starttime && game_stopped)
                return;
-       if (!is_fake_round_start)
-               bprint("^1Match is restarting...\n");
+       if (!is_fake_round_start && !autocvar_g_campaign)
+               Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_COUNTDOWN_RESTART);
 
        VoteReset();
 
@@ -439,6 +439,8 @@ void ReadyRestart_force(bool is_fake_round_start)
 
        if(warmup_stage)
                game_starttime = time; // Warmup: No countdown in warmup
+       else if (autocvar_g_campaign)
+               game_starttime = time + 3;
        else
                game_starttime = time + RESTART_COUNTDOWN; // Go into match mode
 
@@ -452,8 +454,8 @@ void ReadyRestart_force(bool is_fake_round_start)
        if(!is_fake_round_start && !warmup_stage)
                localcmd("\nsv_hook_warmupend\n");
 
-       // reset the .ready status of all players (also spectators)
-       FOREACH_CLIENT(IS_REAL_CLIENT(it), { it.ready = false; });
+       // reset the .ready status of all clients (including spectators and bots)
+       FOREACH_CLIENT(true, { it.ready = false; });
        readycount = 0;
        Nagger_ReadyCounted();  // NOTE: this causes a resend of that entity, and will also turn off warmup state on the client
 
@@ -483,36 +485,81 @@ void ReadyRestart_force(bool is_fake_round_start)
 
 void ReadyRestart(bool forceWarmupEnd)
 {
-       if (MUTATOR_CALLHOOK(ReadyRestart_Deny) || intermission_running || race_completing) localcmd("restart\n");
+       if (MUTATOR_CALLHOOK(ReadyRestart_Deny) || intermission_running || race_completing)
+       {
+               // NOTE: ReadyRestart support is mandatory in campaign
+               if (autocvar_g_campaign)
+                       error("ReadyRestart must be supported in campaign mode!");
+               localcmd("restart\n"); // if ReadyRestart is denied, restart the server
+       }
        else localcmd("\nsv_hook_readyrestart\n");
 
-       if(forceWarmupEnd)
+       if(forceWarmupEnd || autocvar_g_campaign)
                warmup_stage = 0; // forcefully end warmup and go to match stage
        else
-               warmup_stage = cvar("g_warmup"); // go into warmup if it's enabled, otherwise restart into match stage
+               warmup_stage = autocvar_g_warmup; // go into warmup if it's enabled, otherwise restart into match stage
 
        ReadyRestart_force(false);
 }
 
-// Count the players who are ready and determine whether or not to restart the match
+/* Count the players who are ready and determine whether or not to restart the match when:
+ * a player presses F4                      server/command/cmd.qc  ClientCommand_ready()
+ * a player switches from players to specs  server/client.qc       PutObserverInServer()
+ * a player joins (from specs or directly)  server/client.qc       PutPlayerInServer()
+ * a player disconnects                     server/client.qc       ClientDisconnect()  */
 void ReadyCount()
 {
-       float ready_needed_factor, ready_needed_count;
-       float t_ready = 0, t_players = 0;
+       // cannot reset the game while a timeout is active or pending
+       if (timeout_status) return;
 
-       FOREACH_CLIENT(IS_REAL_CLIENT(it) && (IS_PLAYER(it) || INGAME_JOINED(it)), {
-               ++t_players;
-               if (it.ready) ++t_ready;
-       });
+       int total_players = 0, human_players = 0, humans_ready = 0;
+       readycount = 0;
 
-       readycount = t_ready;
+       FOREACH_CLIENT(IS_PLAYER(it) || INGAME_JOINED(it), {
+               ++total_players;
+               if (it.ready) ++readycount;
+               if (IS_REAL_CLIENT(it))
+               {
+                       ++human_players;
+                       if (it.ready) ++humans_ready;
+               }
+       });
 
        Nagger_ReadyCounted();
 
-       ready_needed_factor = bound(0.5, cvar("g_warmup_majority_factor"), 0.999);
-       ready_needed_count = floor(t_players * ready_needed_factor) + 1;
+       // can't read warmup_stage here as it could have been set to 0 by ReadyRestart()
+       // and we need to use this when checking if we should abort the countdown
+       // map_minplayers can only be > 0 if g_warmup was -1 at worldspawn
+       int minplayers = autocvar_g_warmup > 1 ? autocvar_g_warmup : map_minplayers;
 
-       if (readycount >= ready_needed_count) ReadyRestart(true);
+       if (total_players < minplayers)
+       {
+               if (game_starttime > time) // someone bailed during countdown, back to warmup
+               {
+                       warmup_stage = autocvar_g_warmup; // CAN change it AFTER calling Nagger_ReadyCounted() this frame
+                       game_starttime = time;
+                       Send_Notification(NOTIF_ALL, NULL, MSG_MULTI, COUNTDOWN_STOP, minplayers);
+                       if (!sv_ready_restart_after_countdown) // if we ran reset_map() at start of countdown
+                               FOREACH_CLIENT(IS_PLAYER(it), { GiveWarmupResources(it); });
+               }
+               if (warmup_limit > 0)
+                       warmup_limit = -1;
+               return; // don't ReadyRestart if players are ready but too few
+       }
+       else if (minplayers && warmup_limit <= 0)
+       {
+               // there's enough players now but we're still in infinite warmup
+               warmup_limit = cvar("g_warmup_limit");
+               if (warmup_limit == 0)
+                       warmup_limit = autocvar_timelimit * 60;
+               if (warmup_limit > 0)
+                       game_starttime = time;
+               // implicit else: g_warmup -1 && g_warmup_limit -1 means
+               // warmup continues until enough players AND enough RUPs (no time limit)
+       }
+
+       if (humans_ready && humans_ready >= rint(human_players * bound(0.5, cvar("g_warmup_majority_factor"), 1)))
+               ReadyRestart(true);
 }
 
 
@@ -520,9 +567,9 @@ void ReadyCount()
 //  Supporting functions for VoteCommand
 // ======================================
 
-float Votecommand_check_assignment(entity caller, float assignment)
+bool Votecommand_check_assignment(entity caller, float assignment)
 {
-       float from_server = (!caller);
+       bool from_server = (!caller);
 
        if ((assignment == VC_ASGNMNT_BOTH)
            || ((!from_server && assignment == VC_ASGNMNT_CLIENTONLY)
@@ -541,33 +588,32 @@ string VoteCommand_extractcommand(string input, float startpos, int argc)
        return output;
 }
 
-float VoteCommand_checknasty(string vote_command)
+bool VoteCommand_checknasty(string vote_command)
 {
-       if ((strstrofs(vote_command, ";", 0) >= 0)
+       return !((strstrofs(vote_command, ";", 0) >= 0)
            || (strstrofs(vote_command, "\n", 0) >= 0)
            || (strstrofs(vote_command, "\r", 0) >= 0)
-           || (strstrofs(vote_command, "$", 0) >= 0)) return false;
-
-       return true;
+           || (strstrofs(vote_command, "$", 0) >= 0));
 }
 
 // NOTE: requires input to be surrounded by spaces
 string VoteCommand_checkreplacements(string input)
 {
-       string output = input;
+       // add a space around the input so the start and end of the list is captured
+       string output = strcat(" ", input, " ");
        // allow gotomap replacements
        output = strreplace(" map ", " gotomap ", output);
        output = strreplace(" chmap ", " gotomap ", output);
        return output;
 }
 
-float VoteCommand_checkinlist(string vote_command, string list)
+bool VoteCommand_checkinlist(string vote_command, string list)
 {
-       string l = VoteCommand_checkreplacements(strcat(" ", list, " "));
+       if (vote_command == "" || list == "")
+               return false;
 
-       if (strstrofs(l, VoteCommand_checkreplacements(strcat(" ", vote_command, " ")), 0) >= 0) return true;
-
-       return false;
+       string l = VoteCommand_checkreplacements(list);
+       return (strstrofs(l, VoteCommand_checkreplacements(vote_command), 0) >= 0);
 }
 
 string ValidateMap(string validated_map, entity caller)
@@ -761,11 +807,11 @@ int VoteCommand_parse(entity caller, string vote_command, string vote_list, floa
                        break;
                }
 
-               case "restart": // re-direct all match restarting to resetmatch
-                       vote_command = "resetmatch"; // fall-through
-               case "resetmatch":
+               case "restart":
                {
-                       vote_parsed_command = vote_command;
+                       // add a delay so that vote result can be seen and announcer can be heard
+                       // if the vote is accepted
+                       vote_parsed_command = strcat("defer 1 ", vote_command);
                        vote_parsed_display = strzone(strcat("^1", vote_command));
 
                        break;