1 float SPAWNFLAG_NO_WAYPOINTS_FOR_ITEMS = 1;
2 string redirection_target;
3 float world_initialized;
9 float() DoNextMapOverride;
11 void SetDefaultAlpha()
13 if(cvar("g_running_guns"))
15 default_player_alpha = -1;
16 default_weapon_alpha = +1;
20 default_player_alpha = cvar("g_balance_cloaked_alpha");
21 default_weapon_alpha = default_player_alpha;
25 default_player_alpha = cvar("g_player_alpha");
26 if(default_player_alpha <= 0)
27 default_player_alpha = 1;
28 default_weapon_alpha = default_player_alpha;
32 void fteqcc_testbugs()
36 if(!cvar("developer_fteqccbugs"))
39 dprint("*** fteqcc test: checking for bugs...\n");
43 if(sqrt(a) - sqrt(b - a) == 0)
44 dprint("*** fteqcc test: found same-function-twice bug\n");
46 dprint("*** fteqcc test: same-function-twice bug got FINALLY FIXED! HOORAY!\n");
50 world.enemy.cnt += 10;
51 if(world.cnt > 0.2 || world.cnt < -0.2) // don't error out if it's just roundoff errors
52 dprint("*** fteqcc test: found += bug\n");
54 dprint("*** fteqcc test: += bug got FINALLY FIXED! HOORAY!\n");
59 * Takes care of pausing and unpausing the game.
60 * Centerprints the information about an upcoming or active timeout to all active
61 * players. Also plays reminder sounds.
63 void timeoutHandler_Think() {
66 if (timeoutStatus == 1) {
67 if (remainingLeadTime > 0) {
68 //centerprint the information to every player
69 timeStr = getTimeoutText(0);
70 FOR_EACH_REALCLIENT(plr) {
71 if(plr.classname == "player") {
72 centerprint_atprio(plr, CENTERPRIO_SPAM, timeStr);
75 remainingLeadTime -= 1;
76 //think again in 1 second:
77 self.nextthink = time + 1;
82 cvar_set("slowmo", ftos(TIMEOUT_SLOWMO_VALUE));
83 //copy .v_angle to .lastV_angle for every player in order to fix their view during pause (see PlayerPreThink)
84 FOR_EACH_REALPLAYER(plr) {
85 plr.lastV_angle = plr.v_angle;
87 self.nextthink = time;
90 else if (timeoutStatus == 2) {
91 if (remainingTimeoutTime > 0) {
92 timeStr = getTimeoutText(0);
93 FOR_EACH_REALCLIENT(plr) {
94 if(plr.classname == "player") {
95 centerprint_atprio(plr, CENTERPRIO_SPAM, timeStr);
98 if(remainingTimeoutTime == cvar("sv_timeout_resumetime")) { //play a warning sound when only <sv_timeout_resumetime> seconds are left
99 play2all("announcer/robotic/prepareforbattle.wav");
101 remainingTimeoutTime -= 1;
102 self.nextthink = time + TIMEOUT_SLOWMO_VALUE;
105 //unpause the game again
106 remainingTimeoutTime = timeoutStatus = 0;
107 cvar_set("slowmo", ftos(orig_slowmo));
108 //and unlock the fixed view again once there is no timeout active anymore
109 FOR_EACH_REALPLAYER(plr) {
110 plr.fixangle = FALSE;
112 //get rid of the countdown message
113 FOR_EACH_REALCLIENT(plr) {
114 if(plr.classname == "player") {
115 centerprint_atprio(plr, CENTERPRIO_SPAM, "");
123 else if (timeoutStatus == 0) { //if a player called the resumegame command (which set timeoutStatus to 0 already)
124 FOR_EACH_REALCLIENT(plr) {
125 if(plr.classname == "player") {
126 centerprint_atprio(plr, CENTERPRIO_SPAM, "");
138 cvar_set("_sv_init", "0");
139 if(cvar("g_maplist_shuffle"))
141 tokenizebyseparator(cvar_string("g_maplist"), " ");
143 cvar_set("nextmap", argv(0));
146 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0, (g_maplist_allow_hidden ? MAPINFO_FLAG_HIDDEN : 0), 0);
148 if(!DoNextMapOverride())
157 void cvar_changes_init()
164 strunzone(cvar_changes);
165 cvar_changes = string_null;
166 fh = fopen("cvars.txt", FILE_READ);
169 while((s = fgets(fh)))
171 n = tokenize_sane(s);
178 d = cvar_defstring(k);
181 cvar_changes = strcat(cvar_changes, k, " \"", v, "\" // \"", d, "\"\n");
182 if(strlen(cvar_changes) > 16384)
184 cvar_changes = "// too many settings have been changed to show them here\n";
190 if(cvar_changes == "")
191 cvar_changes = "// this server runs at default settings\n";
193 cvar_changes = strcat("// this server runs at modified settings:\n", cvar_changes);
194 cvar_changes = strzone(cvar_changes);
197 float world_already_spawned;
198 void RegisterWeapons();
200 void spawnfunc_worldspawn (void)
205 dprint_load(); // load dprint status from cvar
207 if(world_already_spawned)
208 error("world already spawned - you may have EXACTLY ONE worldspawn!");
209 world_already_spawned = TRUE;
211 remove = remove_safely; // during spawning, watch what you remove!
213 compressShortVector_init();
216 head = nextent(world);
221 head = nextent(head);
224 // needs to be done so early as they would still spawn
229 world_initialized = -1; // don't complain
234 ServerProgsDB = db_create();
236 ServerProgsDB = db_load("server.db");
238 TemporaryDB = db_create();
241 TODO sound pack system
242 // initialize sound pack system
243 soundpack = cvar_string("g_soundpack");
245 soundpack = strcat(soundpack, "/");
246 soundpack = strzone(soundpack);
252 // 1 FLICKER (first variety)
253 lightstyle(1, "mmnmmommommnonmmonqnmmo");
255 // 2 SLOW STRONG PULSE
256 lightstyle(2, "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba");
258 // 3 CANDLE (first variety)
259 lightstyle(3, "mmmmmaaaaammmmmaaaaaabcdefgabcdefg");
262 lightstyle(4, "mamamamamama");
265 lightstyle(5,"jklmnopqrstuvwxyzyxwvutsrqponmlkj");
267 // 6 FLICKER (second variety)
268 lightstyle(6, "nmonqnmomnmomomno");
270 // 7 CANDLE (second variety)
271 lightstyle(7, "mmmaaaabcdefgmmmmaaaammmaamm");
273 // 8 CANDLE (third variety)
274 lightstyle(8, "mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa");
276 // 9 SLOW STROBE (fourth variety)
277 lightstyle(9, "aaaaaaaazzzzzzzz");
279 // 10 FLUORESCENT FLICKER
280 lightstyle(10, "mmamammmmammamamaaamammma");
282 // 11 SLOW PULSE NOT FADE TO BLACK
283 lightstyle(11, "abcdefghijklmnopqrrqponmlkjihgfedcba");
285 // styles 32-62 are assigned by the spawnfunc_light program for switchable lights
290 if(cvar("g_campaign"))
298 bot_waypoints_for_items = cvar("g_waypoints_for_items");
299 if(bot_waypoints_for_items == 1)
300 if(self.spawnflags & SPAWNFLAG_NO_WAYPOINTS_FOR_ITEMS)
301 bot_waypoints_for_items = 0;
303 // for setting by mapinfo
304 q3acompat_machineshotgunswap = cvar("sv_q3acompat_machineshotgunswap");
305 cvar_set("sv_q3acompat_machineshotgunswap", "0");
309 WaypointSprite_Init();
314 GameLogInit(); // prepare everything
315 if(cvar("sv_eventlog"))
317 s = strcat(cvar_string("sv_eventlog_files_counter"), ".");
318 s = strcat(s, ftos(random()));
319 GameLogEcho(strcat(":gamestart:", GetGametype(), "_", GetMapname(), ":", s));
320 s = ":gameinfo:mutators:LIST";
321 if(cvar("g_grappling_hook"))
322 s = strcat(s, ":grappling_hook");
323 if(!cvar("g_use_ammunition"))
324 s = strcat(s, ":no_use_ammunition");
325 if(!cvar("g_pickup_items"))
326 s = strcat(s, ":no_pickup_items");
327 if(cvar("g_instagib"))
328 s = strcat(s, ":instagib");
329 if(cvar_string("g_weaponarena") != "0")
330 s = strcat(s, ":", cvar_string("g_weaponarena"), " arena");
332 s = strcat(s, ":nixnex");
333 if(cvar("g_vampire"))
334 s = strcat(s, ":vampire");
335 if(cvar("g_laserguided_missile"))
336 s = strcat(s, ":laserguided_missile");
337 if(cvar("g_norecoil"))
338 s = strcat(s, ":norecoil");
340 s = strcat(s, ":midair");
341 if(cvar("g_minstagib"))
342 s = strcat(s, ":minstagib");
344 GameLogEcho(":gameinfo:end");
347 cvar_set("nextmap", "");
351 if(cvar("g_campaign"))
358 //initialise globals related to sv_timeout
359 sys_ticrate = cvar("sys_ticrate");
360 orig_slowmo = cvar("slowmo");
363 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0, (g_maplist_allow_hidden ? MAPINFO_FLAG_HIDDEN : 0), 1);
365 if(whichpack(strcat("maps/", mapname, ".cfg")) != "")
367 fd = fopen(strcat("maps/", mapname, ".cfg"), FILE_READ);
370 while((s = fgets(fd)))
372 l = tokenize_sane(s);
377 print("Found ^1DEPRECATED^7 cd loop command in .cfg file; put this line in mapinfo instead:\n");
378 print(" cdtrack ", argv(2), "\n");
380 else if(argv(0) == "fog")
382 print("Found ^1DEPRECATED^7 fog command in .cfg file; put this line in worldspawn in the .map/.bsp/.ent file instead:\n");
383 print(" \"fog\" \"", s, "\"\n");
385 else if(argv(0) == "set")
387 print("Found ^1DEPRECATED^7 set command in .cfg file; put this line in mapinfo instead:\n");
388 print(" clientsettemp_for_type all ", argv(1), " ", argv(2), "\n");
390 else if(argv(0) != "//")
392 print("Found ^1DEPRECATED^7 set command in .cfg file; put this line in mapinfo instead:\n");
393 print(" clientsettemp_for_type all ", argv(0), " ", argv(1), "\n");
400 addstat(STAT_SYS_TICRATE, AS_FLOAT, stat_sys_ticrate);
401 addstat(STAT_WEAPONS, AS_INT, weapons);
402 addstat(STAT_SWITCHWEAPON, AS_INT, switchweapon);
403 addstat(STAT_GAMESTARTTIME, AS_FLOAT, stat_game_starttime);
406 next_pingtime = time + 5;
407 InitializeEntity(self, cvar_changes_init, INITPRIO_CVARS);
409 world_initialized = 1;
412 void spawnfunc_light (void)
414 //makestatic (self); // Who the f___ did that?
418 float TryFile( string pFilename )
421 dprint("TryFile(\"", pFilename, "\")\n");
422 lHandle = fopen( pFilename, FILE_READ );
423 if( lHandle != -1 ) {
433 return GametypeNameFromType(game);
436 string getmapname_stored;
442 float Map_Count, Map_Current;
443 string Map_Current_Name;
445 // NOTE: this now expects the map list to be already tokenize()d and the count in Map_Count
446 float GetMaplistPosition()
452 idx = cvar("g_maplist_index");
459 for(pos = 0; pos < Map_Count; ++pos)
463 // resume normal maplist rotation if current map is not in g_maplist
467 float MapHasRightSize(string map)
470 if(currentbots || cvar("bot_number") || player_count < cvar("minplayers"))
471 if(cvar("g_maplist_check_waypoints"))
473 dprint("checkwp "); dprint(map);
474 fh = fopen(strcat("maps/", map, ".waypoints"), FILE_READ);
477 dprint(": no waypoints\n");
480 dprint(": has waypoints\n");
484 // open map size restriction file
485 dprint("opensize "); dprint(map);
486 fh = fopen(strcat("maps/", map, ".sizes"), FILE_READ);
489 float mapmin, mapmax;
491 mapmin = stof(fgets(fh));
492 mapmax = stof(fgets(fh));
494 if(player_count < mapmin)
496 dprint("not enough\n");
499 if(player_count > mapmax)
501 dprint("too many\n");
504 dprint("right size\n");
507 dprint(": not found\n");
511 string Map_Filename(float position)
513 return strcat("maps/", argv(position), ".bsp");
516 string strwords(string s, float w)
519 for(endpos = 0; w && endpos >= 0; --w)
520 endpos = strstrofs(s, " ", endpos + 1);
524 return substring(s, 0, endpos);
527 float strhasword(string s, string w)
529 return strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0;
532 void Map_MarkAsRecent(string m)
534 cvar_set("g_maplist_mostrecent", strwords(strcat(m, " ", cvar_string("g_maplist_mostrecent")), cvar("g_maplist_mostrecent_count")));
537 float Map_IsRecent(string m)
539 return strhasword(cvar_string("g_maplist_mostrecent"), m);
542 float Map_Check(float position, float pass)
546 map_next = argv(position);
549 if(map_next == Map_Current_Name) // same map again in first pass?
551 if(Map_IsRecent(map_next))
554 filename = Map_Filename(position);
555 if(MapInfo_CheckMap(map_next))
559 if(MapHasRightSize(map_next))
564 dprint( "Couldn't select '", filename, "'..\n" );
569 void Map_Goto_SetStr(string nextmapname)
571 if(getmapname_stored != "")
572 strunzone(getmapname_stored);
573 if(nextmapname == "")
574 getmapname_stored = "";
576 getmapname_stored = strzone(nextmapname);
579 void Map_Goto_SetFloat(float position)
581 cvar_set("g_maplist_index", ftos(position));
582 Map_Goto_SetStr(argv(position));
587 // settings persist, except...
588 if(cvar("g_campaign"))
589 localcmd("\nexec mutator_reset.cfg\n");
590 localcmd("\nsettemp_restore\n");
595 Map_MarkAsRecent(getmapname_stored);
597 MapInfo_LoadMap(getmapname_stored);
600 // return codes of map selectors:
601 // -1 = temporary failure (that is, try some method that is guaranteed to succeed)
602 // -2 = permanent failure
603 float() MaplistMethod_Iterate = // usual method
607 for(pass = 1; pass <= 2; ++pass)
609 for(i = 1; i < Map_Count; ++i)
612 mapindex = mod(i + Map_Current, Map_Count);
613 if(Map_Check(mapindex, pass))
620 float() MaplistMethod_Repeat = // fallback method
622 if(Map_Check(Map_Current, 2))
627 float() MaplistMethod_Random = // random map selection
633 for(i = 0; i <= imax; ++i)
636 mapindex = mod(Map_Current + floor(random() * (Map_Count - 1) + 1), Map_Count); // any OTHER map
637 if(Map_Check(mapindex, 1))
643 float(float exponent) MaplistMethod_Shuffle = // more clever shuffling
644 // the exponent sets a bias on the map selection:
645 // the higher the exponent, the less likely "shortly repeated" same maps are
647 float i, j, imax, insertpos;
651 for(i = 0; i <= imax; ++i)
655 // now reinsert this at another position
656 insertpos = pow(random(), 1 / exponent); // ]0, 1]
657 insertpos = insertpos * (Map_Count - 1); // ]0, Map_Count - 1]
658 insertpos = ceil(insertpos) + 1; // {2, 3, 4, ..., Map_Count}
659 dprint("SHUFFLE: insert pos = ", ftos(insertpos), "\n");
661 // insert the current map there
663 for(j = 1; j < insertpos; ++j) // i == 1: no loop, will be inserted as first; however, i == 1 has been excluded above
664 newlist = strcat(newlist, " ", argv(j));
665 newlist = strcat(newlist, " ", argv(0)); // now insert the just selected map
666 for(j = insertpos; j < Map_Count; ++j) // i == Map_Count: no loop, has just been inserted as last
667 newlist = strcat(newlist, " ", argv(j));
668 newlist = substring(newlist, 1, strlen(newlist) - 1);
669 cvar_set("g_maplist", newlist);
670 Map_Count = tokenizebyseparator(cvar_string("g_maplist"), " ");
672 // NOTE: the selected map has just been inserted at (insertpos-1)th position
673 Map_Current = insertpos - 1; // this is not really valid, but this way the fallback has a chance of working
674 if(Map_Check(Map_Current, 1))
683 temp = cvar_string("g_maplist");
684 Map_Count = tokenizebyseparator(temp, " ");
687 bprint( "Maplist is empty! Resetting it to default map list.\n" );
688 cvar_set("g_maplist", temp = MapInfo_ListAllowedMaps(0, MAPINFO_FLAG_HIDDEN));
689 localcmd("\nmenu_cmd sync\n");
690 Map_Count = tokenizebyseparator(temp, " ");
693 error("empty maplist, cannot select a new map");
694 Map_Current = bound(0, GetMaplistPosition(), Map_Count - 1);
697 strunzone(Map_Current_Name);
698 Map_Current_Name = strzone(argv(Map_Current)); // will be automatically freed on exit thanks to DP
699 // this may or may not be correct, but who cares, in the worst case a map
700 // isn't chosen in the first pass that should have been
711 if(cvar("g_maplist_shuffle") > 0)
712 nextMap = MaplistMethod_Shuffle(cvar("g_maplist_shuffle") + 1);
715 if(cvar("g_maplist_selectrandom"))
716 nextMap = MaplistMethod_Random();
719 nextMap = MaplistMethod_Iterate();
722 nextMap = MaplistMethod_Repeat();
726 Map_Goto_SetFloat(nextMap);
727 return getmapname_stored;
733 float DoNextMapOverride()
735 if(cvar("g_campaign"))
737 CampaignPostIntermission();
738 alreadychangedlevel = TRUE;
741 if(cvar("quit_when_empty"))
743 if(player_count <= currentbots)
746 alreadychangedlevel = TRUE;
750 if(cvar_string("quit_and_redirect") != "")
752 redirection_target = strzone(cvar_string("quit_and_redirect"));
753 alreadychangedlevel = TRUE;
756 if (cvar("samelevel")) // if samelevel is set, stay on same level
758 // this does not work because it tries to exec maps/nexdm01.mapcfg (which doesn't exist, it should be trying maps/dm_nexdm01.mapcfg for example)
759 //localcmd(strcat("exec \"maps/", mapname, ".mapcfg\"\n"));
760 // so instead just restart the current map using the restart command (DOES NOT WORK PROPERLY WITH exit_cfg STUFF)
761 localcmd("restart\n");
762 //changelevel (mapname);
763 alreadychangedlevel = TRUE;
766 if(cvar_string("nextmap") != "")
767 if(MapInfo_CheckMap(cvar_string("nextmap")))
769 Map_Goto_SetStr(cvar_string("nextmap"));
771 alreadychangedlevel = TRUE;
774 if(cvar("lastlevel"))
777 localcmd("set lastlevel 0\ntogglemenu\n");
778 alreadychangedlevel = TRUE;
786 //local string nextmap;
787 //local float n, nummaps;
789 if (alreadychangedlevel)
791 alreadychangedlevel = TRUE;
797 for(allowReset = 1; allowReset >= 0; --allowReset)
799 nextMap = GetNextMap();
805 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
806 cvar_set("g_maplist", MapInfo_ListAllowedMaps(0, MAPINFO_FLAG_HIDDEN));
807 localcmd("\nmenu_cmd sync\n");
811 error("Everything is broken - not even the default map list works. Please report this to the developers.");
823 When the player presses attack or jump, change to the next level
826 .float autoscreenshot;
827 void() MapVote_Start;
828 void() MapVote_Think;
829 float mapvote_initialized;
830 void IntermissionThink()
832 FixIntermissionClient(self);
834 if(cvar("sv_autoscreenshot"))
835 if(self.autoscreenshot > 0)
836 if(time > self.autoscreenshot)
838 self.autoscreenshot = -1;
839 if(clienttype(self) == CLIENTTYPE_REAL)
840 stuffcmd(self, "\nscreenshot\necho \"^5A screenshot has been taken at request of the server.\"\n");
844 if (time < intermission_exittime)
847 if(!mapvote_initialized)
848 if (time < intermission_exittime + 10 && !self.BUTTON_ATCK && !self.BUTTON_JUMP && !self.BUTTON_ATCK2 && !self.BUTTON_HOOK && !self.BUTTON_USE)
858 Returns the entity to view from
862 entity FindIntermission()
867 // look for info_intermission first
868 spot = find (world, classname, "info_intermission");
870 { // pick a random one
874 spot = find (spot, classname, "info_intermission");
876 spot = find (spot, classname, "info_intermission");
882 // then look for the start position
883 spot = find (world, classname, "info_player_start");
887 // testinfo_player_start is only found in regioned levels
888 spot = find (world, classname, "testplayerstart");
892 // then look for the start position
893 spot = find (world, classname, "info_player_deathmatch");
897 //objerror ("FindIntermission: no spot");
903 ===============================================================================
907 ===============================================================================
910 void DumpStats(float final)
914 local float to_console;
915 local float to_eventlog;
919 to_console = cvar("sv_logscores_console");
920 to_eventlog = cvar("sv_eventlog");
921 to_file = cvar("sv_logscores_file");
925 to_console = TRUE; // always print printstats replies
926 to_eventlog = FALSE; // but never print them to the event log
930 if(cvar("sv_eventlog_console"))
931 to_console = FALSE; // otherwise we get the output twice
937 s = strcat(s, GetGametype(), "_", GetMapname(), ":", ftos(rint(time)));
945 file = fopen(cvar_string("sv_logscores_filename"), FILE_APPEND);
949 fputs(file, strcat(s, "\n"));
952 s = strcat(":labels:player:", GetPlayerScoreString(world, 0));
958 fputs(file, strcat(s, "\n"));
960 FOR_EACH_CLIENT(other)
962 if ((clienttype(other) == CLIENTTYPE_REAL) || (clienttype(other) == CLIENTTYPE_BOT && cvar("sv_logscores_bots")))
964 s = strcat(":player:see-labels:", GetPlayerScoreString(other, 0), ":");
965 s = strcat(s, ftos(rint(time - other.jointime)), ":");
966 if(other.classname == "player" || g_arena || g_lms)
967 s = strcat(s, ftos(other.team), ":");
969 s = strcat(s, "spectator:");
972 print(s, other.netname, "\n");
974 GameLogEcho(strcat(s, ftos(other.playerid), ":", other.netname));
976 fputs(file, strcat(s, other.netname, "\n"));
982 s = strcat(":labels:teamscores:", GetTeamScoreString(0, 0));
988 fputs(file, strcat(s, "\n"));
990 for(i = 1; i < 16; ++i)
992 s = strcat(":teamscores:see-labels:", GetTeamScoreString(i, 0));
993 s = strcat(s, ":", ftos(i));
999 fputs(file, strcat(s, "\n"));
1006 GameLogEcho(":end");
1009 fputs(file, ":end\n");
1014 void FixIntermissionClient(entity e)
1017 if(!e.autoscreenshot) // initial call
1019 e.angles = e.v_angle;
1020 e.angles_x = -e.angles_x;
1021 e.autoscreenshot = time + 0.8; // used for autoscreenshot
1023 // first intermission phase; voting phase has positive health (used to decide whether to send SVC_FINALE or not)
1024 e.solid = SOLID_NOT;
1025 e.movetype = MOVETYPE_NONE;
1026 e.takedamage = DAMAGE_NO;
1028 e.weaponentity.effects = EF_NODRAW;
1029 if(clienttype(e) == CLIENTTYPE_REAL)
1031 stuffcmd(e, "\nscr_printspeed 1000000\n");
1032 s = cvar_string("sv_intermission_cdtrack");
1034 stuffcmd(e, strcat("\ncd loop ", s, "\n"));
1036 WriteByte(MSG_ONE, SVC_INTERMISSION);
1040 //e.velocity = '0 0 0';
1041 //e.fixangle = TRUE;
1043 // TODO halt weapon animation
1048 go to the next level for deathmatch
1049 only called if a time or frag limit has expired
1053 float minTotalFrags;
1054 float maxTotalFrags;
1060 intermission_running = 1;
1062 // enforce a wait time before allowing changelevel
1063 if(player_count > 0)
1064 intermission_exittime = time + cvar("sv_mapchange_delay");
1066 intermission_exittime = -1;
1069 WriteByte (MSG_ALL, SVC_CDTRACK);
1070 WriteByte (MSG_ALL, 3);
1071 WriteByte (MSG_ALL, 3);
1072 // done in FixIntermission
1075 //pos = FindIntermission ();
1081 if(cvar("sv_eventlog"))
1082 GameLogEcho(":gameover");
1086 FOR_EACH_CLIENT(other)
1088 FixIntermissionClient(other);
1091 bprint(other.netname, " ^7wins.\n");
1096 FOR_EACH_PLAYER(other)
1098 if(maxTotalFrags < other.totalfrags)
1099 maxTotalFrags = other.totalfrags;
1100 if(minTotalFrags > other.totalfrags)
1101 minTotalFrags = other.totalfrags;
1106 FOR_EACH_PLAYER(other)
1108 score = (other.totalfrags - minTotalFrags) / max(maxTotalFrags - minTotalFrags, 1);
1109 f = bound(0, other.play_time / max(time, 1), 1);
1110 // store some statistics?
1114 if(cvar("g_campaign"))
1115 CampaignPreIntermission();
1117 // WriteByte (MSG_ALL, SVC_INTERMISSION);
1124 Exit deathmatch games upon conditions
1127 void CheckRules_Player()
1129 if (gameover) // someone else quit the game already
1132 if(self.deadflag == DEAD_NO)
1133 self.play_time += frametime;
1135 // fixme: don't check players; instead check spawnfunc_dom_team and spawnfunc_ctf_team entities
1136 // (div0: and that in CheckRules_World please)
1139 float checkrules_oneminutewarning;
1141 float checkrules_equality;
1142 float checkrules_overtimewarning;
1143 float checkrules_overtimeend;
1145 void InitiateOvertime()
1147 if(!checkrules_overtimeend)
1148 checkrules_overtimeend = time + 60 * cvar("timelimit_maxovertime");
1151 float WINNING_NO = 0; // no winner, but time limits may terminate the game
1152 float WINNING_YES = 1; // winner found
1153 float WINNING_NEVER = 2; // no winner, enter overtime if time limit is reached
1154 float WINNING_STARTOVERTIME = 3; // no winner, enter overtime NOW
1156 float GetWinningCode(float fraglimitreached, float equality)
1159 if(fraglimitreached)
1160 return WINNING_STARTOVERTIME;
1162 return WINNING_NEVER;
1164 if(fraglimitreached)
1170 // set the .winning flag for exactly those players with a given field value
1171 void SetWinners(.float field, float value)
1174 FOR_EACH_PLAYER(head)
1175 head.winning = (head.field == value);
1178 // set the .winning flag for those players with a given field value
1179 void AddWinners(.float field, float value)
1182 FOR_EACH_PLAYER(head)
1183 if(head.field == value)
1187 // clear the .winning flags
1188 void ClearWinners(void)
1191 FOR_EACH_PLAYER(head)
1195 // Onslaught winning condition:
1196 // game terminates if only one team has a working generator (or none)
1197 float WinningCondition_Onslaught()
1200 local float t1, t2, t3, t4;
1201 // first check if the game has ended
1202 t1 = t2 = t3 = t4 = 0;
1203 head = find(world, classname, "onslaught_generator");
1206 if (head.health > 0)
1208 if (head.team == COLOR_TEAM1) t1 = 1;
1209 if (head.team == COLOR_TEAM2) t2 = 1;
1210 if (head.team == COLOR_TEAM3) t3 = 1;
1211 if (head.team == COLOR_TEAM4) t4 = 1;
1213 head = find(head, classname, "onslaught_generator");
1215 if (t1 + t2 + t3 + t4 < 2)
1217 // game over, only one team remains (or none)
1219 if (t1) SetWinners(team, COLOR_TEAM1);
1220 if (t2) SetWinners(team, COLOR_TEAM2);
1221 if (t3) SetWinners(team, COLOR_TEAM3);
1222 if (t4) SetWinners(team, COLOR_TEAM4);
1223 dprint("Have a winner, ending game.\n");
1227 // Two or more teams remain
1231 float LMS_NewPlayerLives()
1234 fl = cvar("fraglimit");
1238 // first player has left the game for dying too much? Nobody else can get in.
1239 if(lms_lowest_lives < 1)
1242 if(!cvar("g_lms_join_anytime"))
1243 if(lms_lowest_lives < fl - cvar("g_lms_last_join"))
1246 return bound(1, lms_lowest_lives, fl);
1249 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)
1250 // they win. Otherwise the defending team wins once the timelimit passes.
1251 void assault_new_round();
1252 float WinningCondition_Assault()
1255 status = WINNING_NO;
1257 // as the timelimit has not yet passed just assume the defending team will win
1258 if(assault_attacker_team == COLOR_TEAM1)
1260 SetWinners(team, COLOR_TEAM2);
1264 SetWinners(team, COLOR_TEAM1);
1268 ent = find(world, classname, "target_assault_roundend");
1271 if(ent.winning) // round end has been triggered by attacking team
1273 SetWinners(team, assault_attacker_team);
1274 if(assault_attacker_team == COLOR_TEAM1)
1276 team1_score = team1_score + 50;
1280 team2_score = team2_score + 50;
1283 if(ent.cnt == 1) // this was the second round
1285 status = WINNING_YES;
1289 local entity oldself;
1292 cvar_set("timelimit", ftos((2*time)/60)); // FIXME use game_starttime here
1293 assault_new_round();
1303 // LMS winning condition: game terminates if and only if there's at most one
1304 // one player who's living lives. Top two scores being equal cancels the time
1306 float WinningCondition_LMS()
1313 have_player = FALSE;
1314 have_players = FALSE;
1315 l = LMS_NewPlayerLives();
1317 head = find(world, classname, "player");
1320 head2 = find(head, classname, "player");
1322 have_players = TRUE;
1326 // we have at least one player
1329 // two or more active players - continue with the game
1333 // exactly one player?
1336 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
1340 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
1346 // and assign him his first place
1347 PlayerScore_Add(head, SP_LMS_RANK, 1);
1354 // nobody is playing at all...
1357 // wait for players...
1361 // SNAFU (maybe a draw game?)
1363 dprint("No players, ending game.\n");
1368 // When we get here, we have at least two players who are actually LIVING,
1369 // now check if the top two players have equal score.
1370 WinningConditionHelper();
1373 if(WinningConditionHelper_winner)
1374 WinningConditionHelper_winner.winning = TRUE;
1375 if(WinningConditionHelper_equality)
1376 return WINNING_NEVER;
1378 // Top two have different scores? Way to go for our beloved TIMELIMIT!
1382 void print_to(entity e, string s)
1385 sprint(e, strcat(s, "\n"));
1390 void ShuffleMaplist()
1398 result = cvar_string("g_maplist");
1399 litems = tokenizebyseparator(result, " ");
1401 for(start = 0; start < litems - 1; ++start)
1405 // select a random item
1406 selected = floor(random() * (litems - start) + start);
1408 // shift this item to the place start
1409 for(i = 0; i < start; ++i)
1410 result = strcat(result, " ", argv(i));
1411 result = strcat(result, " ", argv(selected));
1412 for(i = start; i < litems; ++i)
1414 result = strcat(result, " ", argv(i));
1415 result = substring(result, 1, strlen(result) - 1);
1417 litems = tokenizebyseparator(result, " ");
1419 //dprint(result, "\n");
1422 cvar_set("g_maplist", result);
1425 float WinningCondition_Scores(float limit)
1427 // TODO make everything use THIS winning condition (except LMS)
1428 WinningConditionHelper();
1432 team1_score = TeamScore_GetCompareValue(COLOR_TEAM1);
1433 team2_score = TeamScore_GetCompareValue(COLOR_TEAM2);
1434 team3_score = TeamScore_GetCompareValue(COLOR_TEAM3);
1435 team4_score = TeamScore_GetCompareValue(COLOR_TEAM4);
1439 if(WinningConditionHelper_winner)
1440 WinningConditionHelper_winner.winning = 1;
1441 if(WinningConditionHelper_winnerteam >= 0)
1442 SetWinners(team, WinningConditionHelper_winnerteam);
1444 if(WinningConditionHelper_lowerisbetter)
1446 WinningConditionHelper_topscore = -WinningConditionHelper_topscore;
1450 return GetWinningCode(limit && WinningConditionHelper_topscore && (WinningConditionHelper_topscore >= limit), WinningConditionHelper_equality);
1453 float WinningCondition_Race(float fraglimit)
1457 wc = WinningCondition_Scores(fraglimit);
1459 // ALWAYS initiate overtime, unless EVERYONE has finished the race!
1460 if(wc == WINNING_YES || wc == WINNING_STARTOVERTIME)
1461 // do NOT support equality when the laps are all raced!
1464 if not(p.race_completed)
1465 return WINNING_STARTOVERTIME;
1471 void ReadyRestart();
1472 float WinningCondition_QualifyingThenRace(float limit)
1475 wc = WinningCondition_Scores(limit);
1477 // NEVER initiate overtime
1478 if(wc == WINNING_YES || wc == WINNING_STARTOVERTIME)
1486 float WinningCondition_RanOutOfSpawns()
1490 if(!have_team_spawns)
1493 if(!some_spawn_has_been_used)
1496 team1_score = team2_score = team3_score = team4_score = 0;
1498 FOR_EACH_PLAYER(head) if(head.deadflag == DEAD_NO)
1500 if(head.team == COLOR_TEAM1)
1502 else if(head.team == COLOR_TEAM2)
1504 else if(head.team == COLOR_TEAM3)
1506 else if(head.team == COLOR_TEAM4)
1510 for(head = world; (head = find(head, classname, "info_player_deathmatch")) != world; )
1512 if(head.team == COLOR_TEAM1)
1514 else if(head.team == COLOR_TEAM2)
1516 else if(head.team == COLOR_TEAM3)
1518 else if(head.team == COLOR_TEAM4)
1523 if(team1_score + team2_score + team3_score + team4_score == 0)
1525 checkrules_equality = TRUE;
1528 else if(team1_score + team2_score + team3_score + team4_score == 1)
1531 if(team1_score) t = COLOR_TEAM1;
1532 if(team2_score) t = COLOR_TEAM2;
1533 if(team3_score) t = COLOR_TEAM3;
1534 if(team4_score) t = COLOR_TEAM4;
1535 CheckAllowedTeams(world);
1536 for(i = 0; i < MAX_TEAMSCORE; ++i)
1538 if(t != COLOR_TEAM1) if(c1 >= 0) TeamScore_AddToTeam(COLOR_TEAM1, i, -1000);
1539 if(t != COLOR_TEAM2) if(c2 >= 0) TeamScore_AddToTeam(COLOR_TEAM2, i, -1000);
1540 if(t != COLOR_TEAM3) if(c3 >= 0) TeamScore_AddToTeam(COLOR_TEAM3, i, -1000);
1541 if(t != COLOR_TEAM4) if(c4 >= 0) TeamScore_AddToTeam(COLOR_TEAM4, i, -1000);
1544 AddWinners(team, t);
1555 Exit deathmatch games upon conditions
1558 void CheckRules_World()
1561 local float timelimit;
1562 local float fraglimit;
1570 MapVote_Think should now do that part
1571 if (intermission_running)
1572 if (time >= intermission_exittime + 60)
1574 if(!DoNextMapOverride())
1580 if (gameover) // someone else quit the game already
1582 if(player_count == 0) // Nobody there? Then let's go to the next map
1584 // this will actually check the player count in the next frame
1585 // again, but this shouldn't hurt
1589 timelimit = cvar("timelimit") * 60;
1590 fraglimit = cvar("fraglimit");
1592 if(inWarmupStage || time <= game_starttime) // NOTE: this is <= to prevent problems in the very tic where the game starts
1595 timelimit = 0; // timelimit is not made for warmup
1597 fraglimit = 0; // no fraglimit for now
1601 timelimit += game_starttime;
1603 if(checkrules_overtimeend)
1605 if(!checkrules_overtimewarning)
1607 checkrules_overtimewarning = TRUE;
1608 //announceall("announcer/robotic/1minuteremains.wav");
1609 if(g_race && !g_race_qualifying)
1610 bcenterprint("^3Everyone, finish your lap! The race is over!");
1612 bcenterprint("^3Now playing ^1OVERTIME^3!\n\n^3Keep fragging until we have a ^1winner^3!");
1617 if (timelimit && time >= timelimit)
1619 if(g_race && g_race_qualifying == 2 && timelimit > 0)
1622 float playerswithlaps;
1625 totalplayers = playerswithlaps = readyplayers = 0;
1626 FOR_EACH_PLAYER(head)
1629 if(PlayerScore_Add(head, SP_RACE_FASTEST, 0))
1635 // at least 2/3 of the players have completed a lap: start the RACE
1636 // otherwise, the players should end the qualifying on their own
1637 if(readyplayers || ((totalplayers >= 3) && (playerswithlaps * 3 >= totalplayers * 2)))
1639 checkrules_overtimeend = 0;
1640 ReadyRestart(); // go to race
1650 if (checkrules_overtimeend && time >= checkrules_overtimeend)
1656 if (!checkrules_oneminutewarning && timelimit > 0 && time > timelimit - 60)
1658 checkrules_oneminutewarning = TRUE;
1659 play2all("announcer/robotic/1minuteremains.wav");
1662 status = WinningCondition_RanOutOfSpawns();
1663 if(status == WINNING_YES)
1665 bprint("Hey! Someone ran out of spawns!\n");
1667 else if(g_race && !g_race_qualifying && timelimit >= 0)
1669 status = WinningCondition_Race(fraglimit);
1671 else if(g_race && g_race_qualifying == 2 && timelimit >= 0)
1673 status = WinningCondition_QualifyingThenRace(fraglimit);
1677 status = WinningCondition_Assault(); // TODO remove this?
1681 status = WinningCondition_LMS();
1683 else if (g_onslaught)
1685 status = WinningCondition_Onslaught(); // TODO remove this?
1689 status = WinningCondition_Scores(fraglimit);
1692 if(status == WINNING_STARTOVERTIME)
1694 status = WINNING_NEVER;
1698 if(status == WINNING_NEVER)
1699 // equality cases! Nobody wins if the overtime ends in a draw.
1702 if(checkrules_overtimeend)
1703 if(status != WINNING_NEVER || time >= checkrules_overtimeend)
1704 status = WINNING_YES;
1706 if(status == WINNING_YES)
1710 float mapvote_nextthink;
1711 float mapvote_initialized;
1712 float mapvote_keeptwotime;
1713 float mapvote_timeout;
1714 string mapvote_message;
1715 string mapvote_screenshot_dir;
1717 float mapvote_count;
1718 float mapvote_count_real;
1719 string mapvote_maps[MAPVOTE_COUNT];
1720 float mapvote_maps_suggested[MAPVOTE_COUNT];
1721 string mapvote_suggestions[MAPVOTE_COUNT];
1722 float mapvote_suggestion_ptr;
1723 float mapvote_maxlen;
1724 float mapvote_voters;
1725 float mapvote_votes[MAPVOTE_COUNT];
1727 float mapvote_detail;
1728 float mapvote_abstain;
1729 float mapvote_dirty;
1732 void MapVote_ClearAllVotes()
1734 FOR_EACH_CLIENT(other)
1738 string MapVote_Suggest(string m)
1742 return "That's not how to use this command.";
1743 if(!cvar("g_maplist_votable_suggestions"))
1744 return "Suggestions are not accepted on this server.";
1745 if(mapvote_initialized)
1746 return "Can't suggest - voting is already in progress!";
1747 m = MapInfo_FixName(m);
1749 return "The map you suggested is not available on this server.";
1750 if(!cvar("g_maplist_votable_override_mostrecent"))
1752 return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";
1754 if(!MapInfo_CheckMap(m))
1755 return "The map you suggested does not support the current game mode.";
1756 for(i = 0; i < mapvote_suggestion_ptr; ++i)
1757 if(mapvote_suggestions[i] == m)
1758 return "This map was already suggested.";
1759 if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)
1761 i = floor(random() * mapvote_suggestion_ptr);
1765 i = mapvote_suggestion_ptr;
1766 mapvote_suggestion_ptr += 1;
1768 if(mapvote_suggestions[i] != "")
1769 strunzone(mapvote_suggestions[i]);
1770 mapvote_suggestions[i] = strzone(m);
1771 if(cvar("sv_eventlog"))
1772 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));
1773 return strcat("Suggestion of ", m, " accepted.");
1776 void MapVote_AddVotable(string nextMap, float isSuggestion)
1781 for(j = 0; j < mapvote_count; ++j)
1782 if(mapvote_maps[j] == nextMap)
1784 if(strlen(nextMap) > mapvote_maxlen)
1785 mapvote_maxlen = strlen(nextMap);
1786 mapvote_maps[mapvote_count] = strzone(nextMap);
1787 mapvote_maps_suggested[mapvote_count] = isSuggestion;
1791 void MapVote_SendData(float target);
1797 MapVote_ClearAllVotes();
1800 mapvote_detail = !cvar("g_maplist_votable_nodetail");
1801 mapvote_abstain = cvar("g_maplist_votable_abstain");
1804 nmax = min(MAPVOTE_COUNT - 1, cvar("g_maplist_votable"));
1806 nmax = min(MAPVOTE_COUNT, cvar("g_maplist_votable"));
1807 smax = min3(nmax, cvar("g_maplist_votable_suggestions"), mapvote_suggestion_ptr);
1809 if(mapvote_suggestion_ptr)
1810 for(i = 0; i < 100 && mapvote_count < smax; ++i)
1811 MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], TRUE);
1813 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
1814 MapVote_AddVotable(GetNextMap(), FALSE);
1816 if(mapvote_count == 0)
1818 bprint( "Maplist contains no single playable map! Resetting it to default map list.\n" );
1819 cvar_set("g_maplist", MapInfo_ListAllowedMaps(0, MAPINFO_FLAG_HIDDEN));
1820 localcmd("\nmenu_cmd sync\n");
1821 for(i = 0; i < 100 && mapvote_count < nmax; ++i)
1822 MapVote_AddVotable(GetNextMap(), FALSE);
1825 mapvote_count_real = mapvote_count;
1827 MapVote_AddVotable("don't care", 0);
1829 //dprint("mapvote count is ", ftos(mapvote_count), "\n");
1831 mapvote_keeptwotime = time + cvar("g_maplist_votable_keeptwotime");
1832 mapvote_timeout = time + cvar("g_maplist_votable_timeout");
1833 if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)
1834 mapvote_keeptwotime = 0;
1835 mapvote_message = "Choose a map and press its key!";
1837 mapvote_screenshot_dir = cvar_string("g_maplist_votable_screenshot_dir");
1838 if(mapvote_screenshot_dir == "")
1839 mapvote_screenshot_dir = "maps";
1840 mapvote_screenshot_dir = strzone(mapvote_screenshot_dir);
1842 if(!cvar("g_maplist_textonly"))
1843 MapVote_SendData(MSG_ALL);
1846 void MapVote_SendPicture(float id)
1849 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1850 WriteByte(MSG_ONE, TE_CSQC_MAPVOTE);
1851 WriteByte(MSG_ONE, MAPVOTE_NET_PIC);
1852 WriteByte(MSG_ONE, id);
1853 WritePicture(MSG_ONE, strcat(mapvote_screenshot_dir, "/", mapvote_maps[id]), 3072);
1856 float GameCommand_MapVote(string cmd)
1858 if(!intermission_running)
1860 if(!cvar("g_maplist_textonly"))
1862 if(cmd == "mv_getpic")
1864 MapVote_SendPicture(stof(argv(1)));
1872 float MapVote_GetMapMask()
1874 float mask, i, power;
1876 for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)
1877 if(mapvote_maps[i] != "")
1882 void MapVote_SendData(float targ)
1884 string mapfile, pakfile;
1886 WriteByte(targ, SVC_TEMPENTITY);
1887 WriteByte(targ, TE_CSQC_CONFIG);
1888 WriteString(targ, "mv_screenshot_dir");
1889 WriteString(targ, mapvote_screenshot_dir);
1891 WriteByte(targ, SVC_TEMPENTITY);
1892 WriteByte(targ, TE_CSQC_MAPVOTE);
1893 WriteByte(targ, MAPVOTE_NET_INIT);
1895 WriteByte(targ, mapvote_count);
1896 WriteByte(targ, mapvote_abstain);
1897 WriteByte(targ, mapvote_detail);
1898 WriteCoord(targ, mapvote_timeout);
1899 if(mapvote_count <= 8)
1900 WriteByte(targ, MapVote_GetMapMask());
1902 WriteShort(targ, MapVote_GetMapMask());
1903 for(i = 0; i < mapvote_count; ++i)
1904 if(mapvote_maps[i] != "")
1906 WriteString(targ, mapvote_maps[i]);
1907 mapfile = strcat(mapvote_screenshot_dir, "/", mapvote_maps[i]);
1908 pakfile = whichpack(strcat(mapfile, ".tga"));
1910 pakfile = whichpack(strcat(mapfile, ".jpg"));
1912 pakfile = whichpack(strcat(mapfile, ".png"));
1913 print("pakfile is ", pakfile, "\n");
1914 for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)
1915 pakfile = substring(pakfile, o, 999);
1916 WriteString(targ, pakfile);
1920 void MapVote_UpdateData(float targ)
1923 WriteByte(targ, SVC_TEMPENTITY);
1924 WriteByte(targ, TE_CSQC_MAPVOTE);
1925 WriteByte(targ, MAPVOTE_NET_UPDATE);
1926 if(mapvote_count <= 8)
1927 WriteByte(targ, MapVote_GetMapMask());
1929 WriteShort(targ, MapVote_GetMapMask());
1931 for(i = 0; i < mapvote_count; ++i)
1932 if(mapvote_maps[i] != "")
1933 WriteByte(targ, mapvote_votes[i]);
1936 void MapVote_TellVote(float targ, float vote)
1938 WriteByte(targ, SVC_TEMPENTITY);
1939 WriteByte(targ, TE_CSQC_MAPVOTE);
1940 WriteByte(targ, MAPVOTE_NET_OWNVOTE);
1941 WriteByte(targ, vote);
1944 float MapVote_Finished(float mappos)
1950 if(cvar("sv_eventlog"))
1952 result = strcat(":vote:finished:", mapvote_maps[mappos]);
1953 result = strcat(result, ":", ftos(mapvote_votes[mappos]), "::");
1954 didntvote = mapvote_voters;
1955 for(i = 0; i < mapvote_count; ++i)
1956 if(mapvote_maps[i] != "")
1958 didntvote -= mapvote_votes[i];
1961 result = strcat(result, ":", mapvote_maps[i]);
1962 result = strcat(result, ":", ftos(mapvote_votes[i]));
1965 result = strcat(result, ":didn't vote:", ftos(didntvote));
1967 GameLogEcho(result);
1968 if(mapvote_maps_suggested[mappos])
1969 GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));
1972 FOR_EACH_REALCLIENT(other)
1973 FixClientCvars(other);
1975 Map_Goto_SetStr(mapvote_maps[mappos]);
1977 alreadychangedlevel = TRUE;
1980 void MapVote_CheckRules_1()
1984 for(i = 0; i < mapvote_count; ++i) if(mapvote_maps[i] != "")
1986 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");
1987 mapvote_votes[i] = 0;
1991 FOR_EACH_REALCLIENT(other)
1996 i = other.mapvote - 1;
1997 //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");
1998 mapvote_votes[i] = mapvote_votes[i] + 1;
2003 float MapVote_CheckRules_2()
2006 float firstPlace, secondPlace;
2007 float firstPlaceVotes, secondPlaceVotes;
2008 float mapvote_voters_real;
2011 mapvote_voters_real = mapvote_voters;
2013 mapvote_voters_real -= mapvote_votes[mapvote_count - 1];
2015 RandomSelection_Init();
2016 for(i = 0; i < mapvote_count_real; ++i) if(mapvote_maps[i] != "")
2017 RandomSelection_Add(world, i, 1, mapvote_votes[i]);
2018 firstPlace = RandomSelection_chosen_float;
2019 firstPlaceVotes = RandomSelection_best_priority;
2020 //dprint("First place: ", ftos(firstPlace), "\n");
2021 //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");
2023 RandomSelection_Init();
2024 for(i = 0; i < mapvote_count_real; ++i) if(mapvote_maps[i] != "")
2026 RandomSelection_Add(world, i, 1, mapvote_votes[i]);
2027 secondPlace = RandomSelection_chosen_float;
2028 secondPlaceVotes = RandomSelection_best_priority;
2029 //dprint("Second place: ", ftos(secondPlace), "\n");
2030 //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");
2032 if(firstPlace == -1)
2033 error("No first place in map vote... WTF?");
2035 if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)
2036 return MapVote_Finished(firstPlace);
2038 if(mapvote_keeptwotime)
2039 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)
2042 mapvote_dirty = TRUE;
2043 mapvote_message = "Now decide between the TOP TWO!";
2044 mapvote_keeptwotime = 0;
2045 result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);
2046 result = strcat(result, ":", ftos(firstPlaceVotes));
2047 result = strcat(result, ":", mapvote_maps[secondPlace]);
2048 result = strcat(result, ":", ftos(secondPlaceVotes), "::");
2049 didntvote = mapvote_voters;
2050 for(i = 0; i < mapvote_count; ++i)
2051 if(mapvote_maps[i] != "")
2053 didntvote -= mapvote_votes[i];
2055 if(i != secondPlace)
2057 result = strcat(result, ":", mapvote_maps[i]);
2058 result = strcat(result, ":", ftos(mapvote_votes[i]));
2059 if(i < mapvote_count_real)
2061 strunzone(mapvote_maps[i]);
2062 mapvote_maps[i] = "";
2066 result = strcat(result, ":didn't vote:", ftos(didntvote));
2067 if(cvar("sv_eventlog"))
2068 GameLogEcho(result);
2081 keeptwo = mapvote_keeptwotime;
2082 MapVote_CheckRules_1(); // count
2083 if(MapVote_CheckRules_2()) // decide
2087 FOR_EACH_REALCLIENT(other)
2089 // hide scoreboard again
2090 if(other.health != 2342)
2092 other.health = 2342;
2094 if(clienttype(other) == CLIENTTYPE_REAL)
2096 if(cvar("g_maplist_textonly"))
2097 stuffcmd(other, "\nin_bind 7 1 \"impulse 1\"; in_bind 7 2 \"impulse 2\"; in_bind 7 3 \"impulse 3\"; in_bind 7 4 \"impulse 4\"; in_bind 7 5 \"impulse 5\"; in_bind 7 6 \"impulse 6\"; in_bind 7 7 \"impulse 7\"; in_bind 7 8 \"impulse 8\"; in_bind 7 9 \"impulse 9\"; in_bind 7 0 \"impulse 10\"; in_bind 7 KP_1 \"impulse 1\"; in_bind 7 KP_2 \"impulse 2\"; in_bind 7 KP_3 \"impulse 3\"; in_bind 7 KP_4 \"impulse 4\"; in_bind 7 KP_5 \"impulse 5\"; in_bind 7 KP_6 \"impulse 6\"; in_bind 7 KP_7 \"impulse 7\"; in_bind 7 KP_8 \"impulse 8\"; in_bind 7 KP_9 \"impulse 9\"; in_bind 7 KP_0 \"impulse 10\"; in_bindmap 7 0\n");
2100 WriteByte(MSG_ONE, SVC_FINALE);
2101 WriteString(MSG_ONE, "");
2105 // notify about keep-two
2106 if(keeptwo != 0 && mapvote_keeptwotime == 0)
2107 play2(other, "misc/invshot.wav");
2109 // clear possibly invalid votes
2110 if(mapvote_maps[other.mapvote - 1] == "")
2112 // use impulses as new vote
2113 if(other.impulse >= 1 && other.impulse <= mapvote_count)
2114 if(mapvote_maps[other.impulse - 1] != "")
2116 other.mapvote = other.impulse;
2118 mapvote_dirty = TRUE;
2121 MapVote_TellVote(MSG_ONE, other.mapvote);
2129 MapVote_CheckRules_1(); // just count
2131 if(!cvar("g_maplist_textonly"))
2132 if(mapvote_dirty) // 1 if "keeptwo" or "impulse" happened before
2134 MapVote_UpdateData(MSG_BROADCAST);
2135 mapvote_dirty = FALSE;
2138 if(cvar("g_maplist_textonly"))
2140 FOR_EACH_REALCLIENT(other)
2142 // display voting screen
2143 msgstr = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
2144 msgstr = substring(msgstr, 0, strlen(msgstr) - mapvote_count);
2146 msgstr = substring(msgstr, 1, strlen(msgstr) - 1);
2147 msgstr = strcat(msgstr, mapvote_message);
2148 msgstr = strcat(msgstr, "\n\n");
2149 for(i = 0; i < mapvote_count; ++i)
2150 if(mapvote_maps[i] == "")
2151 msgstr = strcat(msgstr, "\n");
2154 tmp = mapvote_maps[i];
2155 tmp = strpad(mapvote_maxlen, tmp);
2156 tmp = strcat(ftos(mod(i + 1, 10)), ": ", tmp);
2159 tmp = strcat(tmp, " ^2(", ftos(mapvote_votes[i]), " vote");
2160 if(mapvote_votes[i] != 1)
2161 tmp = strcat(tmp, "s");
2162 tmp = strcat(tmp, ")");
2163 tmp = strpad(mapvote_maxlen + 15, tmp);
2166 if(i == mapvote_count - 1)
2167 msgstr = strcat(msgstr, "\n");
2168 if(other.mapvote == i + 1)
2169 msgstr = strcat(msgstr, "^3> ", tmp, "\n");
2171 msgstr = strcat(msgstr, "^7 ", tmp, "\n");
2174 msgstr = strcat(msgstr, "\n\n^2", ftos(totalvotes), " vote");
2176 msgstr = strcat(msgstr, "s");
2177 msgstr = strcat(msgstr, " cast");
2178 i = ceil(mapvote_timeout - time);
2179 msgstr = strcat(msgstr, "\n", ftos(i), " second");
2181 msgstr = strcat(msgstr, "s");
2182 msgstr = strcat(msgstr, " left");
2184 centerprint_atprio(other, CENTERPRIO_MAPVOTE, msgstr);
2188 void MapVote_Start()
2193 MapInfo_Enumerate();
2194 if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0, (g_maplist_allow_hidden ? MAPINFO_FLAG_HIDDEN : 0), 1))
2197 void MapVote_Think()
2202 if(alreadychangedlevel)
2205 if(time < mapvote_nextthink)
2209 mapvote_nextthink = time + 0.5;
2211 if(!mapvote_initialized)
2213 mapvote_initialized = TRUE;
2214 if(DoNextMapOverride())
2216 if(!cvar("g_maplist_votable") || player_count <= 0)
2227 string GotoMap(string m)
2229 if(!MapInfo_CheckMap(m))
2230 return "The map you chose is not available on this server.";
2231 cvar_set("nextmap", m);
2232 cvar_set("timelimit", "-1");
2233 if(mapvote_initialized || alreadychangedlevel)
2235 if(DoNextMapOverride())
2236 return "Map switch initiated.";
2238 return "Hm... no. For some reason I like THIS map more.";
2241 return "Map switch will happen after scoreboard.";
2247 FOR_EACH_REALCLIENT(self)
2249 if(self.classname == "spectator")
2251 if(self.enemy.typehitsound)
2252 play2(self, "misc/typehit.wav");
2253 else if(self.enemy.hitsound)
2254 play2(self, "misc/hit.wav");
2258 if(self.typehitsound)
2259 play2(self, "misc/typehit.wav");
2260 else if(self.hitsound)
2261 play2(self, "misc/hit.wav");
2264 FOR_EACH_CLIENT(self)
2266 self.hitsound = FALSE;
2267 self.typehitsound = FALSE;
2274 * returns TRUE if redirecting
2276 float redirection_timeout;
2277 float redirection_nextthink;
2278 float RedirectionThink()
2280 float clients_found;
2282 if(redirection_target == "")
2285 if(!redirection_timeout)
2287 cvar_set("sv_public", "-2");
2288 redirection_timeout = time + 0.6; // this will only try twice... should be able to keep more clients
2289 if(redirection_target == "self")
2290 bprint("^3SERVER NOTICE:^7 restarting the server\n");
2292 bprint("^3SERVER NOTICE:^7 redirecting everyone to ", redirection_target, "\n");
2295 if(time < redirection_nextthink)
2298 redirection_nextthink = time + 1;
2301 FOR_EACH_REALCLIENT(self)
2303 print("Redirecting: sending connect command to ", self.netname, "\n");
2304 if(redirection_target == "self")
2305 stuffcmd(self, "\ndisconnect; reconnect\n");
2307 stuffcmd(self, strcat("\ndisconnect; connect ", redirection_target, "\n"));
2311 print("Redirecting: ", ftos(clients_found), " clients left.\n");
2313 if(time > redirection_timeout || clients_found == 0)
2314 localcmd("\nwait; wait; wait; quit\n");
2321 // Loaded from a save game
2322 // some things then break, so let's work around them...
2324 // Progs DB (capture records)
2326 ServerProgsDB = db_create();
2328 ServerProgsDB = db_load("server.db");
2332 MapInfo_Enumerate();
2333 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), 0, (g_maplist_allow_hidden ? MAPINFO_FLAG_HIDDEN : 0), 1);
2338 if(world_initialized > 0)
2340 world_initialized = 0;
2341 print("Saving persistent data...\n");
2344 db_save(ServerProgsDB, "server.db");
2345 if(cvar("developer"))
2346 db_save(TemporaryDB, "server-temp.db");
2347 db_close(ServerProgsDB);
2348 db_close(TemporaryDB);
2350 // tell the bot system the game is ending now
2355 else if(world_initialized == 0)
2357 print("NOTE: crashed before even initializing the world, not saving persistent data\n");