1 float GameTypeVote_AvailabilityStatus(string gtname)
3 float type = MapInfo_Type_FromString(gtname);
7 if ( autocvar_nextmap != "" )
9 if ( !MapInfo_Get_ByName(autocvar_nextmap, FALSE, 0) )
11 if (!(MapInfo_Map_supportedGametypes & type))
18 float GameTypeVote_GetMask()
20 float n, j, gametype_mask;
21 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
22 n = min(MAPVOTE_COUNT, n);
24 for(j = 0; j < n; ++j)
25 gametype_mask |= MapInfo_Type_FromString(argv(j));
29 string GameTypeVote_MapInfo_FixName(string m)
31 if ( autocvar_sv_vote_gametype )
34 MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
36 return MapInfo_FixName(m);
39 void MapVote_ClearAllVotes()
41 FOR_EACH_CLIENT(other)
45 void MapVote_UnzoneStrings()
48 for(j = 0; j < mapvote_count; ++j)
50 if ( mapvote_maps[j] )
52 strunzone(mapvote_maps[j]);
53 mapvote_maps[j] = string_null;
55 if ( mapvote_maps_pakfile[j] )
57 strunzone(mapvote_maps_pakfile[j]);
58 mapvote_maps_pakfile[j] = string_null;
63 string MapVote_Suggest(string m)
67 return "That's not how to use this command.";
68 if(!autocvar_g_maplist_votable_suggestions)
69 return "Suggestions are not accepted on this server.";
70 if(mapvote_initialized)
72 return "Can't suggest - voting is already in progress!";
73 m = GameTypeVote_MapInfo_FixName(m);
75 return "The map you suggested is not available on this server.";
76 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
78 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
80 if (!autocvar_sv_vote_gametype)
81 if(!MapInfo_CheckMap(m))
82 return "The map you suggested does not support the current game mode.";
83 for(i = 0; i < mapvote_suggestion_ptr; ++i)
84 if(mapvote_suggestions[i] == m)
85 return "This map was already suggested.";
86 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
88 i = floor(random() * mapvote_suggestion_ptr);
92 i = mapvote_suggestion_ptr;
93 mapvote_suggestion_ptr += 1;
95 if(mapvote_suggestions[i] != "")
96 strunzone(mapvote_suggestions[i]);
97 mapvote_suggestions[i] = strzone(m);
98 if(autocvar_sv_eventlog)
99 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));
100 return strcat("Suggestion of ", m, " accepted.");
103 void MapVote_AddVotable(string nextMap, float isSuggestion)
106 string pakfile, mapfile;
110 for(j = 0; j < mapvote_count; ++j)
111 if(mapvote_maps[j] == nextMap)
113 // suggestions might be no longer valid/allowed after gametype switch!
115 if(!MapInfo_CheckMap(nextMap))
117 mapvote_maps[mapvote_count] = strzone(nextMap);
118 mapvote_maps_suggested[mapvote_count] = isSuggestion;
120 pakfile = string_null;
121 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
123 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
124 pakfile = whichpack(strcat(mapfile, ".tga"));
126 pakfile = whichpack(strcat(mapfile, ".jpg"));
128 pakfile = whichpack(strcat(mapfile, ".png"));
132 if(i >= mapvote_screenshot_dirs_count)
133 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
134 for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)
135 pakfile = substring(pakfile, o, -1);
137 mapvote_maps_screenshot_dir[mapvote_count] = i;
138 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
139 mapvote_maps_availability[mapvote_count] = GTV_AVAILABLE;
149 MapVote_ClearAllVotes();
150 MapVote_UnzoneStrings();
153 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
154 mapvote_abstain = autocvar_g_maplist_votable_abstain;
157 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
159 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
160 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
162 // we need this for AddVotable, as that cycles through the screenshot dirs
163 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
164 if(mapvote_screenshot_dirs_count == 0)
165 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
166 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
167 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
168 mapvote_screenshot_dirs[i] = strzone(argv(i));
170 if(mapvote_suggestion_ptr)
171 for(i = 0; i < 100 && mapvote_count < smax; ++i)
172 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], TRUE);
174 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
175 MapVote_AddVotable(GetNextMap(), FALSE);
177 if(mapvote_count == 0)
179 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
180 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
181 if(autocvar_g_maplist_shuffle)
183 localcmd("\nmenu_cmd sync\n");
184 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
185 MapVote_AddVotable(GetNextMap(), FALSE);
188 mapvote_count_real = mapvote_count;
190 MapVote_AddVotable("don't care", 0);
192 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
194 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
195 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
196 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
197 mapvote_keeptwotime = 0;
198 mapvote_message = "Choose a map and press its key!";
203 void MapVote_SendPicture(float id)
206 WriteByte(MSG_ONE, SVC_TEMPENTITY);
207 WriteByte(MSG_ONE, TE_CSQC_PICTURE);
208 WriteByte(MSG_ONE, id);
209 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
213 void MapVote_WriteMask()
216 if ( mapvote_count < 24 )
220 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
221 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
224 if(mapvote_count < 8)
225 WriteByte(MSG_ENTITY, mask);
226 else if (mapvote_count < 16)
227 WriteShort(MSG_ENTITY,mask);
229 WriteLong(MSG_ENTITY, mask);
233 for ( i = 0; i < mapvote_count; ++i )
234 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
238 float MapVote_SendEntity(entity to, float sf)
243 sf &= ~2; // if we send 1, we don't need to also send 2
245 WriteByte(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
246 WriteByte(MSG_ENTITY, sf);
250 // flag 1 == initialization
251 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
252 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
253 WriteString(MSG_ENTITY, "");
254 WriteByte(MSG_ENTITY, mapvote_count);
255 WriteByte(MSG_ENTITY, mapvote_abstain);
256 WriteByte(MSG_ENTITY, mapvote_detail);
257 WriteCoord(MSG_ENTITY, mapvote_timeout);
262 WriteByte(MSG_ENTITY, 1);
263 WriteString(MSG_ENTITY, autocvar_nextmap);
265 else if ( autocvar_sv_vote_gametype )
267 // map vote but gametype has been chosen via voting screen
268 WriteByte(MSG_ENTITY, 2);
269 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
272 WriteByte(MSG_ENTITY, 0); // map vote
276 for(i = 0; i < mapvote_count; ++i)
278 if(mapvote_abstain && i == mapvote_count - 1)
280 WriteString(MSG_ENTITY, ""); // abstain needs no text
281 WriteString(MSG_ENTITY, ""); // abstain needs no pack
282 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
283 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
287 WriteString(MSG_ENTITY, mapvote_maps[i]);
288 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
289 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
290 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
297 // flag 2 == update of mask
304 for(i = 0; i < mapvote_count; ++i)
305 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
306 WriteByte(MSG_ENTITY, mapvote_selections[i]);
308 WriteByte(MSG_ENTITY, to.mapvote);
316 Net_LinkEntity(mapvote_ent = spawn(), FALSE, 0, MapVote_SendEntity);
319 void MapVote_TouchMask()
321 mapvote_ent.SendFlags |= 2;
324 void MapVote_TouchVotes(entity voter)
326 mapvote_ent.SendFlags |= 4;
329 float MapVote_Finished(float mappos)
331 if(alreadychangedlevel)
338 if(autocvar_sv_eventlog)
340 result = strcat(":vote:finished:", mapvote_maps[mappos]);
341 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
342 didntvote = mapvote_voters;
343 for(i = 0; i < mapvote_count; ++i)
344 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
346 didntvote -= mapvote_selections[i];
349 result = strcat(result, ":", mapvote_maps[i]);
350 result = strcat(result, ":", ftos(mapvote_selections[i]));
353 result = strcat(result, ":didn't vote:", ftos(didntvote));
356 if(mapvote_maps_suggested[mappos])
357 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
360 FOR_EACH_REALCLIENT(other)
361 FixClientCvars(other);
365 if ( GameTypeVote_Finished(mappos) )
367 gametypevote = FALSE;
368 if(autocvar_nextmap != "")
370 Map_Goto_SetStr(autocvar_nextmap);
372 alreadychangedlevel = TRUE;
381 Map_Goto_SetStr(mapvote_maps[mappos]);
383 alreadychangedlevel = TRUE;
388 void MapVote_CheckRules_1()
392 for(i = 0; i < mapvote_count; ++i)
393 if( mapvote_maps_availability[i] == GTV_AVAILABLE )
395 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
396 mapvote_selections[i] = 0;
400 FOR_EACH_REALCLIENT(other)
405 i = other.mapvote - 1;
406 //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
407 mapvote_selections[i] = mapvote_selections[i] + 1;
412 float MapVote_CheckRules_2()
415 float firstPlace, secondPlace, currentPlace;
416 float firstPlaceVotes, secondPlaceVotes, currentVotes;
417 float mapvote_voters_real;
420 if(mapvote_count_real == 1)
421 return MapVote_Finished(0);
423 mapvote_voters_real = mapvote_voters;
425 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
427 RandomSelection_Init();
430 for(i = 0; i < mapvote_count_real; ++i)
431 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
433 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
434 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
436 currentVotes = mapvote_selections[i];
440 firstPlaceVotes = RandomSelection_best_priority;
441 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
442 firstPlace = currentPlace;
444 firstPlace = RandomSelection_chosen_float;
446 //dprint("First place: ", ftos(firstPlace), "\n");
447 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
449 RandomSelection_Init();
450 for(i = 0; i < mapvote_count_real; ++i)
452 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
453 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
454 secondPlace = RandomSelection_chosen_float;
455 secondPlaceVotes = RandomSelection_best_priority;
456 //dprint("Second place: ", ftos(secondPlace), "\n");
457 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
460 error("No first place in map vote... WTF?");
462 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
463 return MapVote_Finished(firstPlace);
465 if(mapvote_keeptwotime)
466 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
470 mapvote_message = "Now decide between the TOP TWO!";
471 mapvote_keeptwotime = 0;
472 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
473 result = strcat(result, ":", ftos(firstPlaceVotes));
474 result = strcat(result, ":", mapvote_maps[secondPlace]);
475 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
476 didntvote = mapvote_voters;
477 for(i = 0; i < mapvote_count; ++i)
479 didntvote -= mapvote_selections[i];
483 result = strcat(result, ":", mapvote_maps[i]);
484 result = strcat(result, ":", ftos(mapvote_selections[i]));
485 if(i < mapvote_count_real)
487 mapvote_maps_availability[i] = GTV_FORBIDDEN;
491 result = strcat(result, ":didn't vote:", ftos(didntvote));
492 if(autocvar_sv_eventlog)
504 keeptwo = mapvote_keeptwotime;
505 MapVote_CheckRules_1(); // count
506 if(MapVote_CheckRules_2()) // decide
510 FOR_EACH_REALCLIENT(other)
512 // hide scoreboard again
513 if(other.health != 2342)
517 if(IS_REAL_CLIENT(other))
520 WriteByte(MSG_ONE, SVC_FINALE);
521 WriteString(MSG_ONE, "");
525 // clear possibly invalid votes
526 if ( mapvote_maps_availability[other.mapvote-1] != GTV_AVAILABLE )
528 // use impulses as new vote
529 if(other.impulse >= 1 && other.impulse <= mapvote_count)
530 if( mapvote_maps_availability[other.impulse - 1] == GTV_AVAILABLE )
532 other.mapvote = other.impulse;
533 MapVote_TouchVotes(other);
541 MapVote_CheckRules_1(); // just count
546 // if mapvote is already running, don't do this initialization again
547 if(mapvote_run) { return; }
549 // don't start mapvote until after playerstats gamereport is sent
550 if(PlayerStats_GameReport_DelayMapVote) { return; }
553 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
562 if(alreadychangedlevel)
565 if(time < mapvote_nextthink)
569 mapvote_nextthink = time + 0.5;
571 if(!mapvote_initialized)
573 if(autocvar_rescan_pending == 1)
575 cvar_set("rescan_pending", "2");
576 localcmd("fs_rescan\nrescan_pending 3\n");
579 else if(autocvar_rescan_pending == 2)
583 else if(autocvar_rescan_pending == 3)
585 // now build missing mapinfo files
586 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
589 // we're done, start the timer
590 cvar_set("rescan_pending", "0");
593 mapvote_initialized = TRUE;
594 if(DoNextMapOverride(0))
596 if(!autocvar_g_maplist_votable || player_count <= 0)
602 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
603 else if(autocvar_nextmap == "") { MapVote_Init(); }
609 float GameTypeVote_SetGametype(float type)
611 if (MapInfo_CurrentGametype() == type)
614 float tsave = MapInfo_CurrentGametype();
616 MapInfo_SwitchGameType(type);
619 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
620 if(MapInfo_count > 0)
622 // update lsmaps in case the gametype changed, this way people can easily list maps for it
623 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
624 lsmaps_reply = strzone(getlsmaps());
625 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
629 bprint("Cannot use this game type: no map for it found\n");
630 MapInfo_SwitchGameType(tsave);
631 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
635 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
637 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
638 if(autocvar_g_maplist_shuffle)
644 float gametypevote_finished;
645 float GameTypeVote_Finished(float pos)
647 if(!gametypevote || gametypevote_finished)
650 if ( !GameTypeVote_SetGametype(MapInfo_Type_FromString(mapvote_maps[pos])) )
652 dprint("Selected gametype is not supported by any map");
655 localcmd("sv_vote_gametype_hook_all\n");
656 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
658 gametypevote_finished = TRUE;
663 float GameTypeVote_AddVotable(string nextMode)
666 if ( nextMode == "" || MapInfo_Type_FromString(nextMode) == 0 )
668 for(j = 0; j < mapvote_count; ++j)
669 if(mapvote_maps[j] == nextMode)
672 mapvote_maps[mapvote_count] = strzone(nextMode);
673 mapvote_maps_suggested[mapvote_count] = FALSE;
675 mapvote_maps_screenshot_dir[mapvote_count] = 0;
676 mapvote_maps_pakfile[mapvote_count] = strzone("");
677 mapvote_maps_availability[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
685 float GameTypeVote_Start()
688 MapVote_ClearAllVotes();
689 MapVote_UnzoneStrings();
692 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
694 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
696 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
697 n = min(MAPVOTE_COUNT, n);
699 float really_available, which_available;
700 really_available = 0;
701 which_available = -1;
702 for(j = 0; j < n; ++j)
704 if ( GameTypeVote_AddVotable(argv(j)) )
705 if ( mapvote_maps_availability[j] == GTV_AVAILABLE )
712 mapvote_count_real = mapvote_count;
716 if ( really_available == 0 )
718 if ( mapvote_count > 0 )
719 strunzone(mapvote_maps[0]);
720 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
721 //GameTypeVote_Finished(0);
725 if ( really_available == 1 )
727 //GameTypeVote_Finished(which_available);
728 MapVote_Finished(which_available);
732 mapvote_count_real = mapvote_count;
734 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
735 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
736 mapvote_keeptwotime = 0;