]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - data/qcsrc/server/g_world.qc
Separate intermission tracks for winning and loosing
[voretournament/voretournament.git] / data / qcsrc / server / g_world.qc
1 entity pingplreport;\r
2 void PingPLReport_Think()\r
3 {\r
4         float delta;\r
5         entity e;\r
6 \r
7         delta = 3 / maxclients;\r
8         if(delta < sys_frametime)\r
9                 delta = 0;\r
10         self.nextthink = time + delta;\r
11 \r
12         e = edict_num(self.cnt + 1);\r
13         if(clienttype(e) == CLIENTTYPE_REAL)\r
14         {\r
15                 WriteByte(MSG_BROADCAST, SVC_TEMPENTITY);\r
16                 WriteByte(MSG_BROADCAST, TE_CSQC_PINGPLREPORT);\r
17                 WriteByte(MSG_BROADCAST, self.cnt);\r
18                 WriteShort(MSG_BROADCAST, max(1, e.ping));\r
19                 WriteByte(MSG_BROADCAST, ceil(e.ping_packetloss * 255));\r
20                 WriteByte(MSG_BROADCAST, ceil(e.ping_movementloss * 255));\r
21         }\r
22         else\r
23         {\r
24                 WriteByte(MSG_BROADCAST, SVC_TEMPENTITY);\r
25                 WriteByte(MSG_BROADCAST, TE_CSQC_PINGPLREPORT);\r
26                 WriteByte(MSG_BROADCAST, self.cnt);\r
27                 WriteShort(MSG_BROADCAST, 0);\r
28                 WriteByte(MSG_BROADCAST, 0);\r
29                 WriteByte(MSG_BROADCAST, 0);\r
30         }\r
31         self.cnt = mod(self.cnt + 1, maxclients);\r
32 }\r
33 void PingPLReport_Spawn()\r
34 {\r
35         pingplreport = spawn();\r
36         pingplreport.classname = "pingplreport";\r
37         pingplreport.think = PingPLReport_Think;\r
38         pingplreport.nextthink = time;\r
39 }\r
40 \r
41 entity otherplreport;\r
42 void OtherPLReport_Think()\r
43 {\r
44         entity e;\r
45 \r
46         self.nextthink = time;\r
47 \r
48         e = edict_num(self.cnt + 1);\r
49         WriteByte(MSG_BROADCAST, SVC_TEMPENTITY);\r
50         WriteByte(MSG_BROADCAST, TE_CSQC_OTHERPLREPORT);\r
51         WriteByte(MSG_BROADCAST, self.cnt);\r
52         if(e.predator.classname == "player") // tempt hackers less by only sending the info of eaten players\r
53         {\r
54                 if(cvar("g_vore_showhealth"))\r
55                         WriteByte(MSG_BROADCAST, e.health);\r
56                 else\r
57                         WriteByte(MSG_BROADCAST, 0);\r
58                 WriteByte(MSG_BROADCAST, num_for_edict(e.predator));\r
59         }\r
60         else\r
61         {\r
62                 WriteByte(MSG_BROADCAST, 0);\r
63                 WriteByte(MSG_BROADCAST, 0);\r
64         }\r
65         self.cnt = mod(self.cnt + 1, maxclients);\r
66 }\r
67 \r
68 void OtherPLReport_Spawn()\r
69 {\r
70         otherplreport = spawn();\r
71         otherplreport.classname = "otherplreport";\r
72         otherplreport.think = OtherPLReport_Think;\r
73         otherplreport.nextthink = time;\r
74 }\r
75 \r
76 float SPAWNFLAG_NO_WAYPOINTS_FOR_ITEMS = 1;\r
77 string redirection_target;\r
78 float world_initialized;\r
79 \r
80 string GetMapname();\r
81 string GetGametype();\r
82 void GotoNextMap();\r
83 void ShuffleMaplist()\r
84 float() DoNextMapOverride;\r
85 \r
86 void SetDefaultAlpha()\r
87 {\r
88         if(cvar("g_running_guns"))\r
89         {\r
90                 default_player_alpha = -1;\r
91                 default_weapon_alpha = +1;\r
92         }\r
93         else if(g_cloaked)\r
94         {\r
95                 default_player_alpha = cvar("g_balance_cloaked_alpha");\r
96                 default_weapon_alpha = default_player_alpha;\r
97         }\r
98         else\r
99         {\r
100                 default_player_alpha = cvar("g_player_alpha");\r
101                 if(default_player_alpha == 0)\r
102                         default_player_alpha = 1;\r
103                 default_weapon_alpha = default_player_alpha;\r
104         }\r
105 }\r
106 \r
107 void fteqcc_testbugs()\r
108 {\r
109         float a, b;\r
110 \r
111         if(!cvar("developer_fteqccbugs"))\r
112                 return;\r
113 \r
114         dprint("*** fteqcc test: checking for bugs...\n");\r
115 \r
116         a = 1;\r
117         b = 5;\r
118         if(sqrt(a) - sqrt(b - a) == 0)\r
119                 dprint("*** fteqcc test: found same-function-twice bug\n");\r
120         else\r
121                 dprint("*** fteqcc test: same-function-twice bug got FINALLY FIXED! HOORAY!\n");\r
122 \r
123         world.cnt = -10;\r
124         world.enemy = world;\r
125         world.enemy.cnt += 10;\r
126         if(world.cnt > 0.2 || world.cnt < -0.2) // don't error out if it's just roundoff errors\r
127                 dprint("*** fteqcc test: found += bug\n");\r
128         else\r
129                 dprint("*** fteqcc test: += bug got FINALLY FIXED! HOORAY!\n");\r
130         world.cnt = 0;\r
131 }\r
132 \r
133 /**\r
134  * Takes care of pausing and unpausing the game.\r
135  * Centerprints the information about an upcoming or active timeout to all active\r
136  * players. Also plays reminder sounds.\r
137  */\r
138 void timeoutHandler_Think() {\r
139         local string timeStr;\r
140         local entity plr;\r
141         if (timeoutStatus == 1) {\r
142                 if (remainingLeadTime > 0) {\r
143                         //centerprint the information to every player\r
144                         timeStr = getTimeoutText(0);\r
145                         FOR_EACH_REALCLIENT(plr) {\r
146                                 if(plr.classname == "player") {\r
147                                         centerprint_atprio(plr, CENTERPRIO_SPAM, timeStr);\r
148                                 }\r
149                         }\r
150                         remainingLeadTime -= 1;\r
151                         //think again in 1 second:\r
152                         self.nextthink = time + 1;\r
153                 }\r
154                 else {\r
155                         //now pause the game:\r
156                         timeoutStatus = 2;\r
157                         //reset all the flood variables\r
158                         FOR_EACH_CLIENT(plr) {\r
159                                 plr.nickspamcount = plr.nickspamtime = plr.floodcontrol_chat = plr.floodcontrol_chatteam = plr.floodcontrol_chattell = plr.floodcontrol_voice = plr.floodcontrol_voiceteam = 0;\r
160                         }\r
161                         cvar_set("slowmo", ftos(TIMEOUT_SLOWMO_VALUE));\r
162                         //copy .v_angle to .lastV_angle for every player in order to fix their view during pause (see PlayerPreThink)\r
163                         FOR_EACH_REALPLAYER(plr) {\r
164                                 plr.lastV_angle = plr.v_angle;\r
165                         }\r
166                         self.nextthink = time;\r
167                 }\r
168         }\r
169         else if (timeoutStatus == 2) {\r
170                 if (remainingTimeoutTime > 0) {\r
171                         timeStr = getTimeoutText(0);\r
172                         FOR_EACH_REALCLIENT(plr) {\r
173                                 if(plr.classname == "player") {\r
174                                         centerprint_atprio(plr, CENTERPRIO_SPAM, timeStr);\r
175                                 }\r
176                         }\r
177                         if(remainingTimeoutTime == cvar("sv_timeout_resumetime")) { //play a warning sound when only <sv_timeout_resumetime> seconds are left\r
178                                 Announce("prepareforbattle");\r
179                         }\r
180                         remainingTimeoutTime -= 1;\r
181                         self.nextthink = time + TIMEOUT_SLOWMO_VALUE;\r
182                 }\r
183                 else {\r
184                         //unpause the game again\r
185                         remainingTimeoutTime = timeoutStatus = 0;\r
186                         cvar_set("slowmo", ftos(orig_slowmo));\r
187                         //and unlock the fixed view again once there is no timeout active anymore\r
188                         FOR_EACH_REALPLAYER(plr) {\r
189                                 plr.fixangle = FALSE;\r
190                         }\r
191                         //get rid of the countdown message\r
192                         FOR_EACH_REALCLIENT(plr) {\r
193                                 if(plr.classname == "player") {\r
194                                         centerprint_atprio(plr, CENTERPRIO_SPAM, "");\r
195                                 }\r
196                         }\r
197                         remove(self);\r
198                         return;\r
199                 }\r
200 \r
201         }\r
202         else if (timeoutStatus == 0) { //if a player called the resumegame command (which set timeoutStatus to 0 already)\r
203                 FOR_EACH_REALCLIENT(plr) {\r
204                         if(plr.classname == "player") {\r
205                                 centerprint_atprio(plr, CENTERPRIO_SPAM, "");\r
206                         }\r
207                 }\r
208                 remove(self);\r
209                 return;\r
210         }\r
211 }\r
212 \r
213 void GotoFirstMap()\r
214 {\r
215         float n;\r
216         if(cvar("_sv_init"))\r
217         {\r
218                 // cvar_set("_sv_init", "0");\r
219                 // we do NOT set this to 0 any more, so someone "accidentally" changing\r
220                 // to this "init" map on a dedicated server will cause no permanent\r
221                 // harm\r
222                 if(cvar("g_maplist_shuffle"))\r
223                         ShuffleMaplist();\r
224                 n = tokenizebyseparator(cvar_string("g_maplist"), " ");\r
225                 cvar_set("g_maplist_index", ftos(n - 1)); // jump to map 0 in GotoNextMap\r
226 \r
227                 MapInfo_Enumerate();\r
228                 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);\r
229 \r
230                 if(!DoNextMapOverride())\r
231                         GotoNextMap();\r
232 \r
233                 return;\r
234         }\r
235 \r
236         if(time < 5)\r
237         {\r
238                 self.nextthink = time;\r
239         }\r
240         else\r
241         {\r
242                 self.nextthink = time + 1;\r
243                 print("Waiting for _sv_init being set to 1 by initialization scripts...\n");\r
244         }\r
245 }\r
246 \r
247 void cvar_changes_init()\r
248 {\r
249         float h;\r
250         string k, v, d;\r
251         float n, i;\r
252 \r
253         if(cvar_changes)\r
254                 strunzone(cvar_changes);\r
255         cvar_changes = string_null;\r
256 \r
257         h = buf_create();\r
258         buf_cvarlist(h, "", "_"); // exclude all _ cvars as they are temporary\r
259         n = buf_getsize(h);\r
260 \r
261         for(i = 0; i < n; ++i)\r
262         {\r
263                 k = bufstr_get(h, i);\r
264 \r
265 #define BADPREFIX(p) if(substring(k, 0, strlen(p)) == p) continue\r
266 #define BADCVAR(p) if(k == p) continue\r
267                 // internal\r
268                 BADPREFIX("csqc_");\r
269                 BADPREFIX("cvar_check_");\r
270                 BADCVAR("gamecfg");\r
271                 BADCVAR("g_configversion");\r
272                 BADCVAR("g_maplist_index");\r
273                 BADCVAR("halflifebsp");\r
274 \r
275                 // client\r
276                 BADPREFIX("cl_");\r
277                 BADPREFIX("con_");\r
278                 BADPREFIX("g_campaign");\r
279                 BADPREFIX("gl_");\r
280                 BADPREFIX("joy");\r
281                 BADPREFIX("menu_");\r
282                 BADPREFIX("net_slist_");\r
283                 BADPREFIX("r_");\r
284                 BADPREFIX("sbar_");\r
285                 BADPREFIX("scr_");\r
286                 BADPREFIX("userbind");\r
287                 BADPREFIX("v_");\r
288                 BADPREFIX("vid_");\r
289                 BADPREFIX("crosshair");\r
290                 BADCVAR("mod_q3bsp_lightmapmergepower");\r
291                 BADCVAR("mod_q3bsp_nolightmaps");\r
292 \r
293                 // private\r
294                 BADCVAR("serverconfig");\r
295                 BADPREFIX("g_ban_");\r
296                 BADPREFIX("g_chat_flood_");\r
297                 BADPREFIX("g_voice_flood_");\r
298                 BADPREFIX("rcon_");\r
299                 BADPREFIX("settemp_");\r
300                 BADPREFIX("sv_allowdownloads_");\r
301                 BADPREFIX("sv_autodemo");\r
302                 BADPREFIX("sv_curl_");\r
303                 BADPREFIX("sv_eventlog");\r
304                 BADPREFIX("sv_logscores_");\r
305                 BADPREFIX("sv_master");\r
306                 BADCVAR("g_banned_list");\r
307                 BADCVAR("log_dest_udp");\r
308                 BADCVAR("log_file");\r
309                 BADCVAR("net_address");\r
310                 BADCVAR("port");\r
311                 BADCVAR("savedgamecfg");\r
312                 BADCVAR("sv_heartbeatperiod");\r
313                 BADCVAR("sv_vote_master_password");\r
314                 BADCVAR("sys_colortranslation");\r
315                 BADCVAR("sys_specialcharactertranslation");\r
316                 BADCVAR("timestamps");\r
317 \r
318                 // mapinfo\r
319                 BADCVAR("timelimit");\r
320                 BADCVAR("fraglimit");\r
321                 BADCVAR("g_arena");\r
322                 BADCVAR("g_ca");\r
323                 BADCVAR("g_assault");\r
324                 BADCVAR("g_ctf");\r
325                 BADCVAR("g_dm");\r
326                 BADCVAR("g_domination");\r
327                 BADCVAR("g_keyhunt");\r
328                 BADCVAR("g_keyhunt_teams");\r
329                 BADCVAR("g_onslaught");\r
330                 BADCVAR("g_race");\r
331                 BADCVAR("g_cts");\r
332                 BADCVAR("g_tdm");\r
333                 BADCVAR("g_rpg");\r
334                 BADCVAR("teamplay");\r
335 \r
336                 // long\r
337                 BADCVAR("hostname");\r
338                 BADCVAR("g_maplist");\r
339                 BADCVAR("g_maplist_mostrecent");\r
340                 BADCVAR("sv_motd");\r
341 #undef BADPREFIX\r
342 #undef BADCVAR\r
343 \r
344                 v = cvar_string(k);\r
345                 d = cvar_defstring(k);\r
346                 if(v != d)\r
347                 {\r
348                         cvar_changes = strcat(cvar_changes, k, " \"", v, "\" // \"", d, "\"\n");\r
349                         if(strlen(cvar_changes) > 16384)\r
350                         {\r
351                                 cvar_changes = "// too many settings have been changed to show them here\n";\r
352                                 break;\r
353                         }\r
354                 }\r
355         }\r
356         buf_del(h);\r
357         if(cvar_changes == "")\r
358                 cvar_changes = "// this server runs at default settings\n";\r
359         else\r
360                 cvar_changes = strcat("// this server runs at modified settings:\n", cvar_changes);\r
361         cvar_changes = strzone(cvar_changes);\r
362 }\r
363 \r
364 void detect_maptype()\r
365 {\r
366 #if 0\r
367         vector o, v;\r
368         float i;\r
369 \r
370         for(;;)\r
371         {\r
372                 o = world.mins;\r
373                 o_x += random() * (world.maxs_x - world.mins_x);\r
374                 o_y += random() * (world.maxs_y - world.mins_y);\r
375                 o_z += random() * (world.maxs_z - world.mins_z);\r
376 \r
377                 tracebox(o, PL_MIN, PL_MAX, o - '0 0 32768', MOVE_WORLDONLY, world);\r
378                 if(trace_fraction == 1)\r
379                         continue;\r
380 \r
381                 v = trace_endpos;\r
382 \r
383                 for(i = 0; i < 64; i += 4)\r
384                 {\r
385                         tracebox(o, '-1 -1 -1' * i, '1 1 1' * i, o - '0 0 32768', MOVE_WORLDONLY, world);\r
386         if(trace_fraction == 1)\r
387                 continue;\r
388                         print(ftos(i), " -> ", vtos(trace_endpos), "\n");\r
389                 }\r
390 \r
391                 break;\r
392         }\r
393 #endif\r
394 }\r
395 \r
396 entity randomseed;\r
397 float RandomSeed_Send(entity to, float sf)\r
398 {\r
399         WriteByte(MSG_ENTITY, ENT_CLIENT_RANDOMSEED);\r
400         WriteShort(MSG_ENTITY, self.cnt);\r
401         return TRUE;\r
402 }\r
403 void RandomSeed_Think()\r
404 {\r
405         self.cnt = bound(0, floor(random() * 65536), 65535);\r
406         self.nextthink = time + 5;\r
407 \r
408         self.SendFlags |= 1;\r
409 }\r
410 void RandomSeed_Spawn()\r
411 {\r
412         randomseed = spawn();\r
413         randomseed.think = RandomSeed_Think;\r
414         Net_LinkEntity(randomseed, FALSE, 0, RandomSeed_Send);\r
415 \r
416         entity oldself;\r
417         oldself = self;\r
418         self = randomseed;\r
419         self.think(); // sets random seed and nextthink\r
420         self = oldself;\r
421 }\r
422 \r
423 void spawnfunc___init_dedicated_server(void)\r
424 {\r
425         // handler for _init/_init map (only for dedicated server initialization)\r
426 \r
427         world_initialized = -1; // don't complain\r
428         cvar = cvar_normal;\r
429         cvar_string = cvar_string_normal;\r
430         cvar_set = cvar_set_normal;\r
431 \r
432         remove = remove_unsafely;\r
433 \r
434         entity e;\r
435         e = spawn();\r
436         e.think = GotoFirstMap;\r
437         e.nextthink = time; // this is usually 1 at this point\r
438 \r
439         e = spawn();\r
440         e.classname = "info_player_deathmatch"; // safeguard against player joining\r
441 \r
442         self.classname = "worldspawn"; // safeguard against various stuff ;)\r
443 \r
444         MapInfo_Enumerate();\r
445         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);\r
446 }\r
447 \r
448 void Map_MarkAsRecent(string m);\r
449 float world_already_spawned;\r
450 void RegisterWeapons();\r
451 void Nagger_Init();\r
452 void ClientInit_Spawn();\r
453 void WeaponStats_Init();\r
454 void WeaponStats_Shutdown();\r
455 void spawnfunc_worldspawn (void)\r
456 {\r
457         float fd, l, i, j, n;\r
458         string s, col;\r
459 \r
460         cvar = cvar_normal;\r
461         cvar_string = cvar_string_normal;\r
462         cvar_set = cvar_set_normal;\r
463 \r
464         if(world_already_spawned)\r
465                 error("world already spawned - you may have EXACTLY ONE worldspawn!");\r
466         world_already_spawned = TRUE;\r
467 \r
468         remove = remove_safely; // during spawning, watch what you remove!\r
469 \r
470         check_unacceptable_compiler_bugs();\r
471 \r
472         compressShortVector_init();\r
473 \r
474         local entity head;\r
475         head = nextent(world);\r
476         maxclients = 0;\r
477         while(head)\r
478         {\r
479                 ++maxclients;\r
480                 head = nextent(head);\r
481         }\r
482 \r
483         // needs to be done so early as they would still spawn\r
484         RegisterWeapons();\r
485 \r
486         ServerProgsDB = db_load("server.db");\r
487 \r
488         TemporaryDB = db_create();\r
489 \r
490         /*\r
491         TODO sound pack system\r
492         // initialize sound pack system\r
493         soundpack = cvar_string("g_soundpack");\r
494         if(soundpack != "")\r
495                 soundpack = strcat(soundpack, "/");\r
496         soundpack = strzone(soundpack);\r
497         */\r
498 \r
499         // 0 normal\r
500         lightstyle(0, "m");\r
501 \r
502         // 1 FLICKER (first variety)\r
503         lightstyle(1, "mmnmmommommnonmmonqnmmo");\r
504 \r
505         // 2 SLOW STRONG PULSE\r
506         lightstyle(2, "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba");\r
507 \r
508         // 3 CANDLE (first variety)\r
509         lightstyle(3, "mmmmmaaaaammmmmaaaaaabcdefgabcdefg");\r
510 \r
511         // 4 FAST STROBE\r
512         lightstyle(4, "mamamamamama");\r
513 \r
514         // 5 GENTLE PULSE 1\r
515         lightstyle(5,"jklmnopqrstuvwxyzyxwvutsrqponmlkj");\r
516 \r
517         // 6 FLICKER (second variety)\r
518         lightstyle(6, "nmonqnmomnmomomno");\r
519 \r
520         // 7 CANDLE (second variety)\r
521         lightstyle(7, "mmmaaaabcdefgmmmmaaaammmaamm");\r
522 \r
523         // 8 CANDLE (third variety)\r
524         lightstyle(8, "mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa");\r
525 \r
526         // 9 SLOW STROBE (fourth variety)\r
527         lightstyle(9, "aaaaaaaazzzzzzzz");\r
528 \r
529         // 10 FLUORESCENT FLICKER\r
530         lightstyle(10, "mmamammmmammamamaaamammma");\r
531 \r
532         // 11 SLOW PULSE NOT FADE TO BLACK\r
533         lightstyle(11, "abcdefghijklmnopqrrqponmlkjihgfedcba");\r
534 \r
535         // styles 32-62 are assigned by the spawnfunc_light program for switchable lights\r
536 \r
537         // 63 testing\r
538         lightstyle(63, "a");\r
539 \r
540         if(cvar("g_campaign"))\r
541                 CampaignPreInit();\r
542 \r
543         Map_MarkAsRecent(mapname);\r
544 \r
545         precache_model ("null"); // we need this one before InitGameplayMode\r
546         InitGameplayMode();\r
547         readlevelcvars();\r
548         GrabberInit();\r
549 \r
550         player_count = 0;\r
551         bot_waypoints_for_items = cvar("g_waypoints_for_items");\r
552         if(bot_waypoints_for_items == 1)\r
553                 if(self.spawnflags & SPAWNFLAG_NO_WAYPOINTS_FOR_ITEMS)\r
554                         bot_waypoints_for_items = 0;\r
555 \r
556         // for setting by mapinfo\r
557         q3acompat_machineshotgunswap = cvar("sv_q3acompat_machineshotgunswap");\r
558         cvar_set("sv_q3acompat_machineshotgunswap", "0");\r
559 \r
560         precache();\r
561 \r
562         WaypointSprite_Init();\r
563 \r
564         //if (g_domination)\r
565         //      dom_init();\r
566 \r
567         GameLogInit(); // prepare everything\r
568         if(cvar("sv_eventlog"))\r
569         {\r
570                 s = strcat(cvar_string("sv_eventlog_files_counter"), ".");\r
571                 s = strcat(s, ftos(random()));\r
572                 matchid = strzone(s);\r
573 \r
574                 GameLogEcho(strcat(":gamestart:", GetGametype(), "_", GetMapname(), ":", s));\r
575                 s = ":gameinfo:mutators:LIST";\r
576                 if(!cvar("g_use_ammunition"))\r
577                         s = strcat(s, ":no_use_ammunition");\r
578                 if(!cvar("g_pickup_items"))\r
579                         s = strcat(s, ":no_pickup_items");\r
580                 if(cvar("g_vampire"))\r
581                         s = strcat(s, ":vampire");\r
582                 if(cvar("g_norecoil"))\r
583                         s = strcat(s, ":norecoil");\r
584                 if(cvar("g_midair"))\r
585                         s = strcat(s, ":midair");\r
586                 GameLogEcho(s);\r
587                 GameLogEcho(":gameinfo:end");\r
588         }\r
589         else\r
590                 matchid = strzone(ftos(random()));\r
591 \r
592         cvar_set("nextmap", "");\r
593 \r
594         SetDefaultAlpha();\r
595 \r
596         if(cvar("g_campaign"))\r
597                 CampaignPostInit();\r
598 \r
599         fteqcc_testbugs();\r
600 \r
601         Ban_LoadBans();\r
602 \r
603         MapInfo_Enumerate();\r
604         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1);\r
605 \r
606         if(whichpack(strcat("maps/", mapname, ".cfg")) != "")\r
607         {\r
608                 fd = fopen(strcat("maps/", mapname, ".cfg"), FILE_READ);\r
609                 if(fd != -1)\r
610                 {\r
611                         while((s = fgets(fd)))\r
612                         {\r
613                                 l = tokenize_console(s);\r
614                                 if(l < 2)\r
615                                         continue;\r
616                                 if(argv(0) == "cd")\r
617                                 {\r
618                                         print("Found ^1DEPRECATED^7 cd loop command in .cfg file; put this line in mapinfo instead:\n");\r
619                                         print("  cdtrack ", argv(2), "\n");\r
620                                 }\r
621                                 else if(argv(0) == "fog")\r
622                                 {\r
623                                         print("Found ^1DEPRECATED^7 fog command in .cfg file; put this line in worldspawn in the .map/.bsp/.ent file instead:\n");\r
624                                         print("  \"fog\" \"", s, "\"\n");\r
625                                 }\r
626                                 else if(argv(0) == "set")\r
627                                 {\r
628                                         print("Found ^1DEPRECATED^7 set command in .cfg file; put this line in mapinfo instead:\n");\r
629                                         print("  clientsettemp_for_type all ", argv(1), " ", argv(2), "\n");\r
630                                 }\r
631                                 else if(argv(0) != "//")\r
632                                 {\r
633                                         print("Found ^1DEPRECATED^7 set command in .cfg file; put this line in mapinfo instead:\n");\r
634                                         print("  clientsettemp_for_type all ", argv(0), " ", argv(1), "\n");\r
635                                 }\r
636                         }\r
637                         fclose(fd);\r
638                 }\r
639         }\r
640 \r
641         WeaponStats_Init();\r
642 \r
643         addstat(STAT_WEAPONS, AS_INT, weapons);\r
644         addstat(STAT_SWITCHWEAPON, AS_INT, switchweapon);\r
645         addstat(STAT_GAMESTARTTIME, AS_FLOAT, stat_game_starttime);\r
646         addstat(STAT_ALLOW_OLDNEXBEAM, AS_INT, stat_allow_oldnexbeam);\r
647         Nagger_Init();\r
648 \r
649         addstat(STAT_STRENGTH_FINISHED, AS_FLOAT, strength_finished);\r
650         addstat(STAT_INVINCIBLE_FINISHED, AS_FLOAT, invincible_finished);\r
651         addstat(STAT_PRESSED_KEYS, AS_FLOAT, pressedkeys);\r
652         addstat(STAT_FUEL, AS_INT, ammo_fuel);\r
653         addstat(STAT_DAMAGE_HITS, AS_INT, stat_hit);\r
654         addstat(STAT_DAMAGE_FIRED, AS_INT, stat_fired);\r
655         addstat(STAT_SHOTORG, AS_INT, stat_shotorg);\r
656         addstat(STAT_LEADLIMIT, AS_FLOAT, stat_leadlimit);\r
657         addstat(STAT_WINNING, AS_FLOAT, winning);\r
658         addstat(STAT_VORE_LOAD, AS_INT, stat_stomachload);\r
659         addstat(STAT_VORE_DIGESTING, AS_INT, stat_digesting);\r
660         addstat(STAT_VORE_EATEN, AS_INT, stat_eaten);\r
661         addstat(STAT_VORE_CANLEAVE, AS_INT, stat_canleave);\r
662 \r
663         next_pingtime = time + 5;\r
664         InitializeEntity(self, cvar_changes_init, INITPRIO_CVARS);\r
665 \r
666         detect_maptype();\r
667 \r
668         lsmaps_reply = "^7Maps available: ";\r
669         lsnewmaps_reply = "^7Maps without a record set: ";\r
670         for(i = 0, j = 0; i < MapInfo_count; ++i)\r
671         {\r
672                 if(MapInfo_Get_ByID(i))\r
673                         if not(MapInfo_Map_flags & (MAPINFO_FLAG_HIDDEN | MAPINFO_FLAG_FORBIDDEN))\r
674                         {\r
675                                 if(mod(i, 2))\r
676                                         col = "^2";\r
677                                 else\r
678                                         col = "^3";\r
679                                 ++j;\r
680                                 lsmaps_reply = strcat(lsmaps_reply, col, MapInfo_Map_bspname, " ");\r
681                                 if(g_race && !stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, RACE_RECORD, "time"))))\r
682                                         lsnewmaps_reply = strcat(lsnewmaps_reply, col, MapInfo_Map_bspname, " ");\r
683                                 else if(g_cts && !stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, CTS_RECORD, "time"))))\r
684                                         lsnewmaps_reply = strcat(lsnewmaps_reply, col, MapInfo_Map_bspname, " ");\r
685                         }\r
686         }\r
687         lsmaps_reply = strzone(strcat(lsmaps_reply, "\n"));\r
688         if (!g_race && !g_cts)\r
689                 lsnewmaps_reply = "Need to be playing race or CTS for lsnewmaps to work.";\r
690         lsnewmaps_reply = strzone(strcat(lsnewmaps_reply, "\n"));\r
691 \r
692         maplist_reply = "^7Maps in list: ";\r
693         n = tokenize_console(cvar_string("g_maplist"));\r
694         for(i = 0, j = 0; i < n; ++i)\r
695         {\r
696                 if(MapInfo_CheckMap(argv(i)))\r
697                 {\r
698                         if(mod(j, 2))\r
699                                 col = "^2";\r
700                         else\r
701                                 col = "^3";\r
702                         maplist_reply = strcat(maplist_reply, col, argv(i), " ");\r
703                         ++j;\r
704                 }\r
705         }\r
706         maplist_reply = strzone(strcat(maplist_reply, "\n"));\r
707         MapInfo_ClearTemps();\r
708 \r
709         for(i = 0; i < 10; ++i)\r
710         {\r
711                 records_reply[i] = strzone(getrecords(i));\r
712         }\r
713 \r
714         rankings_reply = strzone(getrankings());\r
715 \r
716         ClientInit_Spawn();\r
717         RandomSeed_Spawn();\r
718         PingPLReport_Spawn();\r
719         OtherPLReport_Spawn();\r
720 \r
721         CheatInit();\r
722 \r
723         localcmd("\n_sv_hook_gamestart ", GetGametype(), ";");\r
724         if(cvar("g_campaign"))\r
725                 localcmd("\n_sv_hook_campaign_gamestart ", GetGametype(), ";");\r
726 \r
727         world_initialized = 1;\r
728 }\r
729 \r
730 void spawnfunc_light (void)\r
731 {\r
732         //makestatic (self); // Who the f___ did that?\r
733         remove(self);\r
734 }\r
735 \r
736 float TryFile( string pFilename )\r
737 {\r
738         local float lHandle;\r
739         dprint("TryFile(\"", pFilename, "\")\n");\r
740         lHandle = fopen( pFilename, FILE_READ );\r
741         if( lHandle != -1 ) {\r
742                 fclose( lHandle );\r
743                 return TRUE;\r
744         } else {\r
745                 return FALSE;\r
746         }\r
747 };\r
748 \r
749 string GetGametype()\r
750 {\r
751         return GametypeNameFromType(game);\r
752 }\r
753 \r
754 string getmapname_stored;\r
755 string GetMapname()\r
756 {\r
757         return mapname;\r
758 }\r
759 \r
760 float Map_Count, Map_Current;\r
761 string Map_Current_Name;\r
762 \r
763 // NOTE: this now expects the map list to be already tokenize()d and the count in Map_Count\r
764 float GetMaplistPosition()\r
765 {\r
766         float pos, idx;\r
767         string map;\r
768 \r
769         map = GetMapname();\r
770         idx = cvar("g_maplist_index");\r
771 \r
772         if(idx >= 0)\r
773                 if(idx < Map_Count)\r
774                         if(map == argv(idx))\r
775                                 return idx;\r
776 \r
777         for(pos = 0; pos < Map_Count; ++pos)\r
778                 if(map == argv(pos))\r
779                         return pos;\r
780 \r
781         // resume normal maplist rotation if current map is not in g_maplist\r
782         return idx;\r
783 }\r
784 \r
785 float MapHasRightSize(string map)\r
786 {\r
787         float fh;\r
788         if(currentbots || cvar("bot_number") || player_count < cvar("minplayers"))\r
789         if(cvar("g_maplist_check_waypoints"))\r
790         {\r
791                 dprint("checkwp "); dprint(map);\r
792                 fh = fopen(strcat("maps/", map, ".waypoints"), FILE_READ);\r
793                 if(fh < 0)\r
794                 {\r
795                         dprint(": no waypoints\n");\r
796                         return FALSE;\r
797                 }\r
798                 dprint(": has waypoints\n");\r
799                 fclose(fh);\r
800         }\r
801 \r
802         // open map size restriction file\r
803         dprint("opensize "); dprint(map);\r
804         fh = fopen(strcat("maps/", map, ".sizes"), FILE_READ);\r
805         if(fh >= 0)\r
806         {\r
807                 float mapmin, mapmax;\r
808                 dprint(": ok, ");\r
809                 mapmin = stof(fgets(fh));\r
810                 mapmax = stof(fgets(fh));\r
811                 fclose(fh);\r
812                 if(player_count < mapmin)\r
813                 {\r
814                         dprint("not enough\n");\r
815                         return FALSE;\r
816                 }\r
817                 if(player_count > mapmax)\r
818                 {\r
819                         dprint("too many\n");\r
820                         return FALSE;\r
821                 }\r
822                 dprint("right size\n");\r
823                 return TRUE;\r
824         }\r
825         dprint(": not found\n");\r
826         return TRUE;\r
827 }\r
828 \r
829 string Map_Filename(float position)\r
830 {\r
831         return strcat("maps/", argv(position), ".bsp");\r
832 }\r
833 \r
834 string strwords(string s, float w)\r
835 {\r
836         float endpos;\r
837         for(endpos = 0; w && endpos >= 0; --w)\r
838                 endpos = strstrofs(s, " ", endpos + 1);\r
839         if(endpos < 0)\r
840                 return s;\r
841         else\r
842                 return substring(s, 0, endpos);\r
843 }\r
844 \r
845 float strhasword(string s, string w)\r
846 {\r
847         return strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0;\r
848 }\r
849 \r
850 void Map_MarkAsRecent(string m)\r
851 {\r
852         cvar_set("g_maplist_mostrecent", strwords(strcat(m, " ", cvar_string("g_maplist_mostrecent")), max(0, cvar("g_maplist_mostrecent_count"))));\r
853 }\r
854 \r
855 float Map_IsRecent(string m)\r
856 {\r
857         return strhasword(cvar_string("g_maplist_mostrecent"), m);\r
858 }\r
859 \r
860 float Map_Check(float position, float pass)\r
861 {\r
862         string filename;\r
863         string map_next;\r
864         map_next = argv(position);\r
865         if(pass <= 1)\r
866         {\r
867                 if(Map_IsRecent(map_next))\r
868                         return 0;\r
869         }\r
870         filename = Map_Filename(position);\r
871         if(MapInfo_CheckMap(map_next))\r
872         {\r
873                 if(pass == 2)\r
874                         return 1;\r
875                 if(MapHasRightSize(map_next))\r
876                         return 1;\r
877                 return 0;\r
878         }\r
879         else\r
880                 dprint( "Couldn't select '", filename, "'..\n" );\r
881 \r
882         return 0;\r
883 }\r
884 \r
885 void Map_Goto_SetStr(string nextmapname)\r
886 {\r
887         if(getmapname_stored != "")\r
888                 strunzone(getmapname_stored);\r
889         if(nextmapname == "")\r
890                 getmapname_stored = "";\r
891         else\r
892                 getmapname_stored = strzone(nextmapname);\r
893 }\r
894 \r
895 void Map_Goto_SetFloat(float position)\r
896 {\r
897         cvar_set("g_maplist_index", ftos(position));\r
898         Map_Goto_SetStr(argv(position));\r
899 }\r
900 \r
901 void GameResetCfg()\r
902 {\r
903         // settings persist, except...\r
904         localcmd("\nsettemp_restore\n");\r
905 };\r
906 \r
907 void Map_Goto()\r
908 {\r
909         GameResetCfg();\r
910         MapInfo_LoadMap(getmapname_stored);\r
911 }\r
912 \r
913 // return codes of map selectors:\r
914 //   -1 = temporary failure (that is, try some method that is guaranteed to succeed)\r
915 //   -2 = permanent failure\r
916 float() MaplistMethod_Iterate = // usual method\r
917 {\r
918         float pass, i;\r
919 \r
920         for(pass = 1; pass <= 2; ++pass)\r
921         {\r
922                 for(i = 1; i < Map_Count; ++i)\r
923                 {\r
924                         float mapindex;\r
925                         mapindex = mod(i + Map_Current, Map_Count);\r
926                         if(Map_Check(mapindex, pass))\r
927                                 return mapindex;\r
928                 }\r
929         }\r
930         return -1;\r
931 }\r
932 \r
933 float() MaplistMethod_Repeat = // fallback method\r
934 {\r
935         if(Map_Check(Map_Current, 2))\r
936                 return Map_Current;\r
937         return -2;\r
938 }\r
939 \r
940 float() MaplistMethod_Random = // random map selection\r
941 {\r
942         float i, imax;\r
943 \r
944         imax = 42;\r
945 \r
946         for(i = 0; i <= imax; ++i)\r
947         {\r
948                 float mapindex;\r
949                 mapindex = mod(Map_Current + floor(random() * (Map_Count - 1) + 1), Map_Count); // any OTHER map\r
950                 if(Map_Check(mapindex, 1))\r
951                         return mapindex;\r
952         }\r
953         return -1;\r
954 }\r
955 \r
956 float(float exponent) MaplistMethod_Shuffle = // more clever shuffling\r
957 // the exponent sets a bias on the map selection:\r
958 // the higher the exponent, the less likely "shortly repeated" same maps are\r
959 {\r
960         float i, j, imax, insertpos;\r
961 \r
962         imax = 42;\r
963 \r
964         for(i = 0; i <= imax; ++i)\r
965         {\r
966                 string newlist;\r
967 \r
968                 // now reinsert this at another position\r
969                 insertpos = pow(random(), 1 / exponent);       // ]0, 1]\r
970                 insertpos = insertpos * (Map_Count - 1);       // ]0, Map_Count - 1]\r
971                 insertpos = ceil(insertpos) + 1;               // {2, 3, 4, ..., Map_Count}\r
972                 dprint("SHUFFLE: insert pos = ", ftos(insertpos), "\n");\r
973 \r
974                 // insert the current map there\r
975                 newlist = "";\r
976                 for(j = 1; j < insertpos; ++j)                 // i == 1: no loop, will be inserted as first; however, i == 1 has been excluded above\r
977                         newlist = strcat(newlist, " ", argv(j));\r
978                 newlist = strcat(newlist, " ", argv(0));       // now insert the just selected map\r
979                 for(j = insertpos; j < Map_Count; ++j)         // i == Map_Count: no loop, has just been inserted as last\r
980                         newlist = strcat(newlist, " ", argv(j));\r
981                 newlist = substring(newlist, 1, strlen(newlist) - 1);\r
982                 cvar_set("g_maplist", newlist);\r
983                 Map_Count = tokenizebyseparator(cvar_string("g_maplist"), " ");\r
984 \r
985                 // NOTE: the selected map has just been inserted at (insertpos-1)th position\r
986                 Map_Current = insertpos - 1; // this is not really valid, but this way the fallback has a chance of working\r
987                 if(Map_Check(Map_Current, 1))\r
988                         return Map_Current;\r
989         }\r
990         return -1;\r
991 }\r
992 \r
993 void Maplist_Init()\r
994 {\r
995         Map_Count = tokenizebyseparator(cvar_string("g_maplist"), " ");\r
996         if(Map_Count == 0)\r
997         {\r
998                 bprint( "Maplist is empty!  Resetting it to default map list.\n" );\r
999                 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));\r
1000                 if(cvar("g_maplist_shuffle"))\r
1001                         ShuffleMaplist();\r
1002                 localcmd("\nmenu_cmd sync\n");\r
1003                 Map_Count = tokenizebyseparator(cvar_string("g_maplist"), " ");\r
1004         }\r
1005         if(Map_Count == 0)\r
1006                 error("empty maplist, cannot select a new map");\r
1007         Map_Current = bound(0, GetMaplistPosition(), Map_Count - 1);\r
1008 \r
1009         if(Map_Current_Name)\r
1010                 strunzone(Map_Current_Name);\r
1011         Map_Current_Name = strzone(argv(Map_Current)); // will be automatically freed on exit thanks to DP\r
1012         // this may or may not be correct, but who cares, in the worst case a map\r
1013         // isn't chosen in the first pass that should have been\r
1014 }\r
1015 \r
1016 string GetNextMap()\r
1017 {\r
1018         float nextMap;\r
1019 \r
1020         Maplist_Init();\r
1021         nextMap = -1;\r
1022 \r
1023         if(nextMap == -1)\r
1024                 if(cvar("g_maplist_shuffle") > 0)\r
1025                         nextMap = MaplistMethod_Shuffle(cvar("g_maplist_shuffle") + 1);\r
1026 \r
1027         if(nextMap == -1)\r
1028                 if(cvar("g_maplist_selectrandom"))\r
1029                         nextMap = MaplistMethod_Random();\r
1030 \r
1031         if(nextMap == -1)\r
1032                 nextMap = MaplistMethod_Iterate();\r
1033 \r
1034         if(nextMap == -1)\r
1035                 nextMap = MaplistMethod_Repeat();\r
1036 \r
1037         if(nextMap >= 0)\r
1038         {\r
1039                 Map_Goto_SetFloat(nextMap);\r
1040                 return getmapname_stored;\r
1041         }\r
1042 \r
1043         return "";\r
1044 };\r
1045 \r
1046 float DoNextMapOverride()\r
1047 {\r
1048         if(cvar("g_campaign"))\r
1049         {\r
1050                 if(cvar("g_campaign_changelevel"))\r
1051                 {\r
1052                         CampaignPostIntermission();\r
1053                         alreadychangedlevel = TRUE;\r
1054                         return TRUE;\r
1055                 }\r
1056                 else\r
1057                         localcmd("togglemenu 1\n");\r
1058         }\r
1059         if(cvar("quit_when_empty"))\r
1060         {\r
1061                 if(player_count <= currentbots)\r
1062                 {\r
1063                         localcmd("quit\n");\r
1064                         alreadychangedlevel = TRUE;\r
1065                         return TRUE;\r
1066                 }\r
1067         }\r
1068         if(cvar_string("quit_and_redirect") != "")\r
1069         {\r
1070                 redirection_target = strzone(cvar_string("quit_and_redirect"));\r
1071                 alreadychangedlevel = TRUE;\r
1072                 return TRUE;\r
1073         }\r
1074         if (cvar("samelevel")) // if samelevel is set, stay on same level\r
1075         {\r
1076                 // 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)\r
1077                 //localcmd(strcat("exec \"maps/", mapname, ".mapcfg\"\n"));\r
1078                 // so instead just restart the current map using the restart command (DOES NOT WORK PROPERLY WITH exit_cfg STUFF)\r
1079                 localcmd("restart\n");\r
1080                 //changelevel (mapname);\r
1081                 alreadychangedlevel = TRUE;\r
1082                 return TRUE;\r
1083         }\r
1084         if(cvar_string("nextmap") != "")\r
1085                 if(MapInfo_CheckMap(cvar_string("nextmap")))\r
1086                 {\r
1087                         Map_Goto_SetStr(cvar_string("nextmap"));\r
1088                         Map_Goto();\r
1089                         alreadychangedlevel = TRUE;\r
1090                         return TRUE;\r
1091                 }\r
1092         if(cvar("lastlevel"))\r
1093         {\r
1094                 GameResetCfg();\r
1095                 localcmd("set lastlevel 0\ntogglemenu\n");\r
1096                 alreadychangedlevel = TRUE;\r
1097                 return TRUE;\r
1098         }\r
1099         return FALSE;\r
1100 };\r
1101 \r
1102 void GotoNextMap()\r
1103 {\r
1104         //local string nextmap;\r
1105         //local float n, nummaps;\r
1106         //local string s;\r
1107         if (alreadychangedlevel)\r
1108                 return;\r
1109         alreadychangedlevel = TRUE;\r
1110 \r
1111         {\r
1112                 string nextMap;\r
1113                 float allowReset;\r
1114 \r
1115                 for(allowReset = 1; allowReset >= 0; --allowReset)\r
1116                 {\r
1117                         nextMap = GetNextMap();\r
1118                         if(nextMap != "")\r
1119                                 break;\r
1120 \r
1121                         if(allowReset)\r
1122                         {\r
1123                                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );\r
1124                                 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));\r
1125                                 if(cvar("g_maplist_shuffle"))\r
1126                                         ShuffleMaplist();\r
1127                                 localcmd("\nmenu_cmd sync\n");\r
1128                         }\r
1129                         else\r
1130                         {\r
1131                                 error("Everything is broken - not even the default map list works. Please report this to the developers.");\r
1132                         }\r
1133                 }\r
1134                 Map_Goto();\r
1135         }\r
1136 };\r
1137 \r
1138 \r
1139 /*\r
1140 ============\r
1141 IntermissionThink\r
1142 \r
1143 When the player presses attack or jump, change to the next level\r
1144 ============\r
1145 */\r
1146 .float autoscreenshot;\r
1147 void() MapVote_Start;\r
1148 void() MapVote_Think;\r
1149 float mapvote_initialized;\r
1150 void IntermissionThink()\r
1151 {\r
1152         FixIntermissionClient(self);\r
1153 \r
1154         if(cvar("sv_autoscreenshot"))\r
1155         if(self.autoscreenshot > 0)\r
1156         if(time > self.autoscreenshot)\r
1157         {\r
1158                 self.autoscreenshot = -1;\r
1159                 if(clienttype(self) == CLIENTTYPE_REAL)\r
1160                         stuffcmd(self, "\nscreenshot\necho \"^5A screenshot has been taken at request of the server.\"\n");\r
1161                 return;\r
1162         }\r
1163 \r
1164         if (time < intermission_exittime)\r
1165                 return;\r
1166 \r
1167         if(!mapvote_initialized)\r
1168                 if (time < intermission_exittime + 10 && !self.BUTTON_ATCK && !self.BUTTON_JUMP && !self.BUTTON_ATCK2 && !self.BUTTON_JETPACK && !self.BUTTON_USE)\r
1169                         return;\r
1170 \r
1171         MapVote_Start();\r
1172 };\r
1173 \r
1174 /*\r
1175 ============\r
1176 FindIntermission\r
1177 \r
1178 Returns the entity to view from\r
1179 ============\r
1180 */\r
1181 /*\r
1182 entity FindIntermission()\r
1183 {\r
1184         local   entity spot;\r
1185         local   float cyc;\r
1186 \r
1187 // look for info_intermission first\r
1188         spot = find (world, classname, "info_intermission");\r
1189         if (spot)\r
1190         {       // pick a random one\r
1191                 cyc = random() * 4;\r
1192                 while (cyc > 1)\r
1193                 {\r
1194                         spot = find (spot, classname, "info_intermission");\r
1195                         if (!spot)\r
1196                                 spot = find (spot, classname, "info_intermission");\r
1197                         cyc = cyc - 1;\r
1198                 }\r
1199                 return spot;\r
1200         }\r
1201 \r
1202 // then look for the start position\r
1203         spot = find (world, classname, "info_player_start");\r
1204         if (spot)\r
1205                 return spot;\r
1206 \r
1207 // testinfo_player_start is only found in regioned levels\r
1208         spot = find (world, classname, "testplayerstart");\r
1209         if (spot)\r
1210                 return spot;\r
1211 \r
1212 // then look for the start position\r
1213         spot = find (world, classname, "info_player_deathmatch");\r
1214         if (spot)\r
1215                 return spot;\r
1216 \r
1217         //objerror ("FindIntermission: no spot");\r
1218         return world;\r
1219 };\r
1220 */\r
1221 \r
1222 /*\r
1223 ===============================================================================\r
1224 \r
1225 RULES\r
1226 \r
1227 ===============================================================================\r
1228 */\r
1229 \r
1230 void DumpStats(float final)\r
1231 {\r
1232         local float file;\r
1233         local string s;\r
1234         local float to_console;\r
1235         local float to_eventlog;\r
1236         local float to_file;\r
1237         local float i;\r
1238 \r
1239         to_console = cvar("sv_logscores_console");\r
1240         to_eventlog = cvar("sv_eventlog");\r
1241         to_file = cvar("sv_logscores_file");\r
1242 \r
1243         if(!final)\r
1244         {\r
1245                 to_console = TRUE; // always print printstats replies\r
1246                 to_eventlog = FALSE; // but never print them to the event log\r
1247         }\r
1248 \r
1249         if(to_eventlog)\r
1250                 if(cvar("sv_eventlog_console"))\r
1251                         to_console = FALSE; // otherwise we get the output twice\r
1252 \r
1253         if(final)\r
1254                 s = ":scores:";\r
1255         else\r
1256                 s = ":status:";\r
1257         s = strcat(s, GetGametype(), "_", GetMapname(), ":", ftos(rint(time)));\r
1258 \r
1259         if(to_console)\r
1260                 print(s, "\n");\r
1261         if(to_eventlog)\r
1262                 GameLogEcho(s);\r
1263         if(to_file)\r
1264         {\r
1265                 file = fopen(cvar_string("sv_logscores_filename"), FILE_APPEND);\r
1266                 if(file == -1)\r
1267                         to_file = FALSE;\r
1268                 else\r
1269                         fputs(file, strcat(s, "\n"));\r
1270         }\r
1271 \r
1272         s = strcat(":labels:player:", GetPlayerScoreString(world, 0));\r
1273         if(to_console)\r
1274                 print(s, "\n");\r
1275         if(to_eventlog)\r
1276                 GameLogEcho(s);\r
1277         if(to_file)\r
1278                 fputs(file, strcat(s, "\n"));\r
1279 \r
1280         FOR_EACH_CLIENT(other)\r
1281         {\r
1282                 if ((clienttype(other) == CLIENTTYPE_REAL) || (clienttype(other) == CLIENTTYPE_BOT && cvar("sv_logscores_bots")))\r
1283                 {\r
1284                         s = strcat(":player:see-labels:", GetPlayerScoreString(other, 0), ":");\r
1285                         s = strcat(s, ftos(rint(time - other.jointime)), ":");\r
1286                         if(other.classname == "player" || g_arena || g_ca || g_lms)\r
1287                                 s = strcat(s, ftos(other.team), ":");\r
1288                         else\r
1289                                 s = strcat(s, "spectator:");\r
1290 \r
1291                         if(to_console)\r
1292                                 print(s, other.netname, "\n");\r
1293                         if(to_eventlog)\r
1294                                 GameLogEcho(strcat(s, ftos(other.playerid), ":", other.netname));\r
1295                         if(to_file)\r
1296                                 fputs(file, strcat(s, other.netname, "\n"));\r
1297                 }\r
1298         }\r
1299 \r
1300         if(teams_matter)\r
1301         {\r
1302                 s = strcat(":labels:teamscores:", GetTeamScoreString(0, 0));\r
1303                 if(to_console)\r
1304                         print(s, "\n");\r
1305                 if(to_eventlog)\r
1306                         GameLogEcho(s);\r
1307                 if(to_file)\r
1308                         fputs(file, strcat(s, "\n"));\r
1309 \r
1310                 for(i = 1; i < 16; ++i)\r
1311                 {\r
1312                         s = strcat(":teamscores:see-labels:", GetTeamScoreString(i, 0));\r
1313                         s = strcat(s, ":", ftos(i));\r
1314                         if(to_console)\r
1315                                 print(s, "\n");\r
1316                         if(to_eventlog)\r
1317                                 GameLogEcho(s);\r
1318                         if(to_file)\r
1319                                 fputs(file, strcat(s, "\n"));\r
1320                 }\r
1321         }\r
1322 \r
1323         if(to_console)\r
1324                 print(":end\n");\r
1325         if(to_eventlog)\r
1326                 GameLogEcho(":end");\r
1327         if(to_file)\r
1328         {\r
1329                 fputs(file, ":end\n");\r
1330                 fclose(file);\r
1331         }\r
1332 }\r
1333 \r
1334 void FixIntermissionClient(entity e)\r
1335 {\r
1336         string s;\r
1337         if(!e.autoscreenshot) // initial call\r
1338         {\r
1339                 e.angles = e.v_angle;\r
1340                 e.angles_x = -e.angles_x;\r
1341                 e.autoscreenshot = time + 0.8;  // used for autoscreenshot\r
1342                 e.health = -2342;\r
1343                 // first intermission phase; voting phase has positive health (used to decide whether to send SVC_FINALE or not)\r
1344                 e.solid = SOLID_NOT;\r
1345                 e.movetype = MOVETYPE_NONE;\r
1346                 e.takedamage = DAMAGE_NO;\r
1347                 if(e.weaponentity)\r
1348                 {\r
1349                         e.weaponentity.effects = EF_NODRAW;\r
1350                         if (e.weaponentity.weaponentity)\r
1351                                 e.weaponentity.weaponentity.effects = EF_NODRAW;\r
1352                 }\r
1353                 if(clienttype(e) == CLIENTTYPE_REAL)\r
1354                 {\r
1355                         stuffcmd(e, "\nscr_printspeed 1000000\n");\r
1356                         if(e.winning)\r
1357                                 s = cvar_string("sv_intermission_cdtrack_win");\r
1358                         else\r
1359                                 s = cvar_string("sv_intermission_cdtrack_loose");\r
1360                         if(s != "")\r
1361                                 stuffcmd(e, strcat("\ncd loop ", s, "\n"));\r
1362                         msg_entity = e;\r
1363                         WriteByte(MSG_ONE, SVC_INTERMISSION);\r
1364                 }\r
1365         }\r
1366 \r
1367         //e.velocity = '0 0 0';\r
1368         //e.fixangle = TRUE;\r
1369 \r
1370         // TODO halt weapon animation\r
1371 }\r
1372 \r
1373 \r
1374 /*\r
1375 go to the next level for deathmatch\r
1376 only called if a time or frag limit has expired\r
1377 */\r
1378 void NextLevel()\r
1379 {\r
1380         float i;\r
1381 \r
1382         gameover = TRUE;\r
1383 \r
1384         intermission_running = 1;\r
1385 \r
1386 // enforce a wait time before allowing changelevel\r
1387         if(player_count > 0)\r
1388                 intermission_exittime = time + cvar("sv_mapchange_delay");\r
1389         else\r
1390                 intermission_exittime = -1;\r
1391 \r
1392         /*\r
1393         WriteByte (MSG_ALL, SVC_CDTRACK);\r
1394         WriteByte (MSG_ALL, 3);\r
1395         WriteByte (MSG_ALL, 3);\r
1396         // done in FixIntermission\r
1397         */\r
1398 \r
1399         //pos = FindIntermission ();\r
1400 \r
1401         VoteReset();\r
1402 \r
1403         DumpStats(TRUE);\r
1404 \r
1405         if(cvar("sv_eventlog"))\r
1406                 GameLogEcho(":gameover");\r
1407 \r
1408         GameLogClose();\r
1409 \r
1410 // TO DO\r
1411 \r
1412 // save the stats to a text file on the client\r
1413 // stuffcmd(other, log_stats "stats/file_name");\r
1414 // bprint stats\r
1415 // stuffcmd(other, log_stats "");\r
1416 // use a filename similar to the demo name\r
1417         // string file_name;\r
1418         // file_name = strcat("\nlog_file \"stats/", strftime(TRUE, "%Y-%m-%d_%H-%M"), "_", mapname, ".txt\"");  // open the log file\r
1419 \r
1420 // write a stats parser for the menu\r
1421 \r
1422         if(cvar("sv_accuracy_data_send")) {\r
1423                 string stats_to_send;\r
1424 \r
1425                 FOR_EACH_CLIENT(other) {  // make the string to send\r
1426                         FixIntermissionClient(other);\r
1427 \r
1428                         if(other.cvar_cl_accuracy_data_share) {\r
1429                                 stats_to_send = strcat(stats_to_send, ":hits:", other.netname);\r
1430 \r
1431                                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)\r
1432                                         stats_to_send = strcat(stats_to_send, ":", ftos(other.stats_hit[i-1]));\r
1433 \r
1434                                 stats_to_send = strcat(stats_to_send, "\n:fired:", other.netname);\r
1435 \r
1436                                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)\r
1437                                         stats_to_send = strcat(stats_to_send, ":", ftos(other.stats_fired[i-1]));\r
1438 \r
1439                                 stats_to_send = strcat(stats_to_send, "\n");\r
1440                         }\r
1441                 }\r
1442 \r
1443                 FOR_EACH_REALCLIENT(other) {  // only spam humans\r
1444                         Score_NicePrint(other);  // print the score\r
1445 \r
1446                         if(other.cvar_cl_accuracy_data_receive)  // send the stats string to all the willing clients\r
1447                                 bprint(stats_to_send);\r
1448                 }\r
1449         } else { // ye olde message\r
1450                 FOR_EACH_PLAYER(other) {\r
1451                         FixIntermissionClient(other);\r
1452 \r
1453                         if(other.winning)\r
1454                                 bprint(other.netname, " ^7wins.\n");\r
1455                 }\r
1456         }\r
1457 \r
1458         if(cvar("g_campaign"))\r
1459                 CampaignPreIntermission();\r
1460 \r
1461         localcmd("\nsv_hook_gameend;");\r
1462         if(cvar("g_campaign"))\r
1463                 localcmd("\nsv_hook_campaign_gameend;");\r
1464 }\r
1465 \r
1466 /*\r
1467 ============\r
1468 CheckRules_Player\r
1469 \r
1470 Exit deathmatch games upon conditions\r
1471 ============\r
1472 */\r
1473 void CheckRules_Player()\r
1474 {\r
1475         if (gameover)   // someone else quit the game already\r
1476                 return;\r
1477 \r
1478         if(self.deadflag == DEAD_NO)\r
1479                 self.play_time += frametime;\r
1480 \r
1481         // fixme: don't check players; instead check spawnfunc_dom_team and spawnfunc_ctf_team entities\r
1482         //   (div0: and that in CheckRules_World please)\r
1483 };\r
1484 \r
1485 float checkrules_equality;\r
1486 float checkrules_suddendeathwarning;\r
1487 float checkrules_suddendeathend;\r
1488 float checkrules_overtimesadded; //how many overtimes have been already added\r
1489 \r
1490 float WINNING_NO = 0; // no winner, but time limits may terminate the game\r
1491 float WINNING_YES = 1; // winner found\r
1492 float WINNING_NEVER = 2; // no winner, enter overtime if time limit is reached\r
1493 float WINNING_STARTSUDDENDEATHOVERTIME = 3; // no winner, enter suddendeath overtime NOW\r
1494 \r
1495 float InitiateSuddenDeath()\r
1496 {\r
1497         // Check first whether normal overtimes could be added before initiating suddendeath mode\r
1498         // - for this timelimit_overtime needs to be >0 of course\r
1499         // - also check the winning condition calculated in the previous frame and only add normal overtime\r
1500         //   again, if at the point at which timelimit would be extended again, still no winner was found\r
1501         if ((checkrules_overtimesadded >= 0) && (checkrules_overtimesadded < cvar("timelimit_overtimes")) && cvar("timelimit_overtime") && !(g_race && !g_race_qualifying))\r
1502         {\r
1503                 return 1; // need to call InitiateOvertime later\r
1504         }\r
1505         else\r
1506         {\r
1507                 if(!checkrules_suddendeathend)\r
1508                 {\r
1509                         checkrules_suddendeathend = time + 60 * cvar("timelimit_suddendeath");\r
1510                         if(g_race && !g_race_qualifying)\r
1511                                 race_StartCompleting();\r
1512                 }\r
1513                 return 0;\r
1514         }\r
1515 }\r
1516 \r
1517 void InitiateOvertime() // ONLY call this if InitiateSuddenDeath returned true\r
1518 {\r
1519         ++checkrules_overtimesadded;\r
1520         //add one more overtime by simply extending the timelimit\r
1521         float tl;\r
1522         tl = cvar("timelimit");\r
1523         tl += cvar("timelimit_overtime");\r
1524         cvar_set("timelimit", ftos(tl));\r
1525         string minutesPlural;\r
1526         if (cvar("timelimit_overtime") == 1)\r
1527                 minutesPlural = " ^3minute";\r
1528         else\r
1529                 minutesPlural = " ^3minutes";\r
1530 \r
1531         bcenterprint(\r
1532                 strcat(\r
1533                         "^3Now playing ^1OVERTIME^3!\n\n^3Added ^1",\r
1534                         ftos(cvar("timelimit_overtime")),\r
1535                         minutesPlural,\r
1536                         " to the game!"\r
1537                 )\r
1538         );\r
1539 }\r
1540 \r
1541 float GetWinningCode(float fraglimitreached, float equality)\r
1542 {\r
1543         if(equality)\r
1544                 if(fraglimitreached)\r
1545                         return WINNING_STARTSUDDENDEATHOVERTIME;\r
1546                 else\r
1547                         return WINNING_NEVER;\r
1548         else\r
1549                 if(fraglimitreached)\r
1550                         return WINNING_YES;\r
1551                 else\r
1552                         return WINNING_NO;\r
1553 }\r
1554 \r
1555 // set the .winning flag for exactly those players with a given field value\r
1556 void SetWinners(.float field, float value)\r
1557 {\r
1558         entity head;\r
1559         FOR_EACH_PLAYER(head)\r
1560                 head.winning = (head.field == value);\r
1561 }\r
1562 \r
1563 // set the .winning flag for those players with a given field value\r
1564 void AddWinners(.float field, float value)\r
1565 {\r
1566         entity head;\r
1567         FOR_EACH_PLAYER(head)\r
1568                 if(head.field == value)\r
1569                         head.winning = 1;\r
1570 }\r
1571 \r
1572 // clear the .winning flags\r
1573 void ClearWinners(void)\r
1574 {\r
1575         entity head;\r
1576         FOR_EACH_PLAYER(head)\r
1577                 head.winning = 0;\r
1578 }\r
1579 \r
1580 // Onslaught winning condition:\r
1581 // game terminates if only one team has a working generator (or none)\r
1582 float WinningCondition_Onslaught()\r
1583 {\r
1584         entity head;\r
1585         local float t1, t2, t3, t4;\r
1586 \r
1587         WinningConditionHelper(); // set worldstatus\r
1588 \r
1589         if(inWarmupStage)\r
1590                 return WINNING_NO;\r
1591 \r
1592         // first check if the game has ended\r
1593         t1 = t2 = t3 = t4 = 0;\r
1594         head = find(world, classname, "onslaught_generator");\r
1595         while (head)\r
1596         {\r
1597                 if (head.health > 0)\r
1598                 {\r
1599                         if (head.team == COLOR_TEAM1) t1 = 1;\r
1600                         if (head.team == COLOR_TEAM2) t2 = 1;\r
1601                         if (head.team == COLOR_TEAM3) t3 = 1;\r
1602                         if (head.team == COLOR_TEAM4) t4 = 1;\r
1603                 }\r
1604                 head = find(head, classname, "onslaught_generator");\r
1605         }\r
1606         if (t1 + t2 + t3 + t4 < 2)\r
1607         {\r
1608                 // game over, only one team remains (or none)\r
1609                 ClearWinners();\r
1610                 if (t1) SetWinners(team, COLOR_TEAM1);\r
1611                 if (t2) SetWinners(team, COLOR_TEAM2);\r
1612                 if (t3) SetWinners(team, COLOR_TEAM3);\r
1613                 if (t4) SetWinners(team, COLOR_TEAM4);\r
1614                 dprint("Have a winner, ending game.\n");\r
1615                 return WINNING_YES;\r
1616         }\r
1617 \r
1618         // Two or more teams remain\r
1619         return WINNING_NO;\r
1620 }\r
1621 \r
1622 float LMS_NewPlayerLives()\r
1623 {\r
1624         float fl;\r
1625         fl = cvar("fraglimit");\r
1626         if(fl == 0)\r
1627                 fl = 999;\r
1628 \r
1629         // first player has left the game for dying too much? Nobody else can get in.\r
1630         if(lms_lowest_lives < 1)\r
1631                 return 0;\r
1632 \r
1633         if(!cvar("g_lms_join_anytime"))\r
1634                 if(lms_lowest_lives < fl - cvar("g_lms_last_join"))\r
1635                         return 0;\r
1636 \r
1637         return bound(1, lms_lowest_lives, fl);\r
1638 }\r
1639 \r
1640 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)\r
1641 // they win. Otherwise the defending team wins once the timelimit passes.\r
1642 void assault_new_round();\r
1643 float WinningCondition_Assault()\r
1644 {\r
1645         local float status;\r
1646 \r
1647         WinningConditionHelper(); // set worldstatus\r
1648 \r
1649         status = WINNING_NO;\r
1650         // as the timelimit has not yet passed just assume the defending team will win\r
1651         if(assault_attacker_team == COLOR_TEAM1)\r
1652         {\r
1653                 SetWinners(team, COLOR_TEAM2);\r
1654         }\r
1655         else\r
1656         {\r
1657                 SetWinners(team, COLOR_TEAM1);\r
1658         }\r
1659 \r
1660         local entity ent;\r
1661         ent = find(world, classname, "target_assault_roundend");\r
1662         if(ent)\r
1663         {\r
1664                 if(ent.winning) // round end has been triggered by attacking team\r
1665                 {\r
1666                         bprint("ASSAULT: round completed...\n");\r
1667                         SetWinners(team, assault_attacker_team);\r
1668 \r
1669                         TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));\r
1670 \r
1671                         if(ent.cnt == 1) // this was the second round\r
1672                         {\r
1673                                 status = WINNING_YES;\r
1674                         }\r
1675                         else\r
1676                         {\r
1677                                 local entity oldself;\r
1678                                 oldself = self;\r
1679                                 self = ent;\r
1680                                 assault_new_round();\r
1681                                 self = oldself;\r
1682                         }\r
1683                 }\r
1684         }\r
1685 \r
1686         return status;\r
1687 }\r
1688 \r
1689 // LMS winning condition: game terminates if and only if there's at most one\r
1690 // one player who's living lives. Top two scores being equal cancels the time\r
1691 // limit.\r
1692 float WinningCondition_LMS()\r
1693 {\r
1694         entity head, head2;\r
1695         float have_player;\r
1696         float have_players;\r
1697         float l;\r
1698 \r
1699         have_player = FALSE;\r
1700         have_players = FALSE;\r
1701         l = LMS_NewPlayerLives();\r
1702 \r
1703         head = find(world, classname, "player");\r
1704         if(head)\r
1705                 have_player = TRUE;\r
1706         head2 = find(head, classname, "player");\r
1707         if(head2)\r
1708                 have_players = TRUE;\r
1709 \r
1710         if(have_player)\r
1711         {\r
1712                 // we have at least one player\r
1713                 if(have_players)\r
1714                 {\r
1715                         // two or more active players - continue with the game\r
1716                 }\r
1717                 else\r
1718                 {\r
1719                         // exactly one player?\r
1720 \r
1721                         ClearWinners();\r
1722                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out\r
1723 \r
1724                         if(l)\r
1725                         {\r
1726                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue\r
1727                                 return WINNING_NO;\r
1728                         }\r
1729                         else\r
1730                         {\r
1731                                 // a winner!\r
1732                                 // and assign him his first place\r
1733                                 PlayerScore_Add(head, SP_LMS_RANK, 1);\r
1734                                 return WINNING_YES;\r
1735                         }\r
1736                 }\r
1737         }\r
1738         else\r
1739         {\r
1740                 // nobody is playing at all...\r
1741                 if(l)\r
1742                 {\r
1743                         // wait for players...\r
1744                 }\r
1745                 else\r
1746                 {\r
1747                         // SNAFU (maybe a draw game?)\r
1748                         ClearWinners();\r
1749                         dprint("No players, ending game.\n");\r
1750                         return WINNING_YES;\r
1751                 }\r
1752         }\r
1753 \r
1754         // When we get here, we have at least two players who are actually LIVING,\r
1755         // now check if the top two players have equal score.\r
1756         WinningConditionHelper();\r
1757 \r
1758         ClearWinners();\r
1759         if(WinningConditionHelper_winner)\r
1760                 WinningConditionHelper_winner.winning = TRUE;\r
1761         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)\r
1762                 return WINNING_NEVER;\r
1763 \r
1764         // Top two have different scores? Way to go for our beloved TIMELIMIT!\r
1765         return WINNING_NO;\r
1766 }\r
1767 \r
1768 void ShuffleMaplist()\r
1769 {\r
1770         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));\r
1771 }\r
1772 \r
1773 float leaderfrags;\r
1774 float WinningCondition_Scores(float limit, float leadlimit)\r
1775 {\r
1776         // TODO make everything use THIS winning condition (except LMS)\r
1777         WinningConditionHelper();\r
1778 \r
1779         if(teams_matter)\r
1780         {\r
1781                 team1_score = TeamScore_GetCompareValue(COLOR_TEAM1);\r
1782                 team2_score = TeamScore_GetCompareValue(COLOR_TEAM2);\r
1783                 team3_score = TeamScore_GetCompareValue(COLOR_TEAM3);\r
1784                 team4_score = TeamScore_GetCompareValue(COLOR_TEAM4);\r
1785         }\r
1786 \r
1787         ClearWinners();\r
1788         if(WinningConditionHelper_winner)\r
1789                 WinningConditionHelper_winner.winning = 1;\r
1790         if(WinningConditionHelper_winnerteam >= 0)\r
1791                 SetWinners(team, WinningConditionHelper_winnerteam);\r
1792 \r
1793         if(WinningConditionHelper_lowerisbetter)\r
1794         {\r
1795                 WinningConditionHelper_topscore = -WinningConditionHelper_topscore;\r
1796                 WinningConditionHelper_secondscore = -WinningConditionHelper_secondscore;\r
1797                 limit = -limit;\r
1798         }\r
1799 \r
1800         if(WinningConditionHelper_zeroisworst)\r
1801                 leadlimit = 0; // not supported in this mode\r
1802 \r
1803         if(g_dm || g_tdm || g_arena || g_ca || (g_race && !g_race_qualifying))\r
1804         // these modes always score in increments of 1, thus this makes sense\r
1805         {\r
1806                 if(leaderfrags != WinningConditionHelper_topscore)\r
1807                 {\r
1808                         leaderfrags = WinningConditionHelper_topscore;\r
1809 \r
1810                         if (limit)\r
1811                         if (leaderfrags == limit - 1)\r
1812                                 Announce("1fragleft");\r
1813                         else if (leaderfrags == limit - 2)\r
1814                                 Announce("2fragsleft");\r
1815                         else if (leaderfrags == limit - 3)\r
1816                                 Announce("3fragsleft");\r
1817                 }\r
1818         }\r
1819 \r
1820         return GetWinningCode(\r
1821                 WinningConditionHelper_topscore &&\r
1822                 (\r
1823                         (limit && (WinningConditionHelper_topscore >= limit))\r
1824                         ||\r
1825                         (leadlimit && (WinningConditionHelper_topscore - WinningConditionHelper_secondscore >= leadlimit))\r
1826                 ),\r
1827                 WinningConditionHelper_equality\r
1828         );\r
1829 }\r
1830 \r
1831 float WinningCondition_Race(float fraglimit)\r
1832 {\r
1833         float wc;\r
1834         entity p;\r
1835         float n, c;\r
1836 \r
1837         n = 0;\r
1838         c = 0;\r
1839         FOR_EACH_PLAYER(p)\r
1840         {\r
1841                 ++n;\r
1842                 if(p.race_completed)\r
1843                         ++c;\r
1844         }\r
1845         if(n && (n == c))\r
1846                 return WINNING_YES;\r
1847         wc = WinningCondition_Scores(fraglimit, 0);\r
1848 \r
1849         // ALWAYS initiate overtime, unless EVERYONE has finished the race!\r
1850         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)\r
1851         // do NOT support equality when the laps are all raced!\r
1852                 return WINNING_STARTSUDDENDEATHOVERTIME;\r
1853         else\r
1854                 return WINNING_NEVER;\r
1855         return wc;\r
1856 }\r
1857 \r
1858 void ReadyRestart();\r
1859 float WinningCondition_QualifyingThenRace(float limit)\r
1860 {\r
1861         float wc;\r
1862         wc = WinningCondition_Scores(limit, 0);\r
1863 \r
1864         // NEVER initiate overtime\r
1865         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)\r
1866         {\r
1867                 return WINNING_YES;\r
1868         }\r
1869 \r
1870         return wc;\r
1871 }\r
1872 \r
1873 float WinningCondition_RanOutOfSpawns()\r
1874 {\r
1875         entity head;\r
1876 \r
1877         if(have_team_spawns <= 0)\r
1878                 return WINNING_NO;\r
1879 \r
1880         if(!some_spawn_has_been_used)\r
1881                 return WINNING_NO;\r
1882 \r
1883         team1_score = team2_score = team3_score = team4_score = 0;\r
1884 \r
1885         FOR_EACH_PLAYER(head) if(head.deadflag == DEAD_NO)\r
1886         {\r
1887                 if(head.team == COLOR_TEAM1)\r
1888                         team1_score = 1;\r
1889                 else if(head.team == COLOR_TEAM2)\r
1890                         team2_score = 1;\r
1891                 else if(head.team == COLOR_TEAM3)\r
1892                         team3_score = 1;\r
1893                 else if(head.team == COLOR_TEAM4)\r
1894                         team4_score = 1;\r
1895         }\r
1896 \r
1897         for(head = world; (head = find(head, classname, "info_player_deathmatch")) != world; )\r
1898         {\r
1899                 if(head.team == COLOR_TEAM1)\r
1900                         team1_score = 1;\r
1901                 else if(head.team == COLOR_TEAM2)\r
1902                         team2_score = 1;\r
1903                 else if(head.team == COLOR_TEAM3)\r
1904                         team3_score = 1;\r
1905                 else if(head.team == COLOR_TEAM4)\r
1906                         team4_score = 1;\r
1907         }\r
1908 \r
1909         ClearWinners();\r
1910         if(team1_score + team2_score + team3_score + team4_score == 0)\r
1911         {\r
1912                 checkrules_equality = TRUE;\r
1913                 return WINNING_YES;\r
1914         }\r
1915         else if(team1_score + team2_score + team3_score + team4_score == 1)\r
1916         {\r
1917                 float t, i;\r
1918                 if(team1_score) t = COLOR_TEAM1;\r
1919                 if(team2_score) t = COLOR_TEAM2;\r
1920                 if(team3_score) t = COLOR_TEAM3;\r
1921                 if(team4_score) t = COLOR_TEAM4;\r
1922                 CheckAllowedTeams(world);\r
1923                 for(i = 0; i < MAX_TEAMSCORE; ++i)\r
1924                 {\r
1925                         if(t != COLOR_TEAM1) if(c1 >= 0) TeamScore_AddToTeam(COLOR_TEAM1, i, -1000);\r
1926                         if(t != COLOR_TEAM2) if(c2 >= 0) TeamScore_AddToTeam(COLOR_TEAM2, i, -1000);\r
1927                         if(t != COLOR_TEAM3) if(c3 >= 0) TeamScore_AddToTeam(COLOR_TEAM3, i, -1000);\r
1928                         if(t != COLOR_TEAM4) if(c4 >= 0) TeamScore_AddToTeam(COLOR_TEAM4, i, -1000);\r
1929                 }\r
1930 \r
1931                 AddWinners(team, t);\r
1932                 return WINNING_YES;\r
1933         }\r
1934         else\r
1935                 return WINNING_NO;\r
1936 }\r
1937 \r
1938 /*\r
1939 ============\r
1940 CheckRules_World\r
1941 \r
1942 Exit deathmatch games upon conditions\r
1943 ============\r
1944 */\r
1945 void CheckRules_World()\r
1946 {\r
1947         float timelimit;\r
1948         float fraglimit;\r
1949         float leadlimit;\r
1950 \r
1951         VoteThink();\r
1952         MapVote_Think();\r
1953 \r
1954         SetDefaultAlpha();\r
1955 \r
1956         /*\r
1957         MapVote_Think should now do that part\r
1958         if (intermission_running)\r
1959                 if (time >= intermission_exittime + 60)\r
1960                 {\r
1961                         if(!DoNextMapOverride())\r
1962                                 GotoNextMap();\r
1963                         return;\r
1964                 }\r
1965         */\r
1966 \r
1967         if (gameover)   // someone else quit the game already\r
1968         {\r
1969                 if(player_count == 0) // Nobody there? Then let's go to the next map\r
1970                         MapVote_Start();\r
1971                         // this will actually check the player count in the next frame\r
1972                         // again, but this shouldn't hurt\r
1973                 return;\r
1974         }\r
1975 \r
1976         timelimit = cvar("timelimit") * 60;\r
1977         fraglimit = cvar("fraglimit");\r
1978         leadlimit = cvar("leadlimit");\r
1979 \r
1980         if(inWarmupStage || time <= game_starttime) // NOTE: this is <= to prevent problems in the very tic where the game starts\r
1981         {\r
1982                 if(timelimit > 0)\r
1983                         timelimit = 0; // timelimit is not made for warmup\r
1984                 if(fraglimit > 0)\r
1985                         fraglimit = 0; // no fraglimit for now\r
1986                 leadlimit = 0; // no leadlimit for now\r
1987         }\r
1988 \r
1989         if(g_onslaught)\r
1990                 timelimit = 0; // ONS has its own overtime rule\r
1991 \r
1992         if(timelimit > 0)\r
1993         {\r
1994                 timelimit += game_starttime;\r
1995         }\r
1996         else if (timelimit < 0)\r
1997         {\r
1998                 // endmatch\r
1999                 NextLevel();\r
2000                 return;\r
2001         }\r
2002 \r
2003         float wantovertime;\r
2004         wantovertime = 0;\r
2005 \r
2006         if(checkrules_suddendeathend)\r
2007         {\r
2008                 if(!checkrules_suddendeathwarning)\r
2009                 {\r
2010                         checkrules_suddendeathwarning = TRUE;\r
2011                         if(g_race && !g_race_qualifying)\r
2012                                 bcenterprint("^3Everyone, finish your lap! The race is over!");\r
2013                         else\r
2014                                 bcenterprint("^3Now playing ^1OVERTIME^3!\n\n^3Keep fragging until we have a ^1winner^3!");\r
2015                 }\r
2016         }\r
2017         else\r
2018         {\r
2019                 if (timelimit && time >= timelimit)\r
2020                 {\r
2021                         if(g_race && (g_race_qualifying == 2) && timelimit > 0)\r
2022                         {\r
2023                                 float totalplayers;\r
2024                                 float playerswithlaps;\r
2025                                 float readyplayers;\r
2026                                 entity head;\r
2027                                 totalplayers = playerswithlaps = readyplayers = 0;\r
2028                                 FOR_EACH_PLAYER(head)\r
2029                                 {\r
2030                                         ++totalplayers;\r
2031                                         if(PlayerScore_Add(head, SP_RACE_FASTEST, 0))\r
2032                                                 ++playerswithlaps;\r
2033                                         if(head.ready)\r
2034                                                 ++readyplayers;\r
2035                                 }\r
2036 \r
2037                                 // at least 2 of the players have completed a lap: start the RACE\r
2038                                 // otherwise, the players should end the qualifying on their own\r
2039                                 if(readyplayers || playerswithlaps >= 2)\r
2040                                 {\r
2041                                         checkrules_suddendeathend = 0;\r
2042                                         ReadyRestart(); // go to race\r
2043                                         return;\r
2044                                 }\r
2045                                 else\r
2046                                         wantovertime |= InitiateSuddenDeath();\r
2047                         }\r
2048                         else\r
2049                                 wantovertime |= InitiateSuddenDeath();\r
2050                 }\r
2051         }\r
2052 \r
2053         if (checkrules_suddendeathend && time >= checkrules_suddendeathend)\r
2054         {\r
2055                 NextLevel();\r
2056                 return;\r
2057         }\r
2058 \r
2059         float checkrules_status;\r
2060         checkrules_status = WinningCondition_RanOutOfSpawns();\r
2061         if(checkrules_status == WINNING_YES)\r
2062         {\r
2063                 bprint("Hey! Someone ran out of spawns!\n");\r
2064         }\r
2065         else if(g_race && !g_race_qualifying && timelimit >= 0)\r
2066         {\r
2067                 checkrules_status = WinningCondition_Race(fraglimit);\r
2068                 //print("WC_RACE yields ", ftos(checkrules_status), "\n");\r
2069         }\r
2070         else if(g_race && g_race_qualifying == 2 && timelimit >= 0)\r
2071         {\r
2072                 checkrules_status = WinningCondition_QualifyingThenRace(fraglimit);\r
2073                 //print("WC_QUALIFYING_THEN_RACE yields ", ftos(checkrules_status), "\n");\r
2074         }\r
2075         else if(g_assault)\r
2076         {\r
2077                 checkrules_status = WinningCondition_Assault(); // TODO remove this?\r
2078         }\r
2079         else if(g_lms)\r
2080         {\r
2081                 checkrules_status = WinningCondition_LMS();\r
2082         }\r
2083         else if (g_onslaught)\r
2084         {\r
2085                 checkrules_status = WinningCondition_Onslaught(); // TODO remove this?\r
2086         }\r
2087         else\r
2088         {\r
2089                 checkrules_status = WinningCondition_Scores(fraglimit, leadlimit);\r
2090                 //print("WC_SCORES yields ", ftos(checkrules_status), "\n");\r
2091         }\r
2092 \r
2093         if(checkrules_status == WINNING_STARTSUDDENDEATHOVERTIME)\r
2094         {\r
2095                 checkrules_status = WINNING_NEVER;\r
2096                 checkrules_overtimesadded = -1;\r
2097                 wantovertime |= InitiateSuddenDeath();\r
2098         }\r
2099 \r
2100         if(checkrules_status == WINNING_NEVER)\r
2101                 // equality cases! Nobody wins if the overtime ends in a draw.\r
2102                 ClearWinners();\r
2103 \r
2104         if(wantovertime)\r
2105         {\r
2106                 if(checkrules_status == WINNING_NEVER)\r
2107                         InitiateOvertime();\r
2108                 else\r
2109                         checkrules_status = WINNING_YES;\r
2110         }\r
2111 \r
2112         if(checkrules_suddendeathend)\r
2113                 if(checkrules_status != WINNING_NEVER || time >= checkrules_suddendeathend)\r
2114                         checkrules_status = WINNING_YES;\r
2115 \r
2116         if(checkrules_status == WINNING_YES)\r
2117         {\r
2118                 //print("WINNING\n");\r
2119                 NextLevel();\r
2120         }\r
2121 };\r
2122 \r
2123 float mapvote_nextthink;\r
2124 float mapvote_initialized;\r
2125 float mapvote_keeptwotime;\r
2126 float mapvote_timeout;\r
2127 string mapvote_message;\r
2128 #define MAPVOTE_SCREENSHOT_DIRS_COUNT 4\r
2129 string mapvote_screenshot_dirs[MAPVOTE_SCREENSHOT_DIRS_COUNT];\r
2130 float mapvote_screenshot_dirs_count;\r
2131 \r
2132 float mapvote_count;\r
2133 float mapvote_count_real;\r
2134 string mapvote_maps[MAPVOTE_COUNT];\r
2135 float mapvote_maps_screenshot_dir[MAPVOTE_COUNT];\r
2136 string mapvote_maps_pakfile[MAPVOTE_COUNT];\r
2137 float mapvote_maps_suggested[MAPVOTE_COUNT];\r
2138 string mapvote_suggestions[MAPVOTE_COUNT];\r
2139 float mapvote_suggestion_ptr;\r
2140 float mapvote_maxlen;\r
2141 float mapvote_voters;\r
2142 float mapvote_votes[MAPVOTE_COUNT];\r
2143 float mapvote_run;\r
2144 float mapvote_detail;\r
2145 float mapvote_abstain;\r
2146 .float mapvote;\r
2147 \r
2148 void MapVote_ClearAllVotes()\r
2149 {\r
2150         FOR_EACH_CLIENT(other)\r
2151                 other.mapvote = 0;\r
2152 }\r
2153 \r
2154 string MapVote_Suggest(string m)\r
2155 {\r
2156         float i;\r
2157         if(m == "")\r
2158                 return "That's not how to use this command.";\r
2159         if(!cvar("g_maplist_votable_suggestions"))\r
2160                 return "Suggestions are not accepted on this server.";\r
2161         if(mapvote_initialized)\r
2162                 return "Can't suggest - voting is already in progress!";\r
2163         m = MapInfo_FixName(m);\r
2164         if(!m)\r
2165                 return "The map you suggested is not available on this server.";\r
2166         if(!cvar("g_maplist_votable_suggestions_override_mostrecent"))\r
2167                 if(Map_IsRecent(m))\r
2168                         return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";\r
2169 \r
2170         if(!MapInfo_CheckMap(m))\r
2171                 return "The map you suggested does not support the current game mode.";\r
2172         for(i = 0; i < mapvote_suggestion_ptr; ++i)\r
2173                 if(mapvote_suggestions[i] == m)\r
2174                         return "This map was already suggested.";\r
2175         if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)\r
2176         {\r
2177                 i = floor(random() * mapvote_suggestion_ptr);\r
2178         }\r
2179         else\r
2180         {\r
2181                 i = mapvote_suggestion_ptr;\r
2182                 mapvote_suggestion_ptr += 1;\r
2183         }\r
2184         if(mapvote_suggestions[i] != "")\r
2185                 strunzone(mapvote_suggestions[i]);\r
2186         mapvote_suggestions[i] = strzone(m);\r
2187         if(cvar("sv_eventlog"))\r
2188                 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));\r
2189         return strcat("Suggestion of ", m, " accepted.");\r
2190 }\r
2191 \r
2192 void MapVote_AddVotable(string nextMap, float isSuggestion)\r
2193 {\r
2194         float j, i, o;\r
2195         string pakfile, mapfile;\r
2196 \r
2197         if(nextMap == "")\r
2198                 return;\r
2199         for(j = 0; j < mapvote_count; ++j)\r
2200                 if(mapvote_maps[j] == nextMap)\r
2201                         return;\r
2202         if(strlen(nextMap) > mapvote_maxlen)\r
2203                 mapvote_maxlen = strlen(nextMap);\r
2204         mapvote_maps[mapvote_count] = strzone(nextMap);\r
2205         mapvote_maps_suggested[mapvote_count] = isSuggestion;\r
2206 \r
2207         for(i = 0; i < mapvote_screenshot_dirs_count; ++i)\r
2208         {\r
2209                 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);\r
2210                 pakfile = whichpack(strcat(mapfile, ".tga"));\r
2211                 if(pakfile == "")\r
2212                         pakfile = whichpack(strcat(mapfile, ".jpg"));\r
2213                 if(pakfile == "")\r
2214                         pakfile = whichpack(strcat(mapfile, ".png"));\r
2215                 if(pakfile != "")\r
2216                         break;\r
2217         }\r
2218         if(i >= mapvote_screenshot_dirs_count)\r
2219                 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?\r
2220         for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)\r
2221                 pakfile = substring(pakfile, o, -1);\r
2222 \r
2223         mapvote_maps_screenshot_dir[mapvote_count] = i;\r
2224         mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);\r
2225 \r
2226         mapvote_count += 1;\r
2227 }\r
2228 \r
2229 void MapVote_Spawn();\r
2230 void MapVote_Init()\r
2231 {\r
2232         float i;\r
2233         float nmax, smax;\r
2234 \r
2235         if(cvar("g_campaign"))\r
2236                 return;\r
2237 \r
2238         MapVote_ClearAllVotes();\r
2239 \r
2240         mapvote_count = 0;\r
2241         mapvote_detail = !cvar("g_maplist_votable_nodetail");\r
2242         mapvote_abstain = cvar("g_maplist_votable_abstain");\r
2243 \r
2244         if(mapvote_abstain)\r
2245                 nmax = min(MAPVOTE_COUNT - 1, cvar("g_maplist_votable"));\r
2246         else\r
2247                 nmax = min(MAPVOTE_COUNT, cvar("g_maplist_votable"));\r
2248         smax = min3(nmax, cvar("g_maplist_votable_suggestions"), mapvote_suggestion_ptr);\r
2249 \r
2250         // we need this for AddVotable, as that cycles through the screenshot dirs\r
2251         mapvote_screenshot_dirs_count = tokenize_console(cvar_string("g_maplist_votable_screenshot_dir"));\r
2252         if(mapvote_screenshot_dirs_count == 0)\r
2253                 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");\r
2254         mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);\r
2255         for(i = 0; i < mapvote_screenshot_dirs_count; ++i)\r
2256                 mapvote_screenshot_dirs[i] = strzone(argv(i));\r
2257 \r
2258         if(mapvote_suggestion_ptr)\r
2259                 for(i = 0; i < 100 && mapvote_count < smax; ++i)\r
2260                         MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], TRUE);\r
2261 \r
2262         for(i = 0; i < 100 && mapvote_count < nmax; ++i)\r
2263                 MapVote_AddVotable(GetNextMap(), FALSE);\r
2264 \r
2265         if(mapvote_count == 0)\r
2266         {\r
2267                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );\r
2268                 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));\r
2269                 if(cvar("g_maplist_shuffle"))\r
2270                         ShuffleMaplist();\r
2271                 localcmd("\nmenu_cmd sync\n");\r
2272                 for(i = 0; i < 100 && mapvote_count < nmax; ++i)\r
2273                         MapVote_AddVotable(GetNextMap(), FALSE);\r
2274         }\r
2275 \r
2276         mapvote_count_real = mapvote_count;\r
2277         if(mapvote_abstain)\r
2278                 MapVote_AddVotable("don't care", 0);\r
2279 \r
2280         //dprint("mapvote count is ", ftos(mapvote_count), "\n");\r
2281 \r
2282         mapvote_keeptwotime = time + cvar("g_maplist_votable_keeptwotime");\r
2283         mapvote_timeout = time + cvar("g_maplist_votable_timeout");\r
2284         if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)\r
2285                 mapvote_keeptwotime = 0;\r
2286         mapvote_message = "Choose a map and press its key!";\r
2287 \r
2288         MapVote_Spawn();\r
2289 }\r
2290 \r
2291 void MapVote_SendPicture(float id)\r
2292 {\r
2293         msg_entity = self;\r
2294         WriteByte(MSG_ONE, SVC_TEMPENTITY);\r
2295         WriteByte(MSG_ONE, TE_CSQC_PICTURE);\r
2296         WriteByte(MSG_ONE, id);\r
2297         WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);\r
2298 }\r
2299 \r
2300 float GameCommand_MapVote(string cmd)\r
2301 {\r
2302         if(!intermission_running)\r
2303                 return FALSE;\r
2304 \r
2305         if(cmd == "mv_getpic")\r
2306         {\r
2307                 MapVote_SendPicture(stof(argv(1)));\r
2308                 return TRUE;\r
2309         }\r
2310 \r
2311         return FALSE;\r
2312 }\r
2313 \r
2314 float MapVote_GetMapMask()\r
2315 {\r
2316         float mask, i, power;\r
2317         mask = 0;\r
2318         for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)\r
2319                 if(mapvote_maps[i] != "")\r
2320                         mask |= power;\r
2321         return mask;\r
2322 }\r
2323 \r
2324 entity mapvote_ent;\r
2325 float MapVote_SendEntity(entity to, float sf)\r
2326 {\r
2327         float i;\r
2328 \r
2329         if(sf & 1)\r
2330                 sf &~= 2; // if we send 1, we don't need to also send 2\r
2331 \r
2332         WriteByte(MSG_ENTITY, ENT_CLIENT_MAPVOTE);\r
2333         WriteByte(MSG_ENTITY, sf);\r
2334 \r
2335         if(sf & 1)\r
2336         {\r
2337                 // flag 1 == initialization\r
2338                 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)\r
2339                         WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);\r
2340                 WriteString(MSG_ENTITY, "");\r
2341                 WriteByte(MSG_ENTITY, mapvote_count);\r
2342                 WriteByte(MSG_ENTITY, mapvote_abstain);\r
2343                 WriteByte(MSG_ENTITY, mapvote_detail);\r
2344                 WriteCoord(MSG_ENTITY, mapvote_timeout);\r
2345                 if(mapvote_count <= 8)\r
2346                         WriteByte(MSG_ENTITY, MapVote_GetMapMask());\r
2347                 else\r
2348                         WriteShort(MSG_ENTITY, MapVote_GetMapMask());\r
2349                 for(i = 0; i < mapvote_count; ++i)\r
2350                         if(mapvote_maps[i] != "")\r
2351                         {\r
2352                                 if(mapvote_abstain && i == mapvote_count - 1)\r
2353                                 {\r
2354                                         WriteString(MSG_ENTITY, ""); // abstain needs no text\r
2355                                         WriteString(MSG_ENTITY, ""); // abstain needs no pack\r
2356                                         WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir\r
2357                                 }\r
2358                                 else\r
2359                                 {\r
2360                                         WriteString(MSG_ENTITY, mapvote_maps[i]);\r
2361                                         WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);\r
2362                                         WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);\r
2363                                 }\r
2364                         }\r
2365         }\r
2366 \r
2367         if(sf & 2)\r
2368         {\r
2369                 // flag 2 == update of mask\r
2370                 if(mapvote_count <= 8)\r
2371                         WriteByte(MSG_ENTITY, MapVote_GetMapMask());\r
2372                 else\r
2373                         WriteShort(MSG_ENTITY, MapVote_GetMapMask());\r
2374         }\r
2375 \r
2376         if(sf & 4)\r
2377         {\r
2378                 if(mapvote_detail)\r
2379                         for(i = 0; i < mapvote_count; ++i)\r
2380                                 if(mapvote_maps[i] != "")\r
2381                                         WriteByte(MSG_ENTITY, mapvote_votes[i]);\r
2382 \r
2383                 WriteByte(MSG_ENTITY, to.mapvote);\r
2384         }\r
2385 \r
2386         return TRUE;\r
2387 }\r
2388 \r
2389 void MapVote_Spawn()\r
2390 {\r
2391         Net_LinkEntity(mapvote_ent = spawn(), FALSE, 0, MapVote_SendEntity);\r
2392 }\r
2393 \r
2394 void MapVote_TouchMask()\r
2395 {\r
2396         mapvote_ent.SendFlags |= 2;\r
2397 }\r
2398 \r
2399 void MapVote_TouchVotes(entity voter)\r
2400 {\r
2401         mapvote_ent.SendFlags |= 4;\r
2402 }\r
2403 \r
2404 float MapVote_Finished(float mappos)\r
2405 {\r
2406         string result;\r
2407         float i;\r
2408         float didntvote;\r
2409 \r
2410         if(cvar("sv_eventlog"))\r
2411         {\r
2412                 result = strcat(":vote:finished:", mapvote_maps[mappos]);\r
2413                 result = strcat(result, ":", ftos(mapvote_votes[mappos]), "::");\r
2414                 didntvote = mapvote_voters;\r
2415                 for(i = 0; i < mapvote_count; ++i)\r
2416                         if(mapvote_maps[i] != "")\r
2417                         {\r
2418                                 didntvote -= mapvote_votes[i];\r
2419                                 if(i != mappos)\r
2420                                 {\r
2421                                         result = strcat(result, ":", mapvote_maps[i]);\r
2422                                         result = strcat(result, ":", ftos(mapvote_votes[i]));\r
2423                                 }\r
2424                         }\r
2425                 result = strcat(result, ":didn't vote:", ftos(didntvote));\r
2426 \r
2427                 GameLogEcho(result);\r
2428                 if(mapvote_maps_suggested[mappos])\r
2429                         GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));\r
2430         }\r
2431 \r
2432         FOR_EACH_REALCLIENT(other)\r
2433                 FixClientCvars(other);\r
2434 \r
2435         Map_Goto_SetStr(mapvote_maps[mappos]);\r
2436         Map_Goto();\r
2437         alreadychangedlevel = TRUE;\r
2438         return TRUE;\r
2439 }\r
2440 void MapVote_CheckRules_1()\r
2441 {\r
2442         float i;\r
2443 \r
2444         for(i = 0; i < mapvote_count; ++i) if(mapvote_maps[i] != "")\r
2445         {\r
2446                 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");\r
2447                 mapvote_votes[i] = 0;\r
2448         }\r
2449 \r
2450         mapvote_voters = 0;\r
2451         FOR_EACH_REALCLIENT(other)\r
2452         {\r
2453                 ++mapvote_voters;\r
2454                 if(other.mapvote)\r
2455                 {\r
2456                         i = other.mapvote - 1;\r
2457                         //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");\r
2458                         mapvote_votes[i] = mapvote_votes[i] + 1;\r
2459                 }\r
2460         }\r
2461 }\r
2462 \r
2463 float MapVote_CheckRules_2()\r
2464 {\r
2465         float i;\r
2466         float firstPlace, secondPlace;\r
2467         float firstPlaceVotes, secondPlaceVotes;\r
2468         float mapvote_voters_real;\r
2469         string result;\r
2470 \r
2471         if(mapvote_count_real == 1)\r
2472                 return MapVote_Finished(0);\r
2473 \r
2474         mapvote_voters_real = mapvote_voters;\r
2475         if(mapvote_abstain)\r
2476                 mapvote_voters_real -= mapvote_votes[mapvote_count - 1];\r
2477 \r
2478         RandomSelection_Init();\r
2479         for(i = 0; i < mapvote_count_real; ++i) if(mapvote_maps[i] != "")\r
2480                 RandomSelection_Add(world, i, string_null, 1, mapvote_votes[i]);\r
2481         firstPlace = RandomSelection_chosen_float;\r
2482         firstPlaceVotes = RandomSelection_best_priority;\r
2483         //dprint("First place: ", ftos(firstPlace), "\n");\r
2484         //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");\r
2485 \r
2486         RandomSelection_Init();\r
2487         for(i = 0; i < mapvote_count_real; ++i) if(mapvote_maps[i] != "")\r
2488                 if(i != firstPlace)\r
2489                         RandomSelection_Add(world, i, string_null, 1, mapvote_votes[i]);\r
2490         secondPlace = RandomSelection_chosen_float;\r
2491         secondPlaceVotes = RandomSelection_best_priority;\r
2492         //dprint("Second place: ", ftos(secondPlace), "\n");\r
2493         //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");\r
2494 \r
2495         if(firstPlace == -1)\r
2496                 error("No first place in map vote... WTF?");\r
2497 \r
2498         if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)\r
2499                 return MapVote_Finished(firstPlace);\r
2500 \r
2501         if(mapvote_keeptwotime)\r
2502                 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)\r
2503                 {\r
2504                         float didntvote;\r
2505                         MapVote_TouchMask();\r
2506                         mapvote_message = "Now decide between the TOP TWO!";\r
2507                         mapvote_keeptwotime = 0;\r
2508                         result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);\r
2509                         result = strcat(result, ":", ftos(firstPlaceVotes));\r
2510                         result = strcat(result, ":", mapvote_maps[secondPlace]);\r
2511                         result = strcat(result, ":", ftos(secondPlaceVotes), "::");\r
2512                         didntvote = mapvote_voters;\r
2513                         for(i = 0; i < mapvote_count; ++i)\r
2514                                 if(mapvote_maps[i] != "")\r
2515                                 {\r
2516                                         didntvote -= mapvote_votes[i];\r
2517                                         if(i != firstPlace)\r
2518                                                 if(i != secondPlace)\r
2519                                                 {\r
2520                                                         result = strcat(result, ":", mapvote_maps[i]);\r
2521                                                         result = strcat(result, ":", ftos(mapvote_votes[i]));\r
2522                                                         if(i < mapvote_count_real)\r
2523                                                         {\r
2524                                                                 strunzone(mapvote_maps[i]);\r
2525                                                                 mapvote_maps[i] = "";\r
2526                                                                 strunzone(mapvote_maps_pakfile[i]);\r
2527                                                                 mapvote_maps_pakfile[i] = "";\r
2528                                                         }\r
2529                                                 }\r
2530                                 }\r
2531                         result = strcat(result, ":didn't vote:", ftos(didntvote));\r
2532                         if(cvar("sv_eventlog"))\r
2533                                 GameLogEcho(result);\r
2534                 }\r
2535 \r
2536         return FALSE;\r
2537 }\r
2538 void MapVote_Tick()\r
2539 {\r
2540         float keeptwo;\r
2541         float totalvotes;\r
2542 \r
2543         keeptwo = mapvote_keeptwotime;\r
2544         MapVote_CheckRules_1(); // count\r
2545         if(MapVote_CheckRules_2()) // decide\r
2546                 return;\r
2547 \r
2548         totalvotes = 0;\r
2549         FOR_EACH_REALCLIENT(other)\r
2550         {\r
2551                 // hide scoreboard again\r
2552                 if(other.health != 2342)\r
2553                 {\r
2554                         other.health = 2342;\r
2555                         other.impulse = 0;\r
2556                         if(clienttype(other) == CLIENTTYPE_REAL)\r
2557                         {\r
2558                                 msg_entity = other;\r
2559                                 WriteByte(MSG_ONE, SVC_FINALE);\r
2560                                 WriteString(MSG_ONE, "");\r
2561                         }\r
2562                 }\r
2563 \r
2564                 // clear possibly invalid votes\r
2565                 if(mapvote_maps[other.mapvote - 1] == "")\r
2566                         other.mapvote = 0;\r
2567                 // use impulses as new vote\r
2568                 if(other.impulse >= 1 && other.impulse <= mapvote_count)\r
2569                         if(mapvote_maps[other.impulse - 1] != "")\r
2570                         {\r
2571                                 other.mapvote = other.impulse;\r
2572                                 MapVote_TouchVotes(other);\r
2573                         }\r
2574                 other.impulse = 0;\r
2575 \r
2576                 if(other.mapvote)\r
2577                         ++totalvotes;\r
2578         }\r
2579 \r
2580         MapVote_CheckRules_1(); // just count\r
2581 }\r
2582 void MapVote_Start()\r
2583 {\r
2584         if(mapvote_run)\r
2585                 return;\r
2586 \r
2587         MapInfo_Enumerate();\r
2588         if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))\r
2589                 mapvote_run = TRUE;\r
2590 }\r
2591 void MapVote_Think()\r
2592 {\r
2593         if(!mapvote_run)\r
2594                 return;\r
2595 \r
2596         if(alreadychangedlevel)\r
2597                 return;\r
2598 \r
2599         if(time < mapvote_nextthink)\r
2600                 return;\r
2601         //dprint("tick\n");\r
2602 \r
2603         mapvote_nextthink = time + 0.5;\r
2604 \r
2605         if(!mapvote_initialized)\r
2606         {\r
2607                 if(cvar("rescan_pending") == 1)\r
2608                 {\r
2609                         cvar_set("rescan_pending", "2");\r
2610                         localcmd("fs_rescan\nrescan_pending 3\n");\r
2611                         return;\r
2612                 }\r
2613                 else if(cvar("rescan_pending") == 2)\r
2614                 {\r
2615                         return;\r
2616                 }\r
2617                 else if(cvar("rescan_pending") == 3)\r
2618                 {\r
2619                         // now build missing mapinfo files\r
2620                         if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))\r
2621                                 return;\r
2622 \r
2623                         // we're done, start the timer\r
2624                         cvar_set("rescan_pending", "0");\r
2625                 }\r
2626 \r
2627                 mapvote_initialized = TRUE;\r
2628                 if(DoNextMapOverride())\r
2629                         return;\r
2630                 if(!cvar("g_maplist_votable") || player_count <= 0)\r
2631                 {\r
2632                         GotoNextMap();\r
2633                         return;\r
2634                 }\r
2635                 MapVote_Init();\r
2636         }\r
2637 \r
2638         MapVote_Tick();\r
2639 };\r
2640 \r
2641 string GotoMap(string m)\r
2642 {\r
2643         if(!MapInfo_CheckMap(m))\r
2644                 return "The map you chose is not available on this server.";\r
2645         cvar_set("nextmap", m);\r
2646         cvar_set("timelimit", "-1");\r
2647         if(mapvote_initialized || alreadychangedlevel)\r
2648         {\r
2649                 if(DoNextMapOverride())\r
2650                         return "Map switch initiated.";\r
2651                 else\r
2652                         return "Hm... no. For some reason I like THIS map more.";\r
2653         }\r
2654         else\r
2655                 return "Map switch will happen after scoreboard.";\r
2656 }\r
2657 \r
2658 \r
2659 void EndFrame()\r
2660 {\r
2661         float altime;\r
2662         FOR_EACH_REALCLIENT(self)\r
2663         {\r
2664                 if(self.classname == "spectator")\r
2665                 {\r
2666                         if(self.enemy.typehitsound)\r
2667                                 play2(self, "misc/typehit.wav");\r
2668                         else if(self.enemy.hitsound && self.cvar_cl_hitsound)\r
2669                                 play2(self, "misc/hit.wav");\r
2670                 }\r
2671                 else\r
2672                 {\r
2673                         if(self.typehitsound)\r
2674                                 play2(self, "misc/typehit.wav");\r
2675                         else if(self.hitsound && self.cvar_cl_hitsound)\r
2676                                 play2(self, "misc/hit.wav");\r
2677                 }\r
2678         }\r
2679         altime = time + frametime * (1 + cvar("g_antilag_nudge"));\r
2680         // add 1 frametime because after this, engine SV_Physics\r
2681         // increases time by a frametime and then networks the frame\r
2682         // add another frametime because client shows everything with\r
2683         // 1 frame of lag (cl_nolerp 0). The last +1 however should not be\r
2684         // needed!\r
2685         FOR_EACH_CLIENT(self)\r
2686         {\r
2687                 self.hitsound = FALSE;\r
2688                 self.typehitsound = FALSE;\r
2689                 antilag_record(self, altime);\r
2690         }\r
2691 }\r
2692 \r
2693 \r
2694 /*\r
2695  * RedirectionThink:\r
2696  * returns TRUE if redirecting\r
2697  */\r
2698 float redirection_timeout;\r
2699 float redirection_nextthink;\r
2700 float RedirectionThink()\r
2701 {\r
2702         float clients_found;\r
2703 \r
2704         if(redirection_target == "")\r
2705                 return FALSE;\r
2706 \r
2707         if(!redirection_timeout)\r
2708         {\r
2709                 cvar_set("sv_public", "-2");\r
2710                 redirection_timeout = time + 0.6; // this will only try twice... should be able to keep more clients\r
2711                 if(redirection_target == "self")\r
2712                         bprint("^3SERVER NOTICE:^7 restarting the server\n");\r
2713                 else\r
2714                         bprint("^3SERVER NOTICE:^7 redirecting everyone to ", redirection_target, "\n");\r
2715         }\r
2716 \r
2717         if(time < redirection_nextthink)\r
2718                 return TRUE;\r
2719 \r
2720         redirection_nextthink = time + 1;\r
2721 \r
2722         clients_found = 0;\r
2723         FOR_EACH_REALCLIENT(self)\r
2724         {\r
2725                 print("Redirecting: sending connect command to ", self.netname, "\n");\r
2726                 if(redirection_target == "self")\r
2727                         stuffcmd(self, "\ndisconnect; reconnect\n");\r
2728                 else\r
2729                         stuffcmd(self, strcat("\ndisconnect; connect ", redirection_target, "\n"));\r
2730                 ++clients_found;\r
2731         }\r
2732 \r
2733         print("Redirecting: ", ftos(clients_found), " clients left.\n");\r
2734 \r
2735         if(time > redirection_timeout || clients_found == 0)\r
2736                 localcmd("\nwait; wait; wait; quit\n");\r
2737 \r
2738         return TRUE;\r
2739 }\r
2740 \r
2741 void TargetMusic_RestoreGame();\r
2742 void RestoreGame()\r
2743 {\r
2744         // Loaded from a save game\r
2745         // some things then break, so let's work around them...\r
2746 \r
2747         // Progs DB (capture records)\r
2748         ServerProgsDB = db_load("server.db");\r
2749 \r
2750         // Mapinfo\r
2751         MapInfo_Shutdown();\r
2752         MapInfo_Enumerate();\r
2753         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1);\r
2754         WeaponStats_Init();\r
2755 \r
2756         TargetMusic_RestoreGame();\r
2757 }\r
2758 \r
2759 void SV_Shutdown()\r
2760 {\r
2761         if(gameover > 1) // shutting down already?\r
2762                 return;\r
2763 \r
2764         gameover = 2; // 2 = server shutting down\r
2765 \r
2766         if(world_initialized > 0)\r
2767         {\r
2768                 world_initialized = 0;\r
2769                 print("Saving persistent data...\n");\r
2770                 Ban_SaveBans();\r
2771                 if(!cheatcount_total)\r
2772                         db_save(ServerProgsDB, "server.db");\r
2773                 if(cvar("developer"))\r
2774                         db_save(TemporaryDB, "server-temp.db");\r
2775                 CheatShutdown(); // must be after cheatcount check\r
2776                 db_close(ServerProgsDB);\r
2777                 db_close(TemporaryDB);\r
2778                 print("done!\n");\r
2779                 // tell the bot system the game is ending now\r
2780                 bot_endgame();\r
2781 \r
2782                 WeaponStats_Shutdown();\r
2783                 MapInfo_Shutdown();\r
2784         }\r
2785         else if(world_initialized == 0)\r
2786         {\r
2787                 print("NOTE: crashed before even initializing the world, not saving persistent data\n");\r
2788         }\r
2789 }\r