4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
6 #include "../common/constants.qh"
7 #include "../common/util.qh"
8 #include "autocvars.qh"
9 #include "constants.qh"
11 #include "../common/mapinfo.qh"
12 #include "command/getreplies.qh"
13 #include "command/cmd.qh"
14 #include "../common/playerstats.qh"
15 #include "mapvoting.qh"
18 float GameTypeVote_AvailabilityStatus(string gtname)
20 float type = MapInfo_Type_FromString(gtname);
24 if ( autocvar_nextmap != "" )
26 if ( !MapInfo_Get_ByName(autocvar_nextmap, false, 0) )
28 if (!(MapInfo_Map_supportedGametypes & type))
35 float GameTypeVote_GetMask()
37 float n, j, gametype_mask;
38 n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
39 n = min(MAPVOTE_COUNT, n);
41 for(j = 0; j < n; ++j)
42 gametype_mask |= MapInfo_Type_FromString(argv(j));
46 string GameTypeVote_MapInfo_FixName(string m)
48 if ( autocvar_sv_vote_gametype )
51 MapInfo_FilterGametype(GameTypeVote_GetMask(), 0, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
53 return MapInfo_FixName(m);
56 void MapVote_ClearAllVotes()
58 FOR_EACH_CLIENT(other)
62 void MapVote_UnzoneStrings()
65 for(j = 0; j < mapvote_count; ++j)
67 if ( mapvote_maps[j] )
69 strunzone(mapvote_maps[j]);
70 mapvote_maps[j] = string_null;
72 if ( mapvote_maps_pakfile[j] )
74 strunzone(mapvote_maps_pakfile[j]);
75 mapvote_maps_pakfile[j] = string_null;
80 string MapVote_Suggest(string m)
84 return "That's not how to use this command.";
85 if(!autocvar_g_maplist_votable_suggestions)
86 return "Suggestions are not accepted on this server.";
87 if(mapvote_initialized)
89 return "Can't suggest - voting is already in progress!";
90 m = GameTypeVote_MapInfo_FixName(m);
92 return "The map you suggested is not available on this server.";
93 if(!autocvar_g_maplist_votable_suggestions_override_mostrecent)
95 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
97 if (!autocvar_sv_vote_gametype)
98 if(!MapInfo_CheckMap(m))
99 return "The map you suggested does not support the current game mode.";
100 for(i = 0; i < mapvote_suggestion_ptr; ++i)
101 if(mapvote_suggestions[i] == m)
102 return "This map was already suggested.";
103 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
105 i = floor(random() * mapvote_suggestion_ptr);
109 i = mapvote_suggestion_ptr;
110 mapvote_suggestion_ptr += 1;
112 if(mapvote_suggestions[i] != "")
113 strunzone(mapvote_suggestions[i]);
114 mapvote_suggestions[i] = strzone(m);
115 if(autocvar_sv_eventlog)
116 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));
117 return strcat("Suggestion of ", m, " accepted.");
120 void MapVote_AddVotable(string nextMap, float isSuggestion)
123 string pakfile, mapfile;
127 for(j = 0; j < mapvote_count; ++j)
128 if(mapvote_maps[j] == nextMap)
130 // suggestions might be no longer valid/allowed after gametype switch!
132 if(!MapInfo_CheckMap(nextMap))
134 mapvote_maps[mapvote_count] = strzone(nextMap);
135 mapvote_maps_suggested[mapvote_count] = isSuggestion;
137 pakfile = string_null;
138 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
140 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);
141 pakfile = whichpack(strcat(mapfile, ".tga"));
143 pakfile = whichpack(strcat(mapfile, ".jpg"));
145 pakfile = whichpack(strcat(mapfile, ".png"));
149 if(i >= mapvote_screenshot_dirs_count)
150 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?
151 for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)
152 pakfile = substring(pakfile, o, -1);
154 mapvote_maps_screenshot_dir[mapvote_count] = i;
155 mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);
156 mapvote_maps_availability[mapvote_count] = GTV_AVAILABLE;
166 MapVote_ClearAllVotes();
167 MapVote_UnzoneStrings();
170 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
171 mapvote_abstain = autocvar_g_maplist_votable_abstain;
174 nmax = min(MAPVOTE_COUNT - 1, autocvar_g_maplist_votable);
176 nmax = min(MAPVOTE_COUNT, autocvar_g_maplist_votable);
177 smax = min3(nmax, autocvar_g_maplist_votable_suggestions, mapvote_suggestion_ptr);
179 // we need this for AddVotable, as that cycles through the screenshot dirs
180 mapvote_screenshot_dirs_count = tokenize_console(autocvar_g_maplist_votable_screenshot_dir);
181 if(mapvote_screenshot_dirs_count == 0)
182 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");
183 mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);
184 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
185 mapvote_screenshot_dirs[i] = strzone(argv(i));
187 if(mapvote_suggestion_ptr)
188 for(i = 0; i < 100 && mapvote_count < smax; ++i)
189 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], true);
191 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
192 MapVote_AddVotable(GetNextMap(), false);
194 if(mapvote_count == 0)
196 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
197 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
198 if(autocvar_g_maplist_shuffle)
200 localcmd("\nmenu_cmd sync\n");
201 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
202 MapVote_AddVotable(GetNextMap(), false);
205 mapvote_count_real = mapvote_count;
207 MapVote_AddVotable("don't care", 0);
209 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
211 mapvote_keeptwotime = time + autocvar_g_maplist_votable_keeptwotime;
212 mapvote_timeout = time + autocvar_g_maplist_votable_timeout;
213 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
214 mapvote_keeptwotime = 0;
215 mapvote_message = "Choose a map and press its key!";
220 void MapVote_SendPicture(float id)
223 WriteByte(MSG_ONE, SVC_TEMPENTITY);
224 WriteByte(MSG_ONE, TE_CSQC_PICTURE);
225 WriteByte(MSG_ONE, id);
226 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);
230 void MapVote_WriteMask()
233 if ( mapvote_count < 24 )
237 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
238 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
241 if(mapvote_count < 8)
242 WriteByte(MSG_ENTITY, mask);
243 else if (mapvote_count < 16)
244 WriteShort(MSG_ENTITY,mask);
246 WriteLong(MSG_ENTITY, mask);
250 for ( i = 0; i < mapvote_count; ++i )
251 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
255 float MapVote_SendEntity(entity to, float sf)
260 sf &= ~2; // if we send 1, we don't need to also send 2
262 WriteByte(MSG_ENTITY, ENT_CLIENT_MAPVOTE);
263 WriteByte(MSG_ENTITY, sf);
267 // flag 1 == initialization
268 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)
269 WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);
270 WriteString(MSG_ENTITY, "");
271 WriteByte(MSG_ENTITY, mapvote_count);
272 WriteByte(MSG_ENTITY, mapvote_abstain);
273 WriteByte(MSG_ENTITY, mapvote_detail);
274 WriteCoord(MSG_ENTITY, mapvote_timeout);
279 WriteByte(MSG_ENTITY, 1);
280 WriteString(MSG_ENTITY, autocvar_nextmap);
282 else if ( autocvar_sv_vote_gametype )
284 // map vote but gametype has been chosen via voting screen
285 WriteByte(MSG_ENTITY, 2);
286 WriteString(MSG_ENTITY, MapInfo_Type_ToText(MapInfo_CurrentGametype()));
289 WriteByte(MSG_ENTITY, 0); // map vote
293 for(i = 0; i < mapvote_count; ++i)
295 if(mapvote_abstain && i == mapvote_count - 1)
297 WriteString(MSG_ENTITY, ""); // abstain needs no text
298 WriteString(MSG_ENTITY, ""); // abstain needs no pack
299 WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir
300 WriteByte(MSG_ENTITY, GTV_AVAILABLE);
304 WriteString(MSG_ENTITY, mapvote_maps[i]);
305 WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);
306 WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);
307 WriteByte(MSG_ENTITY, mapvote_maps_availability[i]);
314 // flag 2 == update of mask
321 for(i = 0; i < mapvote_count; ++i)
322 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
323 WriteByte(MSG_ENTITY, mapvote_selections[i]);
325 WriteByte(MSG_ENTITY, to.mapvote);
333 Net_LinkEntity(mapvote_ent = spawn(), false, 0, MapVote_SendEntity);
336 void MapVote_TouchMask()
338 mapvote_ent.SendFlags |= 2;
341 void MapVote_TouchVotes(entity voter)
343 mapvote_ent.SendFlags |= 4;
346 float MapVote_Finished(float mappos)
348 if(alreadychangedlevel)
355 if(autocvar_sv_eventlog)
357 result = strcat(":vote:finished:", mapvote_maps[mappos]);
358 result = strcat(result, ":", ftos(mapvote_selections[mappos]), "::");
359 didntvote = mapvote_voters;
360 for(i = 0; i < mapvote_count; ++i)
361 if(mapvote_maps_availability[i] == GTV_AVAILABLE )
363 didntvote -= mapvote_selections[i];
366 result = strcat(result, ":", mapvote_maps[i]);
367 result = strcat(result, ":", ftos(mapvote_selections[i]));
370 result = strcat(result, ":didn't vote:", ftos(didntvote));
373 if(mapvote_maps_suggested[mappos])
374 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
377 FOR_EACH_REALCLIENT(other)
378 FixClientCvars(other);
382 if ( GameTypeVote_Finished(mappos) )
384 gametypevote = false;
385 if(autocvar_nextmap != "")
387 Map_Goto_SetStr(autocvar_nextmap);
389 alreadychangedlevel = true;
398 Map_Goto_SetStr(mapvote_maps[mappos]);
400 alreadychangedlevel = true;
405 void MapVote_CheckRules_1()
409 for(i = 0; i < mapvote_count; ++i)
410 if( mapvote_maps_availability[i] == GTV_AVAILABLE )
412 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
413 mapvote_selections[i] = 0;
417 FOR_EACH_REALCLIENT(other)
422 i = other.mapvote - 1;
423 //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
424 mapvote_selections[i] = mapvote_selections[i] + 1;
429 float MapVote_CheckRules_2()
432 float firstPlace, secondPlace, currentPlace;
433 float firstPlaceVotes, secondPlaceVotes, currentVotes;
434 float mapvote_voters_real;
437 if(mapvote_count_real == 1)
438 return MapVote_Finished(0);
440 mapvote_voters_real = mapvote_voters;
442 mapvote_voters_real -= mapvote_selections[mapvote_count - 1];
444 RandomSelection_Init();
447 for(i = 0; i < mapvote_count_real; ++i)
448 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
450 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
451 if ( gametypevote && mapvote_maps[i] == MapInfo_Type_ToString(MapInfo_CurrentGametype()) )
453 currentVotes = mapvote_selections[i];
457 firstPlaceVotes = RandomSelection_best_priority;
458 if ( autocvar_sv_vote_gametype_default_current && currentVotes == firstPlaceVotes )
459 firstPlace = currentPlace;
461 firstPlace = RandomSelection_chosen_float;
463 //dprint("First place: ", ftos(firstPlace), "\n");
464 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
466 RandomSelection_Init();
467 for(i = 0; i < mapvote_count_real; ++i)
469 if ( mapvote_maps_availability[i] == GTV_AVAILABLE )
470 RandomSelection_Add(world, i, string_null, 1, mapvote_selections[i]);
471 secondPlace = RandomSelection_chosen_float;
472 secondPlaceVotes = RandomSelection_best_priority;
473 //dprint("Second place: ", ftos(secondPlace), "\n");
474 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
477 error("No first place in map vote... WTF?");
479 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
480 return MapVote_Finished(firstPlace);
482 if(mapvote_keeptwotime)
483 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
487 mapvote_message = "Now decide between the TOP TWO!";
488 mapvote_keeptwotime = 0;
489 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
490 result = strcat(result, ":", ftos(firstPlaceVotes));
491 result = strcat(result, ":", mapvote_maps[secondPlace]);
492 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
493 didntvote = mapvote_voters;
494 for(i = 0; i < mapvote_count; ++i)
496 didntvote -= mapvote_selections[i];
500 result = strcat(result, ":", mapvote_maps[i]);
501 result = strcat(result, ":", ftos(mapvote_selections[i]));
502 if(i < mapvote_count_real)
504 mapvote_maps_availability[i] = GTV_FORBIDDEN;
508 result = strcat(result, ":didn't vote:", ftos(didntvote));
509 if(autocvar_sv_eventlog)
521 keeptwo = mapvote_keeptwotime;
522 MapVote_CheckRules_1(); // count
523 if(MapVote_CheckRules_2()) // decide
527 FOR_EACH_REALCLIENT(other)
529 // hide scoreboard again
530 if(other.health != 2342)
534 if(IS_REAL_CLIENT(other))
537 WriteByte(MSG_ONE, SVC_FINALE);
538 WriteString(MSG_ONE, "");
542 // clear possibly invalid votes
543 if ( mapvote_maps_availability[other.mapvote-1] != GTV_AVAILABLE )
545 // use impulses as new vote
546 if(other.impulse >= 1 && other.impulse <= mapvote_count)
547 if( mapvote_maps_availability[other.impulse - 1] == GTV_AVAILABLE )
549 other.mapvote = other.impulse;
550 MapVote_TouchVotes(other);
558 MapVote_CheckRules_1(); // just count
563 // if mapvote is already running, don't do this initialization again
564 if(mapvote_run) { return; }
566 // don't start mapvote until after playerstats gamereport is sent
567 if(PlayerStats_GameReport_DelayMapVote) { return; }
570 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
579 if(alreadychangedlevel)
582 if(time < mapvote_nextthink)
586 mapvote_nextthink = time + 0.5;
588 if(!mapvote_initialized)
590 if(autocvar_rescan_pending == 1)
592 cvar_set("rescan_pending", "2");
593 localcmd("fs_rescan\nrescan_pending 3\n");
596 else if(autocvar_rescan_pending == 2)
600 else if(autocvar_rescan_pending == 3)
602 // now build missing mapinfo files
603 if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))
606 // we're done, start the timer
607 cvar_set("rescan_pending", "0");
610 mapvote_initialized = true;
611 if(DoNextMapOverride(0))
613 if(!autocvar_g_maplist_votable || player_count <= 0)
619 if(autocvar_sv_vote_gametype) { GameTypeVote_Start(); }
620 else if(autocvar_nextmap == "") { MapVote_Init(); }
626 float GameTypeVote_SetGametype(float type)
628 if (MapInfo_CurrentGametype() == type)
631 float tsave = MapInfo_CurrentGametype();
633 MapInfo_SwitchGameType(type);
636 MapInfo_FilterGametype(type, MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
637 if(MapInfo_count > 0)
639 // update lsmaps in case the gametype changed, this way people can easily list maps for it
640 if(lsmaps_reply != "") { strunzone(lsmaps_reply); }
641 lsmaps_reply = strzone(getlsmaps());
642 bprint("Game type successfully switched to ", MapInfo_Type_ToString(type), "\n");
646 bprint("Cannot use this game type: no map for it found\n");
647 MapInfo_SwitchGameType(tsave);
648 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
652 //localcmd("gametype ", MapInfo_Type_ToString(type), "\n");
654 cvar_set("g_maplist", MapInfo_ListAllowedMaps(type, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()) );
655 if(autocvar_g_maplist_shuffle)
661 float gametypevote_finished;
662 float GameTypeVote_Finished(float pos)
664 if(!gametypevote || gametypevote_finished)
667 if ( !GameTypeVote_SetGametype(MapInfo_Type_FromString(mapvote_maps[pos])) )
669 dprint("Selected gametype is not supported by any map");
672 localcmd("sv_vote_gametype_hook_all\n");
673 localcmd("sv_vote_gametype_hook_", mapvote_maps[pos], "\n");
675 gametypevote_finished = true;
680 float GameTypeVote_AddVotable(string nextMode)
683 if ( nextMode == "" || MapInfo_Type_FromString(nextMode) == 0 )
685 for(j = 0; j < mapvote_count; ++j)
686 if(mapvote_maps[j] == nextMode)
689 mapvote_maps[mapvote_count] = strzone(nextMode);
690 mapvote_maps_suggested[mapvote_count] = false;
692 mapvote_maps_screenshot_dir[mapvote_count] = 0;
693 mapvote_maps_pakfile[mapvote_count] = strzone("");
694 mapvote_maps_availability[mapvote_count] = GameTypeVote_AvailabilityStatus(nextMode);
702 float GameTypeVote_Start()
705 MapVote_ClearAllVotes();
706 MapVote_UnzoneStrings();
709 mapvote_timeout = time + autocvar_sv_vote_gametype_timeout;
711 mapvote_detail = !autocvar_g_maplist_votable_nodetail;
713 float n = tokenizebyseparator(autocvar_sv_vote_gametype_options, " ");
714 n = min(MAPVOTE_COUNT, n);
716 float really_available, which_available;
717 really_available = 0;
718 which_available = -1;
719 for(j = 0; j < n; ++j)
721 if ( GameTypeVote_AddVotable(argv(j)) )
722 if ( mapvote_maps_availability[j] == GTV_AVAILABLE )
729 mapvote_count_real = mapvote_count;
733 if ( really_available == 0 )
735 if ( mapvote_count > 0 )
736 strunzone(mapvote_maps[0]);
737 mapvote_maps[0] = strzone(MapInfo_Type_ToString(MapInfo_CurrentGametype()));
738 //GameTypeVote_Finished(0);
742 if ( really_available == 1 )
744 //GameTypeVote_Finished(which_available);
745 MapVote_Finished(which_available);
749 mapvote_count_real = mapvote_count;
751 mapvote_keeptwotime = time + autocvar_sv_vote_gametype_keeptwotime;
752 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
753 mapvote_keeptwotime = 0;