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);
259 WriteByte(MSG_ENTITY, gametypevote);
262 WriteString(MSG_ENTITY, autocvar_nextmap);
266 for(i = 0; i < mapvote_count; ++i)
268 if(mapvote_abstain && i == mapvote_count - 1)
270 WriteString(MSG_ENTITY, ""); // abstain needs no text
271 WriteString(MSG_ENTITY, ""); // abstain needs no pack
272 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
273 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
277 WriteString(MSG_ENTITY, mapvote_maps[i]);
278 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
279 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
280 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
287 // flag 2 == update of mask
294 for(i = 0; i < mapvote_count; ++i)
295 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
296 WriteByte(MSG_ENTITY, mapvote_selections[i]);
298 WriteByte(MSG_ENTITY, to.mapvote);
306 Net_LinkEntity(mapvote_ent = spawn(), FALSE, 0, MapVote_SendEntity);
309 void MapVote_TouchMask()
311 mapvote_ent.SendFlags |= 2;
314 void MapVote_TouchVotes(entity voter)
316 mapvote_ent.SendFlags |= 4;
319 float MapVote_Finished(float mappos)
321 if(alreadychangedlevel)
328 if(autocvar_sv_eventlog)
330 result = strcat(":vote:finished:", mapvote_maps[mappos]);
331 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
332 didntvote = mapvote_voters;
333 for(i = 0; i < mapvote_count; ++i)
334 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
336 didntvote -= mapvote_selections[i];
339 result = strcat(result, ":", mapvote_maps[i]);
340 result = strcat(result, ":", ftos(mapvote_selections[i]));
343 result = strcat(result, ":didn't vote:", ftos(didntvote));
346 if(mapvote_maps_suggested[mappos])
347 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
350 FOR_EACH_REALCLIENT(other)
351 FixClientCvars(other);
355 if ( GameTypeVote_Finished(mappos) )
357 gametypevote = FALSE;
358 if(autocvar_nextmap != "")
360 Map_Goto_SetStr(autocvar_nextmap);
362 alreadychangedlevel = TRUE;
371 Map_Goto_SetStr(mapvote_maps[mappos]);
373 alreadychangedlevel = TRUE;
378 void MapVote_CheckRules_1()
382 for(i = 0; i < mapvote_count; ++i)
383 if( mapvote_maps_availability[i] == GTV_AVAILABLE )
385 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
386 mapvote_selections[i] = 0;
390 FOR_EACH_REALCLIENT(other)
395 i = other.mapvote - 1;
396 //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
397 mapvote_selections[i] = mapvote_selections[i] + 1;
402 float MapVote_CheckRules_2()
405 float firstPlace, secondPlace, currentPlace;
406 float firstPlaceVotes, secondPlaceVotes, currentVotes;
407 float mapvote_voters_real;
410 if(mapvote_count_real == 1)
411 return MapVote_Finished(0);
413 mapvote_voters_real = mapvote_voters;
415 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
417 RandomSelection_Init();
420 for(i = 0; i < mapvote_count_real; ++i)
421 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
423 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
424 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
426 currentVotes = mapvote_selections[i];
430 firstPlaceVotes = RandomSelection_best_priority;
431 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
432 firstPlace = currentPlace;
434 firstPlace = RandomSelection_chosen_float;
436 //dprint("First place: ", ftos(firstPlace), "\n");
437 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
439 RandomSelection_Init();
440 for(i = 0; i < mapvote_count_real; ++i)
442 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
443 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
444 secondPlace = RandomSelection_chosen_float;
445 secondPlaceVotes = RandomSelection_best_priority;
446 //dprint("Second place: ", ftos(secondPlace), "\n");
447 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
450 error("No first place in map vote... WTF?");
452 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
453 return MapVote_Finished(firstPlace);
455 if(mapvote_keeptwotime)
456 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
460 mapvote_message = "Now decide between the TOP TWO!";
461 mapvote_keeptwotime = 0;
462 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
463 result = strcat(result, ":", ftos(firstPlaceVotes));
464 result = strcat(result, ":", mapvote_maps[secondPlace]);
465 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
466 didntvote = mapvote_voters;
467 for(i = 0; i < mapvote_count; ++i)
469 didntvote -= mapvote_selections[i];
473 result = strcat(result, ":", mapvote_maps[i]);
474 result = strcat(result, ":", ftos(mapvote_selections[i]));
475 if(i < mapvote_count_real)
477 mapvote_maps_availability[i] = GTV_FORBIDDEN;
481 result = strcat(result, ":didn't vote:", ftos(didntvote));
482 if(autocvar_sv_eventlog)
494 keeptwo = mapvote_keeptwotime;
495 MapVote_CheckRules_1(); // count
496 if(MapVote_CheckRules_2()) // decide
500 FOR_EACH_REALCLIENT(other)
502 // hide scoreboard again
503 if(other.health != 2342)
507 if(IS_REAL_CLIENT(other))
510 WriteByte(MSG_ONE, SVC_FINALE);
511 WriteString(MSG_ONE, "");
515 // clear possibly invalid votes
516 if ( mapvote_maps_availability[other.mapvote-1] != GTV_AVAILABLE )
518 // use impulses as new vote
519 if(other.impulse >= 1 && other.impulse <= mapvote_count)
520 if( mapvote_maps_availability[other.impulse - 1] == GTV_AVAILABLE )
522 other.mapvote = other.impulse;
523 MapVote_TouchVotes(other);
531 MapVote_CheckRules_1(); // just count
539 // wait for stats to be sent first
540 if(!playerstats_waitforme)
544 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
553 if(alreadychangedlevel)
556 if(time < mapvote_nextthink)
560 mapvote_nextthink = time + 0.5;
562 if(!mapvote_initialized)
564 if(autocvar_rescan_pending == 1)
566 cvar_set("rescan_pending", "2");
567 localcmd("fs_rescan\nrescan_pending 3\n");
570 else if(autocvar_rescan_pending == 2)
574 else if(autocvar_rescan_pending == 3)
576 // now build missing mapinfo files
577 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
580 // we're done, start the timer
581 cvar_set("rescan_pending", "0");
584 mapvote_initialized = TRUE;
585 if(DoNextMapOverride(0))
587 if(!autocvar_g_maplist_votable || player_count <= 0)
593 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
594 else if(autocvar_nextmap == "") { MapVote_Init(); }
600 float GameTypeVote_SetGametype(float type)
602 if (MapInfo_CurrentGametype() == type)
605 float tsave = MapInfo_CurrentGametype();
607 MapInfo_SwitchGameType(type);
610 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
611 if(MapInfo_count > 0)
613 // update lsmaps in case the gametype changed, this way people can easily list maps for it
614 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
615 lsmaps_reply = strzone(getlsmaps());
616 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
620 bprint("Cannot use this game type: no map for it found\n");
621 MapInfo_SwitchGameType(tsave);
622 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
626 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
628 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
629 if(autocvar_g_maplist_shuffle)
635 float gametypevote_finished;
636 float GameTypeVote_Finished(float pos)
638 if(!gametypevote || gametypevote_finished)
641 if ( !GameTypeVote_SetGametype(MapInfo_Type_FromString(mapvote_maps[pos])) )
643 dprint("Selected gametype is not supported by any map");
646 localcmd("sv_vote_gametype_hook_all\n");
647 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
649 gametypevote_finished = TRUE;
654 float GameTypeVote_AddVotable(string nextMode)
657 if ( nextMode == "" || MapInfo_Type_FromString(nextMode) == 0 )
659 for(j = 0; j < mapvote_count; ++j)
660 if(mapvote_maps[j] == nextMode)
663 mapvote_maps[mapvote_count] = strzone(nextMode);
664 mapvote_maps_suggested[mapvote_count] = FALSE;
666 mapvote_maps_screenshot_dir[mapvote_count] = 0;
667 mapvote_maps_pakfile[mapvote_count] = strzone("");
668 mapvote_maps_availability[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
676 float GameTypeVote_Start()
679 MapVote_ClearAllVotes();
680 MapVote_UnzoneStrings();
683 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
685 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
687 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
688 n = min(MAPVOTE_COUNT, n);
690 float really_available, which_available;
691 really_available = 0;
692 which_available = -1;
693 for(j = 0; j < n; ++j)
695 if ( GameTypeVote_AddVotable(argv(j)) )
696 if ( mapvote_maps_availability[j] == GTV_AVAILABLE )
703 mapvote_count_real = mapvote_count;
707 if ( really_available == 0 )
709 if ( mapvote_count > 0 )
710 strunzone(mapvote_maps[0]);
711 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
712 //GameTypeVote_Finished(0);
716 if ( really_available == 1 )
718 //GameTypeVote_Finished(which_available);
719 MapVote_Finished(which_available);
723 mapvote_count_real = mapvote_count;
725 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
726 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
727 mapvote_keeptwotime = 0;