4 #include "../dpdefs/progsdefs.qc"
5 #include "../dpdefs/dpextensions.qc"
7 #include "../common/constants.qh"
8 #include "../common/util.qh"
9 #include "autocvars.qh"
10 #include "constants.qh"
12 #include "../common/mapinfo.qh"
13 #include "command/getreplies.qh"
14 #include "command/cmd.qh"
15 #include "../common/playerstats.qh"
16 #include "mapvoting.qh"
19 float GameTypeVote_AvailabilityStatus(string gtname)
21 float type = MapInfo_Type_FromString(gtname);
25 if ( autocvar_nextmap != "" )
27 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, 0) )
29 if (!(MapInfo_Map_supportedGametypes & type))
36 float GameTypeVote_GetMask()
38 float n, j, gametype_mask;
39 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
40 n = min(MAPVOTE_COUNT, n);
42 for(j = 0; j < n; ++j)
43 gametype_mask |= MapInfo_Type_FromString(argv(j));
47 string GameTypeVote_MapInfo_FixName(string m)
49 if ( autocvar_sv_vote_gametype )
52 MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
54 return MapInfo_FixName(m);
57 void MapVote_ClearAllVotes()
59 FOR_EACH_CLIENT(other)
63 void MapVote_UnzoneStrings()
66 for(j = 0; j < mapvote_count; ++j)
68 if ( mapvote_maps[j] )
70 strunzone(mapvote_maps[j]);
71 mapvote_maps[j] = string_null;
73 if ( mapvote_maps_pakfile[j] )
75 strunzone(mapvote_maps_pakfile[j]);
76 mapvote_maps_pakfile[j] = string_null;
81 string MapVote_Suggest(string m)
85 return "That's not how to use this command.";
86 if(!autocvar_g_maplist_votable_suggestions)
87 return "Suggestions are not accepted on this server.";
88 if(mapvote_initialized)
90 return "Can't suggest - voting is already in progress!";
91 m = GameTypeVote_MapInfo_FixName(m);
93 return "The map you suggested is not available on this server.";
94 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
96 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
98 if (!autocvar_sv_vote_gametype)
99 if(!MapInfo_CheckMap(m))
100 return "The map you suggested does not support the current game mode.";
101 for(i = 0; i < mapvote_suggestion_ptr; ++i)
102 if(mapvote_suggestions[i] == m)
103 return "This map was already suggested.";
104 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
106 i = floor(random() * mapvote_suggestion_ptr);
110 i = mapvote_suggestion_ptr;
111 mapvote_suggestion_ptr += 1;
113 if(mapvote_suggestions[i] != "")
114 strunzone(mapvote_suggestions[i]);
115 mapvote_suggestions[i] = strzone(m);
116 if(autocvar_sv_eventlog)
117 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));
118 return strcat("Suggestion of ", m, " accepted.");
121 void MapVote_AddVotable(string nextMap, float isSuggestion)
124 string pakfile, mapfile;
128 for(j = 0; j < mapvote_count; ++j)
129 if(mapvote_maps[j] == nextMap)
131 // suggestions might be no longer valid/allowed after gametype switch!
133 if(!MapInfo_CheckMap(nextMap))
135 mapvote_maps[mapvote_count] = strzone(nextMap);
136 mapvote_maps_suggested[mapvote_count] = isSuggestion;
138 pakfile = string_null;
139 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
141 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
142 pakfile = whichpack(strcat(mapfile, ".tga"));
144 pakfile = whichpack(strcat(mapfile, ".jpg"));
146 pakfile = whichpack(strcat(mapfile, ".png"));
150 if(i >= mapvote_screenshot_dirs_count)
151 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
152 for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)
153 pakfile = substring(pakfile, o, -1);
155 mapvote_maps_screenshot_dir[mapvote_count] = i;
156 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
157 mapvote_maps_availability[mapvote_count] = GTV_AVAILABLE;
167 MapVote_ClearAllVotes();
168 MapVote_UnzoneStrings();
171 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
172 mapvote_abstain = autocvar_g_maplist_votable_abstain;
175 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
177 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
178 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
180 // we need this for AddVotable, as that cycles through the screenshot dirs
181 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
182 if(mapvote_screenshot_dirs_count == 0)
183 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
184 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
185 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
186 mapvote_screenshot_dirs[i] = strzone(argv(i));
188 if(mapvote_suggestion_ptr)
189 for(i = 0; i < 100 && mapvote_count < smax; ++i)
190 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
192 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
193 MapVote_AddVotable(GetNextMap(), false);
195 if(mapvote_count == 0)
197 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
198 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
199 if(autocvar_g_maplist_shuffle)
201 localcmd("\nmenu_cmd sync\n");
202 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
203 MapVote_AddVotable(GetNextMap(), false);
206 mapvote_count_real = mapvote_count;
208 MapVote_AddVotable("don't care", 0);
210 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
212 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
213 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
214 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
215 mapvote_keeptwotime = 0;
216 mapvote_message = "Choose a map and press its key!";
221 void MapVote_SendPicture(float id)
224 WriteByte(MSG_ONE, SVC_TEMPENTITY);
225 WriteByte(MSG_ONE, TE_CSQC_PICTURE);
226 WriteByte(MSG_ONE, id);
227 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
231 void MapVote_WriteMask()
234 if ( mapvote_count < 24 )
238 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
239 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
242 if(mapvote_count < 8)
243 WriteByte(MSG_ENTITY, mask);
244 else if (mapvote_count < 16)
245 WriteShort(MSG_ENTITY,mask);
247 WriteLong(MSG_ENTITY, mask);
251 for ( i = 0; i < mapvote_count; ++i )
252 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
256 float MapVote_SendEntity(entity to, float sf)
261 sf &= ~2; // if we send 1, we don't need to also send 2
263 WriteByte(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
264 WriteByte(MSG_ENTITY, sf);
268 // flag 1 == initialization
269 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
270 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
271 WriteString(MSG_ENTITY, "");
272 WriteByte(MSG_ENTITY, mapvote_count);
273 WriteByte(MSG_ENTITY, mapvote_abstain);
274 WriteByte(MSG_ENTITY, mapvote_detail);
275 WriteCoord(MSG_ENTITY, mapvote_timeout);
280 WriteByte(MSG_ENTITY, 1);
281 WriteString(MSG_ENTITY, autocvar_nextmap);
283 else if ( autocvar_sv_vote_gametype )
285 // map vote but gametype has been chosen via voting screen
286 WriteByte(MSG_ENTITY, 2);
287 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
290 WriteByte(MSG_ENTITY, 0); // map vote
294 for(i = 0; i < mapvote_count; ++i)
296 if(mapvote_abstain && i == mapvote_count - 1)
298 WriteString(MSG_ENTITY, ""); // abstain needs no text
299 WriteString(MSG_ENTITY, ""); // abstain needs no pack
300 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
301 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
305 WriteString(MSG_ENTITY, mapvote_maps[i]);
306 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
307 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
308 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
315 // flag 2 == update of mask
322 for(i = 0; i < mapvote_count; ++i)
323 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
324 WriteByte(MSG_ENTITY, mapvote_selections[i]);
326 WriteByte(MSG_ENTITY, to.mapvote);
334 Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
337 void MapVote_TouchMask()
339 mapvote_ent.SendFlags |= 2;
342 void MapVote_TouchVotes(entity voter)
344 mapvote_ent.SendFlags |= 4;
347 float MapVote_Finished(float mappos)
349 if(alreadychangedlevel)
356 if(autocvar_sv_eventlog)
358 result = strcat(":vote:finished:", mapvote_maps[mappos]);
359 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
360 didntvote = mapvote_voters;
361 for(i = 0; i < mapvote_count; ++i)
362 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
364 didntvote -= mapvote_selections[i];
367 result = strcat(result, ":", mapvote_maps[i]);
368 result = strcat(result, ":", ftos(mapvote_selections[i]));
371 result = strcat(result, ":didn't vote:", ftos(didntvote));
374 if(mapvote_maps_suggested[mappos])
375 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
378 FOR_EACH_REALCLIENT(other)
379 FixClientCvars(other);
383 if ( GameTypeVote_Finished(mappos) )
385 gametypevote = false;
386 if(autocvar_nextmap != "")
388 Map_Goto_SetStr(autocvar_nextmap);
390 alreadychangedlevel = true;
399 Map_Goto_SetStr(mapvote_maps[mappos]);
401 alreadychangedlevel = true;
406 void MapVote_CheckRules_1()
410 for(i = 0; i < mapvote_count; ++i)
411 if( mapvote_maps_availability[i] == GTV_AVAILABLE )
413 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
414 mapvote_selections[i] = 0;
418 FOR_EACH_REALCLIENT(other)
423 i = other.mapvote - 1;
424 //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
425 mapvote_selections[i] = mapvote_selections[i] + 1;
430 float MapVote_CheckRules_2()
433 float firstPlace, secondPlace, currentPlace;
434 float firstPlaceVotes, secondPlaceVotes, currentVotes;
435 float mapvote_voters_real;
438 if(mapvote_count_real == 1)
439 return MapVote_Finished(0);
441 mapvote_voters_real = mapvote_voters;
443 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
445 RandomSelection_Init();
448 for(i = 0; i < mapvote_count_real; ++i)
449 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
451 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
452 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
454 currentVotes = mapvote_selections[i];
458 firstPlaceVotes = RandomSelection_best_priority;
459 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
460 firstPlace = currentPlace;
462 firstPlace = RandomSelection_chosen_float;
464 //dprint("First place: ", ftos(firstPlace), "\n");
465 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
467 RandomSelection_Init();
468 for(i = 0; i < mapvote_count_real; ++i)
470 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
471 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
472 secondPlace = RandomSelection_chosen_float;
473 secondPlaceVotes = RandomSelection_best_priority;
474 //dprint("Second place: ", ftos(secondPlace), "\n");
475 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
478 error("No first place in map vote... WTF?");
480 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
481 return MapVote_Finished(firstPlace);
483 if(mapvote_keeptwotime)
484 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
488 mapvote_message = "Now decide between the TOP TWO!";
489 mapvote_keeptwotime = 0;
490 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
491 result = strcat(result, ":", ftos(firstPlaceVotes));
492 result = strcat(result, ":", mapvote_maps[secondPlace]);
493 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
494 didntvote = mapvote_voters;
495 for(i = 0; i < mapvote_count; ++i)
497 didntvote -= mapvote_selections[i];
501 result = strcat(result, ":", mapvote_maps[i]);
502 result = strcat(result, ":", ftos(mapvote_selections[i]));
503 if(i < mapvote_count_real)
505 mapvote_maps_availability[i] = GTV_FORBIDDEN;
509 result = strcat(result, ":didn't vote:", ftos(didntvote));
510 if(autocvar_sv_eventlog)
522 keeptwo = mapvote_keeptwotime;
523 MapVote_CheckRules_1(); // count
524 if(MapVote_CheckRules_2()) // decide
528 FOR_EACH_REALCLIENT(other)
530 // hide scoreboard again
531 if(other.health != 2342)
535 if(IS_REAL_CLIENT(other))
538 WriteByte(MSG_ONE, SVC_FINALE);
539 WriteString(MSG_ONE, "");
543 // clear possibly invalid votes
544 if ( mapvote_maps_availability[other.mapvote-1] != GTV_AVAILABLE )
546 // use impulses as new vote
547 if(other.impulse >= 1 && other.impulse <= mapvote_count)
548 if( mapvote_maps_availability[other.impulse - 1] == GTV_AVAILABLE )
550 other.mapvote = other.impulse;
551 MapVote_TouchVotes(other);
559 MapVote_CheckRules_1(); // just count
564 // if mapvote is already running, don't do this initialization again
565 if(mapvote_run) { return; }
567 // don't start mapvote until after playerstats gamereport is sent
568 if(PlayerStats_GameReport_DelayMapVote) { return; }
571 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
580 if(alreadychangedlevel)
583 if(time < mapvote_nextthink)
587 mapvote_nextthink = time + 0.5;
589 if(!mapvote_initialized)
591 if(autocvar_rescan_pending == 1)
593 cvar_set("rescan_pending", "2");
594 localcmd("fs_rescan\nrescan_pending 3\n");
597 else if(autocvar_rescan_pending == 2)
601 else if(autocvar_rescan_pending == 3)
603 // now build missing mapinfo files
604 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
607 // we're done, start the timer
608 cvar_set("rescan_pending", "0");
611 mapvote_initialized = true;
612 if(DoNextMapOverride(0))
614 if(!autocvar_g_maplist_votable || player_count <= 0)
620 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
621 else if(autocvar_nextmap == "") { MapVote_Init(); }
627 float GameTypeVote_SetGametype(float type)
629 if (MapInfo_CurrentGametype() == type)
632 float tsave = MapInfo_CurrentGametype();
634 MapInfo_SwitchGameType(type);
637 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
638 if(MapInfo_count > 0)
640 // update lsmaps in case the gametype changed, this way people can easily list maps for it
641 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
642 lsmaps_reply = strzone(getlsmaps());
643 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
647 bprint("Cannot use this game type: no map for it found\n");
648 MapInfo_SwitchGameType(tsave);
649 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
653 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
655 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
656 if(autocvar_g_maplist_shuffle)
662 float gametypevote_finished;
663 float GameTypeVote_Finished(float pos)
665 if(!gametypevote || gametypevote_finished)
668 if ( !GameTypeVote_SetGametype(MapInfo_Type_FromString(mapvote_maps[pos])) )
670 dprint("Selected gametype is not supported by any map");
673 localcmd("sv_vote_gametype_hook_all\n");
674 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
676 gametypevote_finished = true;
681 float GameTypeVote_AddVotable(string nextMode)
684 if ( nextMode == "" || MapInfo_Type_FromString(nextMode) == 0 )
686 for(j = 0; j < mapvote_count; ++j)
687 if(mapvote_maps[j] == nextMode)
690 mapvote_maps[mapvote_count] = strzone(nextMode);
691 mapvote_maps_suggested[mapvote_count] = false;
693 mapvote_maps_screenshot_dir[mapvote_count] = 0;
694 mapvote_maps_pakfile[mapvote_count] = strzone("");
695 mapvote_maps_availability[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
703 float GameTypeVote_Start()
706 MapVote_ClearAllVotes();
707 MapVote_UnzoneStrings();
710 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
712 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
714 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
715 n = min(MAPVOTE_COUNT, n);
717 float really_available, which_available;
718 really_available = 0;
719 which_available = -1;
720 for(j = 0; j < n; ++j)
722 if ( GameTypeVote_AddVotable(argv(j)) )
723 if ( mapvote_maps_availability[j] == GTV_AVAILABLE )
730 mapvote_count_real = mapvote_count;
734 if ( really_available == 0 )
736 if ( mapvote_count > 0 )
737 strunzone(mapvote_maps[0]);
738 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
739 //GameTypeVote_Finished(0);
743 if ( really_available == 1 )
745 //GameTypeVote_Finished(which_available);
746 MapVote_Finished(which_available);
750 mapvote_count_real = mapvote_count;
752 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
753 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
754 mapvote_keeptwotime = 0;