1 float GameTypeVote_AvailabilityStatus(string gtname)
3 float type = MapInfo_Type_FromString(gtname);
7 if ( autocvar_nextmap != "" )
8 if ( !MapInfo_Get_ByName(autocvar_nextmap, FALSE, type) )
14 float GameTypeVote_GetMask()
16 float n, j, gametype_mask;
17 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
18 n = min(MAPVOTE_COUNT, n);
20 for(j = 0; j < n; ++j)
21 gametype_mask |= MapInfo_Type_FromString(argv(j));
25 string GameTypeVote_MapInfo_FixName(string m)
27 if ( autocvar_sv_vote_gametype )
30 MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
32 return MapInfo_FixName(m);
35 void MapVote_ClearAllVotes()
37 FOR_EACH_CLIENT(other)
41 void MapVote_UnzoneStrings()
44 for(j = 0; j < mapvote_count; ++j)
46 if ( mapvote_maps[j] )
48 strunzone(mapvote_maps[j]);
49 mapvote_maps[j] = string_null;
51 if ( mapvote_maps_pakfile[j] )
53 strunzone(mapvote_maps_pakfile[j]);
54 mapvote_maps_pakfile[j] = string_null;
59 string MapVote_Suggest(string m)
63 return "That's not how to use this command.";
64 if(!autocvar_g_maplist_votable_suggestions)
65 return "Suggestions are not accepted on this server.";
66 if(mapvote_initialized)
68 return "Can't suggest - voting is already in progress!";
69 m = GameTypeVote_MapInfo_FixName(m);
71 return "The map you suggested is not available on this server.";
72 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
74 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
76 if (!autocvar_sv_vote_gametype)
77 if(!MapInfo_CheckMap(m))
78 return "The map you suggested does not support the current game mode.";
79 for(i = 0; i < mapvote_suggestion_ptr; ++i)
80 if(mapvote_suggestions[i] == m)
81 return "This map was already suggested.";
82 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
84 i = floor(random() * mapvote_suggestion_ptr);
88 i = mapvote_suggestion_ptr;
89 mapvote_suggestion_ptr += 1;
91 if(mapvote_suggestions[i] != "")
92 strunzone(mapvote_suggestions[i]);
93 mapvote_suggestions[i] = strzone(m);
94 if(autocvar_sv_eventlog)
95 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));
96 return strcat("Suggestion of ", m, " accepted.");
99 void MapVote_AddVotable(string nextMap, float isSuggestion)
102 string pakfile, mapfile;
106 for(j = 0; j < mapvote_count; ++j)
107 if(mapvote_maps[j] == nextMap)
109 // suggestions might be no longer valid/allowed after gametype switch!
111 if(!MapInfo_CheckMap(nextMap))
113 mapvote_maps[mapvote_count] = strzone(nextMap);
114 mapvote_maps_suggested[mapvote_count] = isSuggestion;
116 pakfile = string_null;
117 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
119 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
120 pakfile = whichpack(strcat(mapfile, ".tga"));
122 pakfile = whichpack(strcat(mapfile, ".jpg"));
124 pakfile = whichpack(strcat(mapfile, ".png"));
128 if(i >= mapvote_screenshot_dirs_count)
129 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
130 for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)
131 pakfile = substring(pakfile, o, -1);
133 mapvote_maps_screenshot_dir[mapvote_count] = i;
134 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
135 mapvote_maps_availability[mapvote_count] = GTV_AVAILABLE;
145 MapVote_ClearAllVotes();
146 MapVote_UnzoneStrings();
149 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
150 mapvote_abstain = autocvar_g_maplist_votable_abstain;
153 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
155 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
156 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
158 // we need this for AddVotable, as that cycles through the screenshot dirs
159 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
160 if(mapvote_screenshot_dirs_count == 0)
161 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
162 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
163 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
164 mapvote_screenshot_dirs[i] = strzone(argv(i));
166 if(mapvote_suggestion_ptr)
167 for(i = 0; i < 100 && mapvote_count < smax; ++i)
168 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], TRUE);
170 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
171 MapVote_AddVotable(GetNextMap(), FALSE);
173 if(mapvote_count == 0)
175 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
176 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
177 if(autocvar_g_maplist_shuffle)
179 localcmd("\nmenu_cmd sync\n");
180 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
181 MapVote_AddVotable(GetNextMap(), FALSE);
184 mapvote_count_real = mapvote_count;
186 MapVote_AddVotable("don't care", 0);
188 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
190 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
191 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
192 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
193 mapvote_keeptwotime = 0;
194 mapvote_message = "Choose a map and press its key!";
199 void MapVote_SendPicture(float id)
202 WriteByte(MSG_ONE, SVC_TEMPENTITY);
203 WriteByte(MSG_ONE, TE_CSQC_PICTURE);
204 WriteByte(MSG_ONE, id);
205 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
209 void MapVote_WriteMask()
212 if ( mapvote_count < 24 )
216 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
217 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
220 if(mapvote_count < 8)
221 WriteByte(MSG_ENTITY, mask);
222 else if (mapvote_count < 16)
223 WriteShort(MSG_ENTITY,mask);
225 WriteLong(MSG_ENTITY, mask);
229 for ( i = 0; i < mapvote_count; ++i )
230 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
234 float MapVote_SendEntity(entity to, float sf)
239 sf &= ~2; // if we send 1, we don't need to also send 2
241 WriteByte(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
242 WriteByte(MSG_ENTITY, sf);
246 // flag 1 == initialization
247 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
248 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
249 WriteString(MSG_ENTITY, "");
250 WriteByte(MSG_ENTITY, mapvote_count);
251 WriteByte(MSG_ENTITY, mapvote_abstain);
252 WriteByte(MSG_ENTITY, mapvote_detail);
253 WriteCoord(MSG_ENTITY, mapvote_timeout);
255 WriteByte(MSG_ENTITY, gametypevote);
258 WriteString(MSG_ENTITY, autocvar_nextmap);
262 for(i = 0; i < mapvote_count; ++i)
264 if(mapvote_abstain && i == mapvote_count - 1)
266 WriteString(MSG_ENTITY, ""); // abstain needs no text
267 WriteString(MSG_ENTITY, ""); // abstain needs no pack
268 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
269 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
273 WriteString(MSG_ENTITY, mapvote_maps[i]);
274 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
275 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
276 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
283 // flag 2 == update of mask
290 for(i = 0; i < mapvote_count; ++i)
291 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
292 WriteByte(MSG_ENTITY, mapvote_selections[i]);
294 WriteByte(MSG_ENTITY, to.mapvote);
302 Net_LinkEntity(mapvote_ent = spawn(), FALSE, 0, MapVote_SendEntity);
305 void MapVote_TouchMask()
307 mapvote_ent.SendFlags |= 2;
310 void MapVote_TouchVotes(entity voter)
312 mapvote_ent.SendFlags |= 4;
315 float MapVote_Finished(float mappos)
317 if(alreadychangedlevel)
324 if(autocvar_sv_eventlog)
326 result = strcat(":vote:finished:", mapvote_maps[mappos]);
327 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
328 didntvote = mapvote_voters;
329 for(i = 0; i < mapvote_count; ++i)
330 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
332 didntvote -= mapvote_selections[i];
335 result = strcat(result, ":", mapvote_maps[i]);
336 result = strcat(result, ":", ftos(mapvote_selections[i]));
339 result = strcat(result, ":didn't vote:", ftos(didntvote));
342 if(mapvote_maps_suggested[mappos])
343 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
346 FOR_EACH_REALCLIENT(other)
347 FixClientCvars(other);
351 if ( GameTypeVote_Finished(mappos) )
353 gametypevote = FALSE;
354 if(autocvar_nextmap != "")
356 Map_Goto_SetStr(autocvar_nextmap);
358 alreadychangedlevel = TRUE;
367 Map_Goto_SetStr(mapvote_maps[mappos]);
369 alreadychangedlevel = TRUE;
374 void MapVote_CheckRules_1()
378 for(i = 0; i < mapvote_count; ++i)
379 if( mapvote_maps_availability[i] == GTV_AVAILABLE )
381 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
382 mapvote_selections[i] = 0;
386 FOR_EACH_REALCLIENT(other)
391 i = other.mapvote - 1;
392 //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
393 mapvote_selections[i] = mapvote_selections[i] + 1;
398 float MapVote_CheckRules_2()
401 float firstPlace, secondPlace, currentPlace;
402 float firstPlaceVotes, secondPlaceVotes, currentVotes;
403 float mapvote_voters_real;
406 if(mapvote_count_real == 1)
407 return MapVote_Finished(0);
409 mapvote_voters_real = mapvote_voters;
411 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
413 RandomSelection_Init();
416 for(i = 0; i < mapvote_count_real; ++i)
417 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
419 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
420 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
422 currentVotes = mapvote_selections[i];
426 firstPlaceVotes = RandomSelection_best_priority;
427 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
428 firstPlace = currentPlace;
430 firstPlace = RandomSelection_chosen_float;
432 //dprint("First place: ", ftos(firstPlace), "\n");
433 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
435 RandomSelection_Init();
436 for(i = 0; i < mapvote_count_real; ++i)
438 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
439 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
440 secondPlace = RandomSelection_chosen_float;
441 secondPlaceVotes = RandomSelection_best_priority;
442 //dprint("Second place: ", ftos(secondPlace), "\n");
443 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
446 error("No first place in map vote... WTF?");
448 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
449 return MapVote_Finished(firstPlace);
451 if(mapvote_keeptwotime)
452 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
456 mapvote_message = "Now decide between the TOP TWO!";
457 mapvote_keeptwotime = 0;
458 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
459 result = strcat(result, ":", ftos(firstPlaceVotes));
460 result = strcat(result, ":", mapvote_maps[secondPlace]);
461 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
462 didntvote = mapvote_voters;
463 for(i = 0; i < mapvote_count; ++i)
465 didntvote -= mapvote_selections[i];
469 result = strcat(result, ":", mapvote_maps[i]);
470 result = strcat(result, ":", ftos(mapvote_selections[i]));
471 if(i < mapvote_count_real)
473 mapvote_maps_availability[i] = GTV_FORBIDDEN;
477 result = strcat(result, ":didn't vote:", ftos(didntvote));
478 if(autocvar_sv_eventlog)
490 keeptwo = mapvote_keeptwotime;
491 MapVote_CheckRules_1(); // count
492 if(MapVote_CheckRules_2()) // decide
496 FOR_EACH_REALCLIENT(other)
498 // hide scoreboard again
499 if(other.health != 2342)
503 if(IS_REAL_CLIENT(other))
506 WriteByte(MSG_ONE, SVC_FINALE);
507 WriteString(MSG_ONE, "");
511 // clear possibly invalid votes
512 if ( mapvote_maps_availability[other.mapvote-1] != GTV_AVAILABLE )
514 // use impulses as new vote
515 if(other.impulse >= 1 && other.impulse <= mapvote_count)
516 if( mapvote_maps_availability[other.impulse - 1] == GTV_AVAILABLE )
518 other.mapvote = other.impulse;
519 MapVote_TouchVotes(other);
527 MapVote_CheckRules_1(); // just count
535 // wait for stats to be sent first
536 if(!playerstats_waitforme)
540 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
549 if(alreadychangedlevel)
552 if(time < mapvote_nextthink)
556 mapvote_nextthink = time + 0.5;
558 if(!mapvote_initialized)
560 if(autocvar_rescan_pending == 1)
562 cvar_set("rescan_pending", "2");
563 localcmd("fs_rescan\nrescan_pending 3\n");
566 else if(autocvar_rescan_pending == 2)
570 else if(autocvar_rescan_pending == 3)
572 // now build missing mapinfo files
573 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
576 // we're done, start the timer
577 cvar_set("rescan_pending", "0");
580 mapvote_initialized = TRUE;
581 if(DoNextMapOverride(0))
583 if(!autocvar_g_maplist_votable || player_count <= 0)
589 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
590 else if(autocvar_nextmap == "") { MapVote_Init(); }
596 float GameTypeVote_SetGametype(float type)
598 if (MapInfo_CurrentGametype() == type)
601 float tsave = MapInfo_CurrentGametype();
603 MapInfo_SwitchGameType(type);
606 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
607 if(MapInfo_count > 0)
609 // update lsmaps in case the gametype changed, this way people can easily list maps for it
610 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
611 lsmaps_reply = strzone(getlsmaps());
612 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
616 bprint("Cannot use this game type: no map for it found\n");
617 MapInfo_SwitchGameType(tsave);
618 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
622 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
624 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
625 if(autocvar_g_maplist_shuffle)
631 float gametypevote_finished;
632 float GameTypeVote_Finished(float pos)
634 if(!gametypevote || gametypevote_finished)
637 if ( !GameTypeVote_SetGametype(MapInfo_Type_FromString(mapvote_maps[pos])) )
639 dprint("Selected gametype is not supported by any map");
642 localcmd("sv_vote_gametype_hook_all\n");
643 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
645 gametypevote_finished = TRUE;
650 float GameTypeVote_AddVotable(string nextMode)
653 if ( nextMode == "" || MapInfo_Type_FromString(nextMode) == 0 )
655 for(j = 0; j < mapvote_count; ++j)
656 if(mapvote_maps[j] == nextMode)
659 mapvote_maps[mapvote_count] = strzone(nextMode);
660 mapvote_maps_suggested[mapvote_count] = FALSE;
662 mapvote_maps_screenshot_dir[mapvote_count] = 0;
663 mapvote_maps_pakfile[mapvote_count] = strzone("");
664 mapvote_maps_availability[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
672 float GameTypeVote_Start()
675 MapVote_ClearAllVotes();
676 MapVote_UnzoneStrings();
679 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
681 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
683 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
684 n = min(MAPVOTE_COUNT, n);
686 float really_available, which_available;
687 really_available = 0;
688 which_available = -1;
689 for(j = 0; j < n; ++j)
691 if ( GameTypeVote_AddVotable(argv(j)) )
692 if ( mapvote_maps_availability[j] == GTV_AVAILABLE )
699 mapvote_count_real = mapvote_count;
703 if ( really_available == 0 )
705 if ( mapvote_count > 0 )
706 strunzone(mapvote_maps[0]);
707 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
708 //GameTypeVote_Finished(0);
712 if ( really_available == 1 )
714 //GameTypeVote_Finished(which_available);
715 MapVote_Finished(which_available);
719 mapvote_count_real = mapvote_count;
721 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
722 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
723 mapvote_keeptwotime = 0;