]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - data/qcsrc/server/g_world.qc
Port crosshair pickup effect from Xonotic
[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_LAST_PICKUP, AS_FLOAT, last_pickup);\r
658         addstat(STAT_WINNING, AS_FLOAT, winning);\r
659         addstat(STAT_VORE_LOAD, AS_INT, stat_stomachload);\r
660         addstat(STAT_VORE_DIGESTING, AS_INT, stat_digesting);\r
661         addstat(STAT_VORE_EATEN, AS_INT, stat_eaten);\r
662         addstat(STAT_VORE_CANLEAVE, AS_INT, stat_canleave);\r
663 \r
664         next_pingtime = time + 5;\r
665         InitializeEntity(self, cvar_changes_init, INITPRIO_CVARS);\r
666 \r
667         detect_maptype();\r
668 \r
669         lsmaps_reply = "^7Maps available: ";\r
670         lsnewmaps_reply = "^7Maps without a record set: ";\r
671         for(i = 0, j = 0; i < MapInfo_count; ++i)\r
672         {\r
673                 if(MapInfo_Get_ByID(i))\r
674                         if not(MapInfo_Map_flags & (MAPINFO_FLAG_HIDDEN | MAPINFO_FLAG_FORBIDDEN))\r
675                         {\r
676                                 if(mod(i, 2))\r
677                                         col = "^2";\r
678                                 else\r
679                                         col = "^3";\r
680                                 ++j;\r
681                                 lsmaps_reply = strcat(lsmaps_reply, col, MapInfo_Map_bspname, " ");\r
682                                 if(g_race && !stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, RACE_RECORD, "time"))))\r
683                                         lsnewmaps_reply = strcat(lsnewmaps_reply, col, MapInfo_Map_bspname, " ");\r
684                                 else if(g_cts && !stof(db_get(ServerProgsDB, strcat(MapInfo_Map_bspname, CTS_RECORD, "time"))))\r
685                                         lsnewmaps_reply = strcat(lsnewmaps_reply, col, MapInfo_Map_bspname, " ");\r
686                         }\r
687         }\r
688         lsmaps_reply = strzone(strcat(lsmaps_reply, "\n"));\r
689         if (!g_race && !g_cts)\r
690                 lsnewmaps_reply = "Need to be playing race or CTS for lsnewmaps to work.";\r
691         lsnewmaps_reply = strzone(strcat(lsnewmaps_reply, "\n"));\r
692 \r
693         maplist_reply = "^7Maps in list: ";\r
694         n = tokenize_console(cvar_string("g_maplist"));\r
695         for(i = 0, j = 0; i < n; ++i)\r
696         {\r
697                 if(MapInfo_CheckMap(argv(i)))\r
698                 {\r
699                         if(mod(j, 2))\r
700                                 col = "^2";\r
701                         else\r
702                                 col = "^3";\r
703                         maplist_reply = strcat(maplist_reply, col, argv(i), " ");\r
704                         ++j;\r
705                 }\r
706         }\r
707         maplist_reply = strzone(strcat(maplist_reply, "\n"));\r
708         MapInfo_ClearTemps();\r
709 \r
710         for(i = 0; i < 10; ++i)\r
711         {\r
712                 records_reply[i] = strzone(getrecords(i));\r
713         }\r
714 \r
715         rankings_reply = strzone(getrankings());\r
716 \r
717         ClientInit_Spawn();\r
718         RandomSeed_Spawn();\r
719         PingPLReport_Spawn();\r
720         OtherPLReport_Spawn();\r
721 \r
722         CheatInit();\r
723 \r
724         localcmd("\n_sv_hook_gamestart ", GetGametype(), ";");\r
725         if(cvar("g_campaign"))\r
726                 localcmd("\n_sv_hook_campaign_gamestart ", GetGametype(), ";");\r
727 \r
728         world_initialized = 1;\r
729 }\r
730 \r
731 void spawnfunc_light (void)\r
732 {\r
733         //makestatic (self); // Who the f___ did that?\r
734         remove(self);\r
735 }\r
736 \r
737 float TryFile( string pFilename )\r
738 {\r
739         local float lHandle;\r
740         dprint("TryFile(\"", pFilename, "\")\n");\r
741         lHandle = fopen( pFilename, FILE_READ );\r
742         if( lHandle != -1 ) {\r
743                 fclose( lHandle );\r
744                 return TRUE;\r
745         } else {\r
746                 return FALSE;\r
747         }\r
748 };\r
749 \r
750 string GetGametype()\r
751 {\r
752         return GametypeNameFromType(game);\r
753 }\r
754 \r
755 string getmapname_stored;\r
756 string GetMapname()\r
757 {\r
758         return mapname;\r
759 }\r
760 \r
761 float Map_Count, Map_Current;\r
762 string Map_Current_Name;\r
763 \r
764 // NOTE: this now expects the map list to be already tokenize()d and the count in Map_Count\r
765 float GetMaplistPosition()\r
766 {\r
767         float pos, idx;\r
768         string map;\r
769 \r
770         map = GetMapname();\r
771         idx = cvar("g_maplist_index");\r
772 \r
773         if(idx >= 0)\r
774                 if(idx < Map_Count)\r
775                         if(map == argv(idx))\r
776                                 return idx;\r
777 \r
778         for(pos = 0; pos < Map_Count; ++pos)\r
779                 if(map == argv(pos))\r
780                         return pos;\r
781 \r
782         // resume normal maplist rotation if current map is not in g_maplist\r
783         return idx;\r
784 }\r
785 \r
786 float MapHasRightSize(string map)\r
787 {\r
788         float fh;\r
789         if(currentbots || cvar("bot_number") || player_count < cvar("minplayers"))\r
790         if(cvar("g_maplist_check_waypoints"))\r
791         {\r
792                 dprint("checkwp "); dprint(map);\r
793                 fh = fopen(strcat("maps/", map, ".waypoints"), FILE_READ);\r
794                 if(fh < 0)\r
795                 {\r
796                         dprint(": no waypoints\n");\r
797                         return FALSE;\r
798                 }\r
799                 dprint(": has waypoints\n");\r
800                 fclose(fh);\r
801         }\r
802 \r
803         // open map size restriction file\r
804         dprint("opensize "); dprint(map);\r
805         fh = fopen(strcat("maps/", map, ".sizes"), FILE_READ);\r
806         if(fh >= 0)\r
807         {\r
808                 float mapmin, mapmax;\r
809                 dprint(": ok, ");\r
810                 mapmin = stof(fgets(fh));\r
811                 mapmax = stof(fgets(fh));\r
812                 fclose(fh);\r
813                 if(player_count < mapmin)\r
814                 {\r
815                         dprint("not enough\n");\r
816                         return FALSE;\r
817                 }\r
818                 if(player_count > mapmax)\r
819                 {\r
820                         dprint("too many\n");\r
821                         return FALSE;\r
822                 }\r
823                 dprint("right size\n");\r
824                 return TRUE;\r
825         }\r
826         dprint(": not found\n");\r
827         return TRUE;\r
828 }\r
829 \r
830 string Map_Filename(float position)\r
831 {\r
832         return strcat("maps/", argv(position), ".bsp");\r
833 }\r
834 \r
835 string strwords(string s, float w)\r
836 {\r
837         float endpos;\r
838         for(endpos = 0; w && endpos >= 0; --w)\r
839                 endpos = strstrofs(s, " ", endpos + 1);\r
840         if(endpos < 0)\r
841                 return s;\r
842         else\r
843                 return substring(s, 0, endpos);\r
844 }\r
845 \r
846 float strhasword(string s, string w)\r
847 {\r
848         return strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0;\r
849 }\r
850 \r
851 void Map_MarkAsRecent(string m)\r
852 {\r
853         cvar_set("g_maplist_mostrecent", strwords(strcat(m, " ", cvar_string("g_maplist_mostrecent")), max(0, cvar("g_maplist_mostrecent_count"))));\r
854 }\r
855 \r
856 float Map_IsRecent(string m)\r
857 {\r
858         return strhasword(cvar_string("g_maplist_mostrecent"), m);\r
859 }\r
860 \r
861 float Map_Check(float position, float pass)\r
862 {\r
863         string filename;\r
864         string map_next;\r
865         map_next = argv(position);\r
866         if(pass <= 1)\r
867         {\r
868                 if(Map_IsRecent(map_next))\r
869                         return 0;\r
870         }\r
871         filename = Map_Filename(position);\r
872         if(MapInfo_CheckMap(map_next))\r
873         {\r
874                 if(pass == 2)\r
875                         return 1;\r
876                 if(MapHasRightSize(map_next))\r
877                         return 1;\r
878                 return 0;\r
879         }\r
880         else\r
881                 dprint( "Couldn't select '", filename, "'..\n" );\r
882 \r
883         return 0;\r
884 }\r
885 \r
886 void Map_Goto_SetStr(string nextmapname)\r
887 {\r
888         if(getmapname_stored != "")\r
889                 strunzone(getmapname_stored);\r
890         if(nextmapname == "")\r
891                 getmapname_stored = "";\r
892         else\r
893                 getmapname_stored = strzone(nextmapname);\r
894 }\r
895 \r
896 void Map_Goto_SetFloat(float position)\r
897 {\r
898         cvar_set("g_maplist_index", ftos(position));\r
899         Map_Goto_SetStr(argv(position));\r
900 }\r
901 \r
902 void GameResetCfg()\r
903 {\r
904         // settings persist, except...\r
905         localcmd("\nsettemp_restore\n");\r
906 };\r
907 \r
908 void Map_Goto()\r
909 {\r
910         GameResetCfg();\r
911         MapInfo_LoadMap(getmapname_stored);\r
912 }\r
913 \r
914 // return codes of map selectors:\r
915 //   -1 = temporary failure (that is, try some method that is guaranteed to succeed)\r
916 //   -2 = permanent failure\r
917 float() MaplistMethod_Iterate = // usual method\r
918 {\r
919         float pass, i;\r
920 \r
921         for(pass = 1; pass <= 2; ++pass)\r
922         {\r
923                 for(i = 1; i < Map_Count; ++i)\r
924                 {\r
925                         float mapindex;\r
926                         mapindex = mod(i + Map_Current, Map_Count);\r
927                         if(Map_Check(mapindex, pass))\r
928                                 return mapindex;\r
929                 }\r
930         }\r
931         return -1;\r
932 }\r
933 \r
934 float() MaplistMethod_Repeat = // fallback method\r
935 {\r
936         if(Map_Check(Map_Current, 2))\r
937                 return Map_Current;\r
938         return -2;\r
939 }\r
940 \r
941 float() MaplistMethod_Random = // random map selection\r
942 {\r
943         float i, imax;\r
944 \r
945         imax = 42;\r
946 \r
947         for(i = 0; i <= imax; ++i)\r
948         {\r
949                 float mapindex;\r
950                 mapindex = mod(Map_Current + floor(random() * (Map_Count - 1) + 1), Map_Count); // any OTHER map\r
951                 if(Map_Check(mapindex, 1))\r
952                         return mapindex;\r
953         }\r
954         return -1;\r
955 }\r
956 \r
957 float(float exponent) MaplistMethod_Shuffle = // more clever shuffling\r
958 // the exponent sets a bias on the map selection:\r
959 // the higher the exponent, the less likely "shortly repeated" same maps are\r
960 {\r
961         float i, j, imax, insertpos;\r
962 \r
963         imax = 42;\r
964 \r
965         for(i = 0; i <= imax; ++i)\r
966         {\r
967                 string newlist;\r
968 \r
969                 // now reinsert this at another position\r
970                 insertpos = pow(random(), 1 / exponent);       // ]0, 1]\r
971                 insertpos = insertpos * (Map_Count - 1);       // ]0, Map_Count - 1]\r
972                 insertpos = ceil(insertpos) + 1;               // {2, 3, 4, ..., Map_Count}\r
973                 dprint("SHUFFLE: insert pos = ", ftos(insertpos), "\n");\r
974 \r
975                 // insert the current map there\r
976                 newlist = "";\r
977                 for(j = 1; j < insertpos; ++j)                 // i == 1: no loop, will be inserted as first; however, i == 1 has been excluded above\r
978                         newlist = strcat(newlist, " ", argv(j));\r
979                 newlist = strcat(newlist, " ", argv(0));       // now insert the just selected map\r
980                 for(j = insertpos; j < Map_Count; ++j)         // i == Map_Count: no loop, has just been inserted as last\r
981                         newlist = strcat(newlist, " ", argv(j));\r
982                 newlist = substring(newlist, 1, strlen(newlist) - 1);\r
983                 cvar_set("g_maplist", newlist);\r
984                 Map_Count = tokenizebyseparator(cvar_string("g_maplist"), " ");\r
985 \r
986                 // NOTE: the selected map has just been inserted at (insertpos-1)th position\r
987                 Map_Current = insertpos - 1; // this is not really valid, but this way the fallback has a chance of working\r
988                 if(Map_Check(Map_Current, 1))\r
989                         return Map_Current;\r
990         }\r
991         return -1;\r
992 }\r
993 \r
994 void Maplist_Init()\r
995 {\r
996         Map_Count = tokenizebyseparator(cvar_string("g_maplist"), " ");\r
997         if(Map_Count == 0)\r
998         {\r
999                 bprint( "Maplist is empty!  Resetting it to default map list.\n" );\r
1000                 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));\r
1001                 if(cvar("g_maplist_shuffle"))\r
1002                         ShuffleMaplist();\r
1003                 localcmd("\nmenu_cmd sync\n");\r
1004                 Map_Count = tokenizebyseparator(cvar_string("g_maplist"), " ");\r
1005         }\r
1006         if(Map_Count == 0)\r
1007                 error("empty maplist, cannot select a new map");\r
1008         Map_Current = bound(0, GetMaplistPosition(), Map_Count - 1);\r
1009 \r
1010         if(Map_Current_Name)\r
1011                 strunzone(Map_Current_Name);\r
1012         Map_Current_Name = strzone(argv(Map_Current)); // will be automatically freed on exit thanks to DP\r
1013         // this may or may not be correct, but who cares, in the worst case a map\r
1014         // isn't chosen in the first pass that should have been\r
1015 }\r
1016 \r
1017 string GetNextMap()\r
1018 {\r
1019         float nextMap;\r
1020 \r
1021         Maplist_Init();\r
1022         nextMap = -1;\r
1023 \r
1024         if(nextMap == -1)\r
1025                 if(cvar("g_maplist_shuffle") > 0)\r
1026                         nextMap = MaplistMethod_Shuffle(cvar("g_maplist_shuffle") + 1);\r
1027 \r
1028         if(nextMap == -1)\r
1029                 if(cvar("g_maplist_selectrandom"))\r
1030                         nextMap = MaplistMethod_Random();\r
1031 \r
1032         if(nextMap == -1)\r
1033                 nextMap = MaplistMethod_Iterate();\r
1034 \r
1035         if(nextMap == -1)\r
1036                 nextMap = MaplistMethod_Repeat();\r
1037 \r
1038         if(nextMap >= 0)\r
1039         {\r
1040                 Map_Goto_SetFloat(nextMap);\r
1041                 return getmapname_stored;\r
1042         }\r
1043 \r
1044         return "";\r
1045 };\r
1046 \r
1047 float DoNextMapOverride()\r
1048 {\r
1049         if(cvar("g_campaign"))\r
1050         {\r
1051                 if(cvar("g_campaign_changelevel"))\r
1052                 {\r
1053                         CampaignPostIntermission();\r
1054                         alreadychangedlevel = TRUE;\r
1055                         return TRUE;\r
1056                 }\r
1057                 else\r
1058                         localcmd("togglemenu 1\n");\r
1059         }\r
1060         if(cvar("quit_when_empty"))\r
1061         {\r
1062                 if(player_count <= currentbots)\r
1063                 {\r
1064                         localcmd("quit\n");\r
1065                         alreadychangedlevel = TRUE;\r
1066                         return TRUE;\r
1067                 }\r
1068         }\r
1069         if(cvar_string("quit_and_redirect") != "")\r
1070         {\r
1071                 redirection_target = strzone(cvar_string("quit_and_redirect"));\r
1072                 alreadychangedlevel = TRUE;\r
1073                 return TRUE;\r
1074         }\r
1075         if (cvar("samelevel")) // if samelevel is set, stay on same level\r
1076         {\r
1077                 // 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
1078                 //localcmd(strcat("exec \"maps/", mapname, ".mapcfg\"\n"));\r
1079                 // so instead just restart the current map using the restart command (DOES NOT WORK PROPERLY WITH exit_cfg STUFF)\r
1080                 localcmd("restart\n");\r
1081                 //changelevel (mapname);\r
1082                 alreadychangedlevel = TRUE;\r
1083                 return TRUE;\r
1084         }\r
1085         if(cvar_string("nextmap") != "")\r
1086                 if(MapInfo_CheckMap(cvar_string("nextmap")))\r
1087                 {\r
1088                         Map_Goto_SetStr(cvar_string("nextmap"));\r
1089                         Map_Goto();\r
1090                         alreadychangedlevel = TRUE;\r
1091                         return TRUE;\r
1092                 }\r
1093         if(cvar("lastlevel"))\r
1094         {\r
1095                 GameResetCfg();\r
1096                 localcmd("set lastlevel 0\ntogglemenu\n");\r
1097                 alreadychangedlevel = TRUE;\r
1098                 return TRUE;\r
1099         }\r
1100         return FALSE;\r
1101 };\r
1102 \r
1103 void GotoNextMap()\r
1104 {\r
1105         //local string nextmap;\r
1106         //local float n, nummaps;\r
1107         //local string s;\r
1108         if (alreadychangedlevel)\r
1109                 return;\r
1110         alreadychangedlevel = TRUE;\r
1111 \r
1112         {\r
1113                 string nextMap;\r
1114                 float allowReset;\r
1115 \r
1116                 for(allowReset = 1; allowReset >= 0; --allowReset)\r
1117                 {\r
1118                         nextMap = GetNextMap();\r
1119                         if(nextMap != "")\r
1120                                 break;\r
1121 \r
1122                         if(allowReset)\r
1123                         {\r
1124                                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );\r
1125                                 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));\r
1126                                 if(cvar("g_maplist_shuffle"))\r
1127                                         ShuffleMaplist();\r
1128                                 localcmd("\nmenu_cmd sync\n");\r
1129                         }\r
1130                         else\r
1131                         {\r
1132                                 error("Everything is broken - not even the default map list works. Please report this to the developers.");\r
1133                         }\r
1134                 }\r
1135                 Map_Goto();\r
1136         }\r
1137 };\r
1138 \r
1139 \r
1140 /*\r
1141 ============\r
1142 IntermissionThink\r
1143 \r
1144 When the player presses attack or jump, change to the next level\r
1145 ============\r
1146 */\r
1147 .float autoscreenshot;\r
1148 void() MapVote_Start;\r
1149 void() MapVote_Think;\r
1150 float mapvote_initialized;\r
1151 void IntermissionThink()\r
1152 {\r
1153         FixIntermissionClient(self);\r
1154 \r
1155         if(cvar("sv_autoscreenshot"))\r
1156         if(self.autoscreenshot > 0)\r
1157         if(time > self.autoscreenshot)\r
1158         {\r
1159                 self.autoscreenshot = -1;\r
1160                 if(clienttype(self) == CLIENTTYPE_REAL)\r
1161                         stuffcmd(self, "\nscreenshot\necho \"^5A screenshot has been taken at request of the server.\"\n");\r
1162                 return;\r
1163         }\r
1164 \r
1165         if (time < intermission_exittime)\r
1166                 return;\r
1167 \r
1168         if(!mapvote_initialized)\r
1169                 if (time < intermission_exittime + 10 && !self.BUTTON_ATCK && !self.BUTTON_JUMP && !self.BUTTON_ATCK2 && !self.BUTTON_JETPACK && !self.BUTTON_USE)\r
1170                         return;\r
1171 \r
1172         MapVote_Start();\r
1173 };\r
1174 \r
1175 /*\r
1176 ============\r
1177 FindIntermission\r
1178 \r
1179 Returns the entity to view from\r
1180 ============\r
1181 */\r
1182 /*\r
1183 entity FindIntermission()\r
1184 {\r
1185         local   entity spot;\r
1186         local   float cyc;\r
1187 \r
1188 // look for info_intermission first\r
1189         spot = find (world, classname, "info_intermission");\r
1190         if (spot)\r
1191         {       // pick a random one\r
1192                 cyc = random() * 4;\r
1193                 while (cyc > 1)\r
1194                 {\r
1195                         spot = find (spot, classname, "info_intermission");\r
1196                         if (!spot)\r
1197                                 spot = find (spot, classname, "info_intermission");\r
1198                         cyc = cyc - 1;\r
1199                 }\r
1200                 return spot;\r
1201         }\r
1202 \r
1203 // then look for the start position\r
1204         spot = find (world, classname, "info_player_start");\r
1205         if (spot)\r
1206                 return spot;\r
1207 \r
1208 // testinfo_player_start is only found in regioned levels\r
1209         spot = find (world, classname, "testplayerstart");\r
1210         if (spot)\r
1211                 return spot;\r
1212 \r
1213 // then look for the start position\r
1214         spot = find (world, classname, "info_player_deathmatch");\r
1215         if (spot)\r
1216                 return spot;\r
1217 \r
1218         //objerror ("FindIntermission: no spot");\r
1219         return world;\r
1220 };\r
1221 */\r
1222 \r
1223 /*\r
1224 ===============================================================================\r
1225 \r
1226 RULES\r
1227 \r
1228 ===============================================================================\r
1229 */\r
1230 \r
1231 void DumpStats(float final)\r
1232 {\r
1233         local float file;\r
1234         local string s;\r
1235         local float to_console;\r
1236         local float to_eventlog;\r
1237         local float to_file;\r
1238         local float i;\r
1239 \r
1240         to_console = cvar("sv_logscores_console");\r
1241         to_eventlog = cvar("sv_eventlog");\r
1242         to_file = cvar("sv_logscores_file");\r
1243 \r
1244         if(!final)\r
1245         {\r
1246                 to_console = TRUE; // always print printstats replies\r
1247                 to_eventlog = FALSE; // but never print them to the event log\r
1248         }\r
1249 \r
1250         if(to_eventlog)\r
1251                 if(cvar("sv_eventlog_console"))\r
1252                         to_console = FALSE; // otherwise we get the output twice\r
1253 \r
1254         if(final)\r
1255                 s = ":scores:";\r
1256         else\r
1257                 s = ":status:";\r
1258         s = strcat(s, GetGametype(), "_", GetMapname(), ":", ftos(rint(time)));\r
1259 \r
1260         if(to_console)\r
1261                 print(s, "\n");\r
1262         if(to_eventlog)\r
1263                 GameLogEcho(s);\r
1264         if(to_file)\r
1265         {\r
1266                 file = fopen(cvar_string("sv_logscores_filename"), FILE_APPEND);\r
1267                 if(file == -1)\r
1268                         to_file = FALSE;\r
1269                 else\r
1270                         fputs(file, strcat(s, "\n"));\r
1271         }\r
1272 \r
1273         s = strcat(":labels:player:", GetPlayerScoreString(world, 0));\r
1274         if(to_console)\r
1275                 print(s, "\n");\r
1276         if(to_eventlog)\r
1277                 GameLogEcho(s);\r
1278         if(to_file)\r
1279                 fputs(file, strcat(s, "\n"));\r
1280 \r
1281         FOR_EACH_CLIENT(other)\r
1282         {\r
1283                 if ((clienttype(other) == CLIENTTYPE_REAL) || (clienttype(other) == CLIENTTYPE_BOT && cvar("sv_logscores_bots")))\r
1284                 {\r
1285                         s = strcat(":player:see-labels:", GetPlayerScoreString(other, 0), ":");\r
1286                         s = strcat(s, ftos(rint(time - other.jointime)), ":");\r
1287                         if(other.classname == "player" || g_arena || g_ca || g_lms)\r
1288                                 s = strcat(s, ftos(other.team), ":");\r
1289                         else\r
1290                                 s = strcat(s, "spectator:");\r
1291 \r
1292                         if(to_console)\r
1293                                 print(s, other.netname, "\n");\r
1294                         if(to_eventlog)\r
1295                                 GameLogEcho(strcat(s, ftos(other.playerid), ":", other.netname));\r
1296                         if(to_file)\r
1297                                 fputs(file, strcat(s, other.netname, "\n"));\r
1298                 }\r
1299         }\r
1300 \r
1301         if(teams_matter)\r
1302         {\r
1303                 s = strcat(":labels:teamscores:", GetTeamScoreString(0, 0));\r
1304                 if(to_console)\r
1305                         print(s, "\n");\r
1306                 if(to_eventlog)\r
1307                         GameLogEcho(s);\r
1308                 if(to_file)\r
1309                         fputs(file, strcat(s, "\n"));\r
1310 \r
1311                 for(i = 1; i < 16; ++i)\r
1312                 {\r
1313                         s = strcat(":teamscores:see-labels:", GetTeamScoreString(i, 0));\r
1314                         s = strcat(s, ":", ftos(i));\r
1315                         if(to_console)\r
1316                                 print(s, "\n");\r
1317                         if(to_eventlog)\r
1318                                 GameLogEcho(s);\r
1319                         if(to_file)\r
1320                                 fputs(file, strcat(s, "\n"));\r
1321                 }\r
1322         }\r
1323 \r
1324         if(to_console)\r
1325                 print(":end\n");\r
1326         if(to_eventlog)\r
1327                 GameLogEcho(":end");\r
1328         if(to_file)\r
1329         {\r
1330                 fputs(file, ":end\n");\r
1331                 fclose(file);\r
1332         }\r
1333 }\r
1334 \r
1335 void FixIntermissionClient(entity e)\r
1336 {\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                         msg_entity = e;\r
1357                         WriteByte(MSG_ONE, SVC_INTERMISSION);\r
1358                 }\r
1359         }\r
1360 \r
1361         //e.velocity = '0 0 0';\r
1362         //e.fixangle = TRUE;\r
1363 \r
1364         // TODO halt weapon animation\r
1365 }\r
1366 \r
1367 \r
1368 /*\r
1369 go to the next level for deathmatch\r
1370 only called if a time or frag limit has expired\r
1371 */\r
1372 void NextLevel()\r
1373 {\r
1374         float i;\r
1375 \r
1376         gameover = TRUE;\r
1377 \r
1378         intermission_running = 1;\r
1379 \r
1380 // enforce a wait time before allowing changelevel\r
1381         if(player_count > 0)\r
1382                 intermission_exittime = time + cvar("sv_mapchange_delay");\r
1383         else\r
1384                 intermission_exittime = -1;\r
1385 \r
1386         /*\r
1387         WriteByte (MSG_ALL, SVC_CDTRACK);\r
1388         WriteByte (MSG_ALL, 3);\r
1389         WriteByte (MSG_ALL, 3);\r
1390         // done in FixIntermission\r
1391         */\r
1392 \r
1393         //pos = FindIntermission ();\r
1394 \r
1395         VoteReset();\r
1396 \r
1397         DumpStats(TRUE);\r
1398 \r
1399         if(cvar("sv_eventlog"))\r
1400                 GameLogEcho(":gameover");\r
1401 \r
1402         GameLogClose();\r
1403 \r
1404 // TO DO\r
1405 \r
1406 // save the stats to a text file on the client\r
1407 // stuffcmd(other, log_stats "stats/file_name");\r
1408 // bprint stats\r
1409 // stuffcmd(other, log_stats "");\r
1410 // use a filename similar to the demo name\r
1411         // string file_name;\r
1412         // file_name = strcat("\nlog_file \"stats/", strftime(TRUE, "%Y-%m-%d_%H-%M"), "_", mapname, ".txt\"");  // open the log file\r
1413 \r
1414 // write a stats parser for the menu\r
1415 \r
1416         if(cvar("sv_accuracy_data_send")) {\r
1417                 string stats_to_send;\r
1418 \r
1419                 FOR_EACH_CLIENT(other) {  // make the string to send\r
1420                         FixIntermissionClient(other);\r
1421 \r
1422                         if(other.cvar_cl_accuracy_data_share) {\r
1423                                 stats_to_send = strcat(stats_to_send, ":hits:", other.netname);\r
1424 \r
1425                                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)\r
1426                                         stats_to_send = strcat(stats_to_send, ":", ftos(other.stats_hit[i-1]));\r
1427 \r
1428                                 stats_to_send = strcat(stats_to_send, "\n:fired:", other.netname);\r
1429 \r
1430                                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)\r
1431                                         stats_to_send = strcat(stats_to_send, ":", ftos(other.stats_fired[i-1]));\r
1432 \r
1433                                 stats_to_send = strcat(stats_to_send, "\n");\r
1434                         }\r
1435                 }\r
1436 \r
1437                 FOR_EACH_REALCLIENT(other) {  // only spam humans\r
1438                         Score_NicePrint(other);  // print the score\r
1439 \r
1440                         if(other.cvar_cl_accuracy_data_receive)  // send the stats string to all the willing clients\r
1441                                 bprint(stats_to_send);\r
1442                 }\r
1443         } else { // ye olde message\r
1444                 FOR_EACH_PLAYER(other) {\r
1445                         FixIntermissionClient(other);\r
1446 \r
1447                         if(other.winning)\r
1448                                 bprint(other.netname, " ^7wins.\n");\r
1449                 }\r
1450         }\r
1451 \r
1452         if(cvar("g_campaign"))\r
1453                 CampaignPreIntermission();\r
1454 \r
1455         localcmd("\nsv_hook_gameend;");\r
1456         if(cvar("g_campaign"))\r
1457                 localcmd("\nsv_hook_campaign_gameend;");\r
1458 }\r
1459 \r
1460 /*\r
1461 ============\r
1462 CheckRules_Player\r
1463 \r
1464 Exit deathmatch games upon conditions\r
1465 ============\r
1466 */\r
1467 void CheckRules_Player()\r
1468 {\r
1469         if (gameover)   // someone else quit the game already\r
1470                 return;\r
1471 \r
1472         if(self.deadflag == DEAD_NO)\r
1473                 self.play_time += frametime;\r
1474 \r
1475         // fixme: don't check players; instead check spawnfunc_dom_team and spawnfunc_ctf_team entities\r
1476         //   (div0: and that in CheckRules_World please)\r
1477 };\r
1478 \r
1479 float checkrules_equality;\r
1480 float checkrules_suddendeathwarning;\r
1481 float checkrules_suddendeathend;\r
1482 float checkrules_overtimesadded; //how many overtimes have been already added\r
1483 \r
1484 float WINNING_NO = 0; // no winner, but time limits may terminate the game\r
1485 float WINNING_YES = 1; // winner found\r
1486 float WINNING_NEVER = 2; // no winner, enter overtime if time limit is reached\r
1487 float WINNING_STARTSUDDENDEATHOVERTIME = 3; // no winner, enter suddendeath overtime NOW\r
1488 \r
1489 float InitiateSuddenDeath()\r
1490 {\r
1491         // Check first whether normal overtimes could be added before initiating suddendeath mode\r
1492         // - for this timelimit_overtime needs to be >0 of course\r
1493         // - also check the winning condition calculated in the previous frame and only add normal overtime\r
1494         //   again, if at the point at which timelimit would be extended again, still no winner was found\r
1495         if ((checkrules_overtimesadded >= 0) && (checkrules_overtimesadded < cvar("timelimit_overtimes")) && cvar("timelimit_overtime") && !(g_race && !g_race_qualifying))\r
1496         {\r
1497                 return 1; // need to call InitiateOvertime later\r
1498         }\r
1499         else\r
1500         {\r
1501                 if(!checkrules_suddendeathend)\r
1502                 {\r
1503                         checkrules_suddendeathend = time + 60 * cvar("timelimit_suddendeath");\r
1504                         if(g_race && !g_race_qualifying)\r
1505                                 race_StartCompleting();\r
1506                 }\r
1507                 return 0;\r
1508         }\r
1509 }\r
1510 \r
1511 void InitiateOvertime() // ONLY call this if InitiateSuddenDeath returned true\r
1512 {\r
1513         ++checkrules_overtimesadded;\r
1514         //add one more overtime by simply extending the timelimit\r
1515         float tl;\r
1516         tl = cvar("timelimit");\r
1517         tl += cvar("timelimit_overtime");\r
1518         cvar_set("timelimit", ftos(tl));\r
1519         string minutesPlural;\r
1520         if (cvar("timelimit_overtime") == 1)\r
1521                 minutesPlural = " ^3minute";\r
1522         else\r
1523                 minutesPlural = " ^3minutes";\r
1524 \r
1525         bcenterprint(\r
1526                 strcat(\r
1527                         "^3Now playing ^1OVERTIME^3!\n\n^3Added ^1",\r
1528                         ftos(cvar("timelimit_overtime")),\r
1529                         minutesPlural,\r
1530                         " to the game!"\r
1531                 )\r
1532         );\r
1533 }\r
1534 \r
1535 float GetWinningCode(float fraglimitreached, float equality)\r
1536 {\r
1537         if(equality)\r
1538                 if(fraglimitreached)\r
1539                         return WINNING_STARTSUDDENDEATHOVERTIME;\r
1540                 else\r
1541                         return WINNING_NEVER;\r
1542         else\r
1543                 if(fraglimitreached)\r
1544                         return WINNING_YES;\r
1545                 else\r
1546                         return WINNING_NO;\r
1547 }\r
1548 \r
1549 // set the .winning flag for exactly those players with a given field value\r
1550 void SetWinners(.float field, float value)\r
1551 {\r
1552         entity head;\r
1553         FOR_EACH_PLAYER(head)\r
1554                 head.winning = (head.field == value);\r
1555 }\r
1556 \r
1557 // set the .winning flag for those players with a given field value\r
1558 void AddWinners(.float field, float value)\r
1559 {\r
1560         entity head;\r
1561         FOR_EACH_PLAYER(head)\r
1562                 if(head.field == value)\r
1563                         head.winning = 1;\r
1564 }\r
1565 \r
1566 // clear the .winning flags\r
1567 void ClearWinners(void)\r
1568 {\r
1569         entity head;\r
1570         FOR_EACH_PLAYER(head)\r
1571                 head.winning = 0;\r
1572 }\r
1573 \r
1574 // Onslaught winning condition:\r
1575 // game terminates if only one team has a working generator (or none)\r
1576 float WinningCondition_Onslaught()\r
1577 {\r
1578         entity head;\r
1579         local float t1, t2, t3, t4;\r
1580 \r
1581         WinningConditionHelper(); // set worldstatus\r
1582 \r
1583         if(inWarmupStage)\r
1584                 return WINNING_NO;\r
1585 \r
1586         // first check if the game has ended\r
1587         t1 = t2 = t3 = t4 = 0;\r
1588         head = find(world, classname, "onslaught_generator");\r
1589         while (head)\r
1590         {\r
1591                 if (head.health > 0)\r
1592                 {\r
1593                         if (head.team == COLOR_TEAM1) t1 = 1;\r
1594                         if (head.team == COLOR_TEAM2) t2 = 1;\r
1595                         if (head.team == COLOR_TEAM3) t3 = 1;\r
1596                         if (head.team == COLOR_TEAM4) t4 = 1;\r
1597                 }\r
1598                 head = find(head, classname, "onslaught_generator");\r
1599         }\r
1600         if (t1 + t2 + t3 + t4 < 2)\r
1601         {\r
1602                 // game over, only one team remains (or none)\r
1603                 ClearWinners();\r
1604                 if (t1) SetWinners(team, COLOR_TEAM1);\r
1605                 if (t2) SetWinners(team, COLOR_TEAM2);\r
1606                 if (t3) SetWinners(team, COLOR_TEAM3);\r
1607                 if (t4) SetWinners(team, COLOR_TEAM4);\r
1608                 dprint("Have a winner, ending game.\n");\r
1609                 return WINNING_YES;\r
1610         }\r
1611 \r
1612         // Two or more teams remain\r
1613         return WINNING_NO;\r
1614 }\r
1615 \r
1616 float LMS_NewPlayerLives()\r
1617 {\r
1618         float fl;\r
1619         fl = cvar("fraglimit");\r
1620         if(fl == 0)\r
1621                 fl = 999;\r
1622 \r
1623         // first player has left the game for dying too much? Nobody else can get in.\r
1624         if(lms_lowest_lives < 1)\r
1625                 return 0;\r
1626 \r
1627         if(!cvar("g_lms_join_anytime"))\r
1628                 if(lms_lowest_lives < fl - cvar("g_lms_last_join"))\r
1629                         return 0;\r
1630 \r
1631         return bound(1, lms_lowest_lives, fl);\r
1632 }\r
1633 \r
1634 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)\r
1635 // they win. Otherwise the defending team wins once the timelimit passes.\r
1636 void assault_new_round();\r
1637 float WinningCondition_Assault()\r
1638 {\r
1639         local float status;\r
1640 \r
1641         WinningConditionHelper(); // set worldstatus\r
1642 \r
1643         status = WINNING_NO;\r
1644         // as the timelimit has not yet passed just assume the defending team will win\r
1645         if(assault_attacker_team == COLOR_TEAM1)\r
1646         {\r
1647                 SetWinners(team, COLOR_TEAM2);\r
1648         }\r
1649         else\r
1650         {\r
1651                 SetWinners(team, COLOR_TEAM1);\r
1652         }\r
1653 \r
1654         local entity ent;\r
1655         ent = find(world, classname, "target_assault_roundend");\r
1656         if(ent)\r
1657         {\r
1658                 if(ent.winning) // round end has been triggered by attacking team\r
1659                 {\r
1660                         bprint("ASSAULT: round completed...\n");\r
1661                         SetWinners(team, assault_attacker_team);\r
1662 \r
1663                         TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));\r
1664 \r
1665                         if(ent.cnt == 1) // this was the second round\r
1666                         {\r
1667                                 status = WINNING_YES;\r
1668                         }\r
1669                         else\r
1670                         {\r
1671                                 local entity oldself;\r
1672                                 oldself = self;\r
1673                                 self = ent;\r
1674                                 assault_new_round();\r
1675                                 self = oldself;\r
1676                         }\r
1677                 }\r
1678         }\r
1679 \r
1680         return status;\r
1681 }\r
1682 \r
1683 // LMS winning condition: game terminates if and only if there's at most one\r
1684 // one player who's living lives. Top two scores being equal cancels the time\r
1685 // limit.\r
1686 float WinningCondition_LMS()\r
1687 {\r
1688         entity head, head2;\r
1689         float have_player;\r
1690         float have_players;\r
1691         float l;\r
1692 \r
1693         have_player = FALSE;\r
1694         have_players = FALSE;\r
1695         l = LMS_NewPlayerLives();\r
1696 \r
1697         head = find(world, classname, "player");\r
1698         if(head)\r
1699                 have_player = TRUE;\r
1700         head2 = find(head, classname, "player");\r
1701         if(head2)\r
1702                 have_players = TRUE;\r
1703 \r
1704         if(have_player)\r
1705         {\r
1706                 // we have at least one player\r
1707                 if(have_players)\r
1708                 {\r
1709                         // two or more active players - continue with the game\r
1710                 }\r
1711                 else\r
1712                 {\r
1713                         // exactly one player?\r
1714 \r
1715                         ClearWinners();\r
1716                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out\r
1717 \r
1718                         if(l)\r
1719                         {\r
1720                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue\r
1721                                 return WINNING_NO;\r
1722                         }\r
1723                         else\r
1724                         {\r
1725                                 // a winner!\r
1726                                 // and assign him his first place\r
1727                                 PlayerScore_Add(head, SP_LMS_RANK, 1);\r
1728                                 return WINNING_YES;\r
1729                         }\r
1730                 }\r
1731         }\r
1732         else\r
1733         {\r
1734                 // nobody is playing at all...\r
1735                 if(l)\r
1736                 {\r
1737                         // wait for players...\r
1738                 }\r
1739                 else\r
1740                 {\r
1741                         // SNAFU (maybe a draw game?)\r
1742                         ClearWinners();\r
1743                         dprint("No players, ending game.\n");\r
1744                         return WINNING_YES;\r
1745                 }\r
1746         }\r
1747 \r
1748         // When we get here, we have at least two players who are actually LIVING,\r
1749         // now check if the top two players have equal score.\r
1750         WinningConditionHelper();\r
1751 \r
1752         ClearWinners();\r
1753         if(WinningConditionHelper_winner)\r
1754                 WinningConditionHelper_winner.winning = TRUE;\r
1755         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)\r
1756                 return WINNING_NEVER;\r
1757 \r
1758         // Top two have different scores? Way to go for our beloved TIMELIMIT!\r
1759         return WINNING_NO;\r
1760 }\r
1761 \r
1762 void ShuffleMaplist()\r
1763 {\r
1764         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));\r
1765 }\r
1766 \r
1767 float leaderfrags;\r
1768 float WinningCondition_Scores(float limit, float leadlimit)\r
1769 {\r
1770         // TODO make everything use THIS winning condition (except LMS)\r
1771         WinningConditionHelper();\r
1772 \r
1773         if(teams_matter)\r
1774         {\r
1775                 team1_score = TeamScore_GetCompareValue(COLOR_TEAM1);\r
1776                 team2_score = TeamScore_GetCompareValue(COLOR_TEAM2);\r
1777                 team3_score = TeamScore_GetCompareValue(COLOR_TEAM3);\r
1778                 team4_score = TeamScore_GetCompareValue(COLOR_TEAM4);\r
1779         }\r
1780 \r
1781         ClearWinners();\r
1782         if(WinningConditionHelper_winner)\r
1783                 WinningConditionHelper_winner.winning = 1;\r
1784         if(WinningConditionHelper_winnerteam >= 0)\r
1785                 SetWinners(team, WinningConditionHelper_winnerteam);\r
1786 \r
1787         if(WinningConditionHelper_lowerisbetter)\r
1788         {\r
1789                 WinningConditionHelper_topscore = -WinningConditionHelper_topscore;\r
1790                 WinningConditionHelper_secondscore = -WinningConditionHelper_secondscore;\r
1791                 limit = -limit;\r
1792         }\r
1793 \r
1794         if(WinningConditionHelper_zeroisworst)\r
1795                 leadlimit = 0; // not supported in this mode\r
1796 \r
1797         if(g_dm || g_tdm || g_arena || g_ca || (g_race && !g_race_qualifying))\r
1798         // these modes always score in increments of 1, thus this makes sense\r
1799         {\r
1800                 if(leaderfrags != WinningConditionHelper_topscore)\r
1801                 {\r
1802                         leaderfrags = WinningConditionHelper_topscore;\r
1803 \r
1804                         if (limit)\r
1805                         if (leaderfrags == limit - 1)\r
1806                                 Announce("1fragleft");\r
1807                         else if (leaderfrags == limit - 2)\r
1808                                 Announce("2fragsleft");\r
1809                         else if (leaderfrags == limit - 3)\r
1810                                 Announce("3fragsleft");\r
1811                 }\r
1812         }\r
1813 \r
1814         return GetWinningCode(\r
1815                 WinningConditionHelper_topscore &&\r
1816                 (\r
1817                         (limit && (WinningConditionHelper_topscore >= limit))\r
1818                         ||\r
1819                         (leadlimit && (WinningConditionHelper_topscore - WinningConditionHelper_secondscore >= leadlimit))\r
1820                 ),\r
1821                 WinningConditionHelper_equality\r
1822         );\r
1823 }\r
1824 \r
1825 float WinningCondition_Race(float fraglimit)\r
1826 {\r
1827         float wc;\r
1828         entity p;\r
1829         float n, c;\r
1830 \r
1831         n = 0;\r
1832         c = 0;\r
1833         FOR_EACH_PLAYER(p)\r
1834         {\r
1835                 ++n;\r
1836                 if(p.race_completed)\r
1837                         ++c;\r
1838         }\r
1839         if(n && (n == c))\r
1840                 return WINNING_YES;\r
1841         wc = WinningCondition_Scores(fraglimit, 0);\r
1842 \r
1843         // ALWAYS initiate overtime, unless EVERYONE has finished the race!\r
1844         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)\r
1845         // do NOT support equality when the laps are all raced!\r
1846                 return WINNING_STARTSUDDENDEATHOVERTIME;\r
1847         else\r
1848                 return WINNING_NEVER;\r
1849         return wc;\r
1850 }\r
1851 \r
1852 void ReadyRestart();\r
1853 float WinningCondition_QualifyingThenRace(float limit)\r
1854 {\r
1855         float wc;\r
1856         wc = WinningCondition_Scores(limit, 0);\r
1857 \r
1858         // NEVER initiate overtime\r
1859         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)\r
1860         {\r
1861                 return WINNING_YES;\r
1862         }\r
1863 \r
1864         return wc;\r
1865 }\r
1866 \r
1867 float WinningCondition_RanOutOfSpawns()\r
1868 {\r
1869         entity head;\r
1870 \r
1871         if(have_team_spawns <= 0)\r
1872                 return WINNING_NO;\r
1873 \r
1874         if(!some_spawn_has_been_used)\r
1875                 return WINNING_NO;\r
1876 \r
1877         team1_score = team2_score = team3_score = team4_score = 0;\r
1878 \r
1879         FOR_EACH_PLAYER(head) if(head.deadflag == DEAD_NO)\r
1880         {\r
1881                 if(head.team == COLOR_TEAM1)\r
1882                         team1_score = 1;\r
1883                 else if(head.team == COLOR_TEAM2)\r
1884                         team2_score = 1;\r
1885                 else if(head.team == COLOR_TEAM3)\r
1886                         team3_score = 1;\r
1887                 else if(head.team == COLOR_TEAM4)\r
1888                         team4_score = 1;\r
1889         }\r
1890 \r
1891         for(head = world; (head = find(head, classname, "info_player_deathmatch")) != world; )\r
1892         {\r
1893                 if(head.team == COLOR_TEAM1)\r
1894                         team1_score = 1;\r
1895                 else if(head.team == COLOR_TEAM2)\r
1896                         team2_score = 1;\r
1897                 else if(head.team == COLOR_TEAM3)\r
1898                         team3_score = 1;\r
1899                 else if(head.team == COLOR_TEAM4)\r
1900                         team4_score = 1;\r
1901         }\r
1902 \r
1903         ClearWinners();\r
1904         if(team1_score + team2_score + team3_score + team4_score == 0)\r
1905         {\r
1906                 checkrules_equality = TRUE;\r
1907                 return WINNING_YES;\r
1908         }\r
1909         else if(team1_score + team2_score + team3_score + team4_score == 1)\r
1910         {\r
1911                 float t, i;\r
1912                 if(team1_score) t = COLOR_TEAM1;\r
1913                 if(team2_score) t = COLOR_TEAM2;\r
1914                 if(team3_score) t = COLOR_TEAM3;\r
1915                 if(team4_score) t = COLOR_TEAM4;\r
1916                 CheckAllowedTeams(world);\r
1917                 for(i = 0; i < MAX_TEAMSCORE; ++i)\r
1918                 {\r
1919                         if(t != COLOR_TEAM1) if(c1 >= 0) TeamScore_AddToTeam(COLOR_TEAM1, i, -1000);\r
1920                         if(t != COLOR_TEAM2) if(c2 >= 0) TeamScore_AddToTeam(COLOR_TEAM2, i, -1000);\r
1921                         if(t != COLOR_TEAM3) if(c3 >= 0) TeamScore_AddToTeam(COLOR_TEAM3, i, -1000);\r
1922                         if(t != COLOR_TEAM4) if(c4 >= 0) TeamScore_AddToTeam(COLOR_TEAM4, i, -1000);\r
1923                 }\r
1924 \r
1925                 AddWinners(team, t);\r
1926                 return WINNING_YES;\r
1927         }\r
1928         else\r
1929                 return WINNING_NO;\r
1930 }\r
1931 \r
1932 /*\r
1933 ============\r
1934 CheckRules_World\r
1935 \r
1936 Exit deathmatch games upon conditions\r
1937 ============\r
1938 */\r
1939 void CheckRules_World()\r
1940 {\r
1941         float timelimit;\r
1942         float fraglimit;\r
1943         float leadlimit;\r
1944 \r
1945         VoteThink();\r
1946         MapVote_Think();\r
1947 \r
1948         SetDefaultAlpha();\r
1949 \r
1950         /*\r
1951         MapVote_Think should now do that part\r
1952         if (intermission_running)\r
1953                 if (time >= intermission_exittime + 60)\r
1954                 {\r
1955                         if(!DoNextMapOverride())\r
1956                                 GotoNextMap();\r
1957                         return;\r
1958                 }\r
1959         */\r
1960 \r
1961         if (gameover)   // someone else quit the game already\r
1962         {\r
1963                 if(player_count == 0) // Nobody there? Then let's go to the next map\r
1964                         MapVote_Start();\r
1965                         // this will actually check the player count in the next frame\r
1966                         // again, but this shouldn't hurt\r
1967                 return;\r
1968         }\r
1969 \r
1970         timelimit = cvar("timelimit") * 60;\r
1971         fraglimit = cvar("fraglimit");\r
1972         leadlimit = cvar("leadlimit");\r
1973 \r
1974         if(inWarmupStage || time <= game_starttime) // NOTE: this is <= to prevent problems in the very tic where the game starts\r
1975         {\r
1976                 if(timelimit > 0)\r
1977                         timelimit = 0; // timelimit is not made for warmup\r
1978                 if(fraglimit > 0)\r
1979                         fraglimit = 0; // no fraglimit for now\r
1980                 leadlimit = 0; // no leadlimit for now\r
1981         }\r
1982 \r
1983         if(g_onslaught)\r
1984                 timelimit = 0; // ONS has its own overtime rule\r
1985 \r
1986         if(timelimit > 0)\r
1987         {\r
1988                 timelimit += game_starttime;\r
1989         }\r
1990         else if (timelimit < 0)\r
1991         {\r
1992                 // endmatch\r
1993                 NextLevel();\r
1994                 return;\r
1995         }\r
1996 \r
1997         float wantovertime;\r
1998         wantovertime = 0;\r
1999 \r
2000         if(checkrules_suddendeathend)\r
2001         {\r
2002                 if(!checkrules_suddendeathwarning)\r
2003                 {\r
2004                         checkrules_suddendeathwarning = TRUE;\r
2005                         if(g_race && !g_race_qualifying)\r
2006                                 bcenterprint("^3Everyone, finish your lap! The race is over!");\r
2007                         else\r
2008                                 bcenterprint("^3Now playing ^1OVERTIME^3!\n\n^3Keep fragging until we have a ^1winner^3!");\r
2009                 }\r
2010         }\r
2011         else\r
2012         {\r
2013                 if (timelimit && time >= timelimit)\r
2014                 {\r
2015                         if(g_race && (g_race_qualifying == 2) && timelimit > 0)\r
2016                         {\r
2017                                 float totalplayers;\r
2018                                 float playerswithlaps;\r
2019                                 float readyplayers;\r
2020                                 entity head;\r
2021                                 totalplayers = playerswithlaps = readyplayers = 0;\r
2022                                 FOR_EACH_PLAYER(head)\r
2023                                 {\r
2024                                         ++totalplayers;\r
2025                                         if(PlayerScore_Add(head, SP_RACE_FASTEST, 0))\r
2026                                                 ++playerswithlaps;\r
2027                                         if(head.ready)\r
2028                                                 ++readyplayers;\r
2029                                 }\r
2030 \r
2031                                 // at least 2 of the players have completed a lap: start the RACE\r
2032                                 // otherwise, the players should end the qualifying on their own\r
2033                                 if(readyplayers || playerswithlaps >= 2)\r
2034                                 {\r
2035                                         checkrules_suddendeathend = 0;\r
2036                                         ReadyRestart(); // go to race\r
2037                                         return;\r
2038                                 }\r
2039                                 else\r
2040                                         wantovertime |= InitiateSuddenDeath();\r
2041                         }\r
2042                         else\r
2043                                 wantovertime |= InitiateSuddenDeath();\r
2044                 }\r
2045         }\r
2046 \r
2047         if (checkrules_suddendeathend && time >= checkrules_suddendeathend)\r
2048         {\r
2049                 NextLevel();\r
2050                 return;\r
2051         }\r
2052 \r
2053         float checkrules_status;\r
2054         checkrules_status = WinningCondition_RanOutOfSpawns();\r
2055         if(checkrules_status == WINNING_YES)\r
2056         {\r
2057                 bprint("Hey! Someone ran out of spawns!\n");\r
2058         }\r
2059         else if(g_race && !g_race_qualifying && timelimit >= 0)\r
2060         {\r
2061                 checkrules_status = WinningCondition_Race(fraglimit);\r
2062                 //print("WC_RACE yields ", ftos(checkrules_status), "\n");\r
2063         }\r
2064         else if(g_race && g_race_qualifying == 2 && timelimit >= 0)\r
2065         {\r
2066                 checkrules_status = WinningCondition_QualifyingThenRace(fraglimit);\r
2067                 //print("WC_QUALIFYING_THEN_RACE yields ", ftos(checkrules_status), "\n");\r
2068         }\r
2069         else if(g_assault)\r
2070         {\r
2071                 checkrules_status = WinningCondition_Assault(); // TODO remove this?\r
2072         }\r
2073         else if(g_lms)\r
2074         {\r
2075                 checkrules_status = WinningCondition_LMS();\r
2076         }\r
2077         else if (g_onslaught)\r
2078         {\r
2079                 checkrules_status = WinningCondition_Onslaught(); // TODO remove this?\r
2080         }\r
2081         else\r
2082         {\r
2083                 checkrules_status = WinningCondition_Scores(fraglimit, leadlimit);\r
2084                 //print("WC_SCORES yields ", ftos(checkrules_status), "\n");\r
2085         }\r
2086 \r
2087         if(checkrules_status == WINNING_STARTSUDDENDEATHOVERTIME)\r
2088         {\r
2089                 checkrules_status = WINNING_NEVER;\r
2090                 checkrules_overtimesadded = -1;\r
2091                 wantovertime |= InitiateSuddenDeath();\r
2092         }\r
2093 \r
2094         if(checkrules_status == WINNING_NEVER)\r
2095                 // equality cases! Nobody wins if the overtime ends in a draw.\r
2096                 ClearWinners();\r
2097 \r
2098         if(wantovertime)\r
2099         {\r
2100                 if(checkrules_status == WINNING_NEVER)\r
2101                         InitiateOvertime();\r
2102                 else\r
2103                         checkrules_status = WINNING_YES;\r
2104         }\r
2105 \r
2106         if(checkrules_suddendeathend)\r
2107                 if(checkrules_status != WINNING_NEVER || time >= checkrules_suddendeathend)\r
2108                         checkrules_status = WINNING_YES;\r
2109 \r
2110         if(checkrules_status == WINNING_YES)\r
2111         {\r
2112                 //print("WINNING\n");\r
2113                 NextLevel();\r
2114         }\r
2115 };\r
2116 \r
2117 float mapvote_nextthink;\r
2118 float mapvote_initialized;\r
2119 float mapvote_keeptwotime;\r
2120 float mapvote_timeout;\r
2121 string mapvote_message;\r
2122 #define MAPVOTE_SCREENSHOT_DIRS_COUNT 4\r
2123 string mapvote_screenshot_dirs[MAPVOTE_SCREENSHOT_DIRS_COUNT];\r
2124 float mapvote_screenshot_dirs_count;\r
2125 \r
2126 float mapvote_count;\r
2127 float mapvote_count_real;\r
2128 string mapvote_maps[MAPVOTE_COUNT];\r
2129 float mapvote_maps_screenshot_dir[MAPVOTE_COUNT];\r
2130 string mapvote_maps_pakfile[MAPVOTE_COUNT];\r
2131 float mapvote_maps_suggested[MAPVOTE_COUNT];\r
2132 string mapvote_suggestions[MAPVOTE_COUNT];\r
2133 float mapvote_suggestion_ptr;\r
2134 float mapvote_maxlen;\r
2135 float mapvote_voters;\r
2136 float mapvote_votes[MAPVOTE_COUNT];\r
2137 float mapvote_run;\r
2138 float mapvote_detail;\r
2139 float mapvote_abstain;\r
2140 .float mapvote;\r
2141 \r
2142 void MapVote_ClearAllVotes()\r
2143 {\r
2144         FOR_EACH_CLIENT(other)\r
2145                 other.mapvote = 0;\r
2146 }\r
2147 \r
2148 string MapVote_Suggest(string m)\r
2149 {\r
2150         float i;\r
2151         if(m == "")\r
2152                 return "That's not how to use this command.";\r
2153         if(!cvar("g_maplist_votable_suggestions"))\r
2154                 return "Suggestions are not accepted on this server.";\r
2155         if(mapvote_initialized)\r
2156                 return "Can't suggest - voting is already in progress!";\r
2157         m = MapInfo_FixName(m);\r
2158         if(!m)\r
2159                 return "The map you suggested is not available on this server.";\r
2160         if(!cvar("g_maplist_votable_suggestions_override_mostrecent"))\r
2161                 if(Map_IsRecent(m))\r
2162                         return "This server does not allow for recent maps to be played again. Please be patient for some rounds.";\r
2163 \r
2164         if(!MapInfo_CheckMap(m))\r
2165                 return "The map you suggested does not support the current game mode.";\r
2166         for(i = 0; i < mapvote_suggestion_ptr; ++i)\r
2167                 if(mapvote_suggestions[i] == m)\r
2168                         return "This map was already suggested.";\r
2169         if(mapvote_suggestion_ptr >= MAPVOTE_COUNT)\r
2170         {\r
2171                 i = floor(random() * mapvote_suggestion_ptr);\r
2172         }\r
2173         else\r
2174         {\r
2175                 i = mapvote_suggestion_ptr;\r
2176                 mapvote_suggestion_ptr += 1;\r
2177         }\r
2178         if(mapvote_suggestions[i] != "")\r
2179                 strunzone(mapvote_suggestions[i]);\r
2180         mapvote_suggestions[i] = strzone(m);\r
2181         if(cvar("sv_eventlog"))\r
2182                 GameLogEcho(strcat(":vote:suggested:", m, ":", ftos(self.playerid)));\r
2183         return strcat("Suggestion of ", m, " accepted.");\r
2184 }\r
2185 \r
2186 void MapVote_AddVotable(string nextMap, float isSuggestion)\r
2187 {\r
2188         float j, i, o;\r
2189         string pakfile, mapfile;\r
2190 \r
2191         if(nextMap == "")\r
2192                 return;\r
2193         for(j = 0; j < mapvote_count; ++j)\r
2194                 if(mapvote_maps[j] == nextMap)\r
2195                         return;\r
2196         if(strlen(nextMap) > mapvote_maxlen)\r
2197                 mapvote_maxlen = strlen(nextMap);\r
2198         mapvote_maps[mapvote_count] = strzone(nextMap);\r
2199         mapvote_maps_suggested[mapvote_count] = isSuggestion;\r
2200 \r
2201         for(i = 0; i < mapvote_screenshot_dirs_count; ++i)\r
2202         {\r
2203                 mapfile = strcat(mapvote_screenshot_dirs[i], "/", mapvote_maps[i]);\r
2204                 pakfile = whichpack(strcat(mapfile, ".tga"));\r
2205                 if(pakfile == "")\r
2206                         pakfile = whichpack(strcat(mapfile, ".jpg"));\r
2207                 if(pakfile == "")\r
2208                         pakfile = whichpack(strcat(mapfile, ".png"));\r
2209                 if(pakfile != "")\r
2210                         break;\r
2211         }\r
2212         if(i >= mapvote_screenshot_dirs_count)\r
2213                 i = 0; // FIXME maybe network this error case, as that means there is no mapshot on the server?\r
2214         for(o = strstr(pakfile, "/", 0)+1; o > 0; o = strstr(pakfile, "/", 0)+1)\r
2215                 pakfile = substring(pakfile, o, -1);\r
2216 \r
2217         mapvote_maps_screenshot_dir[mapvote_count] = i;\r
2218         mapvote_maps_pakfile[mapvote_count] = strzone(pakfile);\r
2219 \r
2220         mapvote_count += 1;\r
2221 }\r
2222 \r
2223 void MapVote_Spawn();\r
2224 void MapVote_Init()\r
2225 {\r
2226         float i;\r
2227         float nmax, smax;\r
2228 \r
2229         if(cvar("g_campaign"))\r
2230                 return;\r
2231 \r
2232         MapVote_ClearAllVotes();\r
2233 \r
2234         mapvote_count = 0;\r
2235         mapvote_detail = !cvar("g_maplist_votable_nodetail");\r
2236         mapvote_abstain = cvar("g_maplist_votable_abstain");\r
2237 \r
2238         if(mapvote_abstain)\r
2239                 nmax = min(MAPVOTE_COUNT - 1, cvar("g_maplist_votable"));\r
2240         else\r
2241                 nmax = min(MAPVOTE_COUNT, cvar("g_maplist_votable"));\r
2242         smax = min3(nmax, cvar("g_maplist_votable_suggestions"), mapvote_suggestion_ptr);\r
2243 \r
2244         // we need this for AddVotable, as that cycles through the screenshot dirs\r
2245         mapvote_screenshot_dirs_count = tokenize_console(cvar_string("g_maplist_votable_screenshot_dir"));\r
2246         if(mapvote_screenshot_dirs_count == 0)\r
2247                 mapvote_screenshot_dirs_count = tokenize_console("maps levelshots");\r
2248         mapvote_screenshot_dirs_count = min(mapvote_screenshot_dirs_count, MAPVOTE_SCREENSHOT_DIRS_COUNT);\r
2249         for(i = 0; i < mapvote_screenshot_dirs_count; ++i)\r
2250                 mapvote_screenshot_dirs[i] = strzone(argv(i));\r
2251 \r
2252         if(mapvote_suggestion_ptr)\r
2253                 for(i = 0; i < 100 && mapvote_count < smax; ++i)\r
2254                         MapVote_AddVotable(mapvote_suggestions[floor(random() * mapvote_suggestion_ptr)], TRUE);\r
2255 \r
2256         for(i = 0; i < 100 && mapvote_count < nmax; ++i)\r
2257                 MapVote_AddVotable(GetNextMap(), FALSE);\r
2258 \r
2259         if(mapvote_count == 0)\r
2260         {\r
2261                 bprint( "Maplist contains no single playable map!  Resetting it to default map list.\n" );\r
2262                 cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));\r
2263                 if(cvar("g_maplist_shuffle"))\r
2264                         ShuffleMaplist();\r
2265                 localcmd("\nmenu_cmd sync\n");\r
2266                 for(i = 0; i < 100 && mapvote_count < nmax; ++i)\r
2267                         MapVote_AddVotable(GetNextMap(), FALSE);\r
2268         }\r
2269 \r
2270         mapvote_count_real = mapvote_count;\r
2271         if(mapvote_abstain)\r
2272                 MapVote_AddVotable("don't care", 0);\r
2273 \r
2274         //dprint("mapvote count is ", ftos(mapvote_count), "\n");\r
2275 \r
2276         mapvote_keeptwotime = time + cvar("g_maplist_votable_keeptwotime");\r
2277         mapvote_timeout = time + cvar("g_maplist_votable_timeout");\r
2278         if(mapvote_count_real < 3 || mapvote_keeptwotime <= time)\r
2279                 mapvote_keeptwotime = 0;\r
2280         mapvote_message = "Choose a map and press its key!";\r
2281 \r
2282         MapVote_Spawn();\r
2283 }\r
2284 \r
2285 void MapVote_SendPicture(float id)\r
2286 {\r
2287         msg_entity = self;\r
2288         WriteByte(MSG_ONE, SVC_TEMPENTITY);\r
2289         WriteByte(MSG_ONE, TE_CSQC_PICTURE);\r
2290         WriteByte(MSG_ONE, id);\r
2291         WritePicture(MSG_ONE, strcat(mapvote_screenshot_dirs[mapvote_maps_screenshot_dir[id]], "/", mapvote_maps[id]), 3072);\r
2292 }\r
2293 \r
2294 float GameCommand_MapVote(string cmd)\r
2295 {\r
2296         if(!intermission_running)\r
2297                 return FALSE;\r
2298 \r
2299         if(cmd == "mv_getpic")\r
2300         {\r
2301                 MapVote_SendPicture(stof(argv(1)));\r
2302                 return TRUE;\r
2303         }\r
2304 \r
2305         return FALSE;\r
2306 }\r
2307 \r
2308 float MapVote_GetMapMask()\r
2309 {\r
2310         float mask, i, power;\r
2311         mask = 0;\r
2312         for(i = 0, power = 1; i < mapvote_count; ++i, power *= 2)\r
2313                 if(mapvote_maps[i] != "")\r
2314                         mask |= power;\r
2315         return mask;\r
2316 }\r
2317 \r
2318 entity mapvote_ent;\r
2319 float MapVote_SendEntity(entity to, float sf)\r
2320 {\r
2321         float i;\r
2322 \r
2323         if(sf & 1)\r
2324                 sf &~= 2; // if we send 1, we don't need to also send 2\r
2325 \r
2326         WriteByte(MSG_ENTITY, ENT_CLIENT_MAPVOTE);\r
2327         WriteByte(MSG_ENTITY, sf);\r
2328 \r
2329         if(sf & 1)\r
2330         {\r
2331                 // flag 1 == initialization\r
2332                 for(i = 0; i < mapvote_screenshot_dirs_count; ++i)\r
2333                         WriteString(MSG_ENTITY, mapvote_screenshot_dirs[i]);\r
2334                 WriteString(MSG_ENTITY, "");\r
2335                 WriteByte(MSG_ENTITY, mapvote_count);\r
2336                 WriteByte(MSG_ENTITY, mapvote_abstain);\r
2337                 WriteByte(MSG_ENTITY, mapvote_detail);\r
2338                 WriteCoord(MSG_ENTITY, mapvote_timeout);\r
2339                 if(mapvote_count <= 8)\r
2340                         WriteByte(MSG_ENTITY, MapVote_GetMapMask());\r
2341                 else\r
2342                         WriteShort(MSG_ENTITY, MapVote_GetMapMask());\r
2343                 for(i = 0; i < mapvote_count; ++i)\r
2344                         if(mapvote_maps[i] != "")\r
2345                         {\r
2346                                 if(mapvote_abstain && i == mapvote_count - 1)\r
2347                                 {\r
2348                                         WriteString(MSG_ENTITY, ""); // abstain needs no text\r
2349                                         WriteString(MSG_ENTITY, ""); // abstain needs no pack\r
2350                                         WriteByte(MSG_ENTITY, 0); // abstain needs no screenshot dir\r
2351                                 }\r
2352                                 else\r
2353                                 {\r
2354                                         WriteString(MSG_ENTITY, mapvote_maps[i]);\r
2355                                         WriteString(MSG_ENTITY, mapvote_maps_pakfile[i]);\r
2356                                         WriteByte(MSG_ENTITY, mapvote_maps_screenshot_dir[i]);\r
2357                                 }\r
2358                         }\r
2359         }\r
2360 \r
2361         if(sf & 2)\r
2362         {\r
2363                 // flag 2 == update of mask\r
2364                 if(mapvote_count <= 8)\r
2365                         WriteByte(MSG_ENTITY, MapVote_GetMapMask());\r
2366                 else\r
2367                         WriteShort(MSG_ENTITY, MapVote_GetMapMask());\r
2368         }\r
2369 \r
2370         if(sf & 4)\r
2371         {\r
2372                 if(mapvote_detail)\r
2373                         for(i = 0; i < mapvote_count; ++i)\r
2374                                 if(mapvote_maps[i] != "")\r
2375                                         WriteByte(MSG_ENTITY, mapvote_votes[i]);\r
2376 \r
2377                 WriteByte(MSG_ENTITY, to.mapvote);\r
2378         }\r
2379 \r
2380         return TRUE;\r
2381 }\r
2382 \r
2383 void MapVote_Spawn()\r
2384 {\r
2385         Net_LinkEntity(mapvote_ent = spawn(), FALSE, 0, MapVote_SendEntity);\r
2386 }\r
2387 \r
2388 void MapVote_TouchMask()\r
2389 {\r
2390         mapvote_ent.SendFlags |= 2;\r
2391 }\r
2392 \r
2393 void MapVote_TouchVotes(entity voter)\r
2394 {\r
2395         mapvote_ent.SendFlags |= 4;\r
2396 }\r
2397 \r
2398 float MapVote_Finished(float mappos)\r
2399 {\r
2400         string result;\r
2401         float i;\r
2402         float didntvote;\r
2403 \r
2404         if(cvar("sv_eventlog"))\r
2405         {\r
2406                 result = strcat(":vote:finished:", mapvote_maps[mappos]);\r
2407                 result = strcat(result, ":", ftos(mapvote_votes[mappos]), "::");\r
2408                 didntvote = mapvote_voters;\r
2409                 for(i = 0; i < mapvote_count; ++i)\r
2410                         if(mapvote_maps[i] != "")\r
2411                         {\r
2412                                 didntvote -= mapvote_votes[i];\r
2413                                 if(i != mappos)\r
2414                                 {\r
2415                                         result = strcat(result, ":", mapvote_maps[i]);\r
2416                                         result = strcat(result, ":", ftos(mapvote_votes[i]));\r
2417                                 }\r
2418                         }\r
2419                 result = strcat(result, ":didn't vote:", ftos(didntvote));\r
2420 \r
2421                 GameLogEcho(result);\r
2422                 if(mapvote_maps_suggested[mappos])\r
2423                         GameLogEcho(strcat(":vote:suggestion_accepted:", mapvote_maps[mappos]));\r
2424         }\r
2425 \r
2426         FOR_EACH_REALCLIENT(other)\r
2427                 FixClientCvars(other);\r
2428 \r
2429         Map_Goto_SetStr(mapvote_maps[mappos]);\r
2430         Map_Goto();\r
2431         alreadychangedlevel = TRUE;\r
2432         return TRUE;\r
2433 }\r
2434 void MapVote_CheckRules_1()\r
2435 {\r
2436         float i;\r
2437 \r
2438         for(i = 0; i < mapvote_count; ++i) if(mapvote_maps[i] != "")\r
2439         {\r
2440                 //dprint("Map ", ftos(i), ": "); dprint(mapvote_maps[i], "\n");\r
2441                 mapvote_votes[i] = 0;\r
2442         }\r
2443 \r
2444         mapvote_voters = 0;\r
2445         FOR_EACH_REALCLIENT(other)\r
2446         {\r
2447                 ++mapvote_voters;\r
2448                 if(other.mapvote)\r
2449                 {\r
2450                         i = other.mapvote - 1;\r
2451                         //dprint("Player ", other.netname, " vote = ", ftos(other.mapvote - 1), "\n");\r
2452                         mapvote_votes[i] = mapvote_votes[i] + 1;\r
2453                 }\r
2454         }\r
2455 }\r
2456 \r
2457 float MapVote_CheckRules_2()\r
2458 {\r
2459         float i;\r
2460         float firstPlace, secondPlace;\r
2461         float firstPlaceVotes, secondPlaceVotes;\r
2462         float mapvote_voters_real;\r
2463         string result;\r
2464 \r
2465         if(mapvote_count_real == 1)\r
2466                 return MapVote_Finished(0);\r
2467 \r
2468         mapvote_voters_real = mapvote_voters;\r
2469         if(mapvote_abstain)\r
2470                 mapvote_voters_real -= mapvote_votes[mapvote_count - 1];\r
2471 \r
2472         RandomSelection_Init();\r
2473         for(i = 0; i < mapvote_count_real; ++i) if(mapvote_maps[i] != "")\r
2474                 RandomSelection_Add(world, i, string_null, 1, mapvote_votes[i]);\r
2475         firstPlace = RandomSelection_chosen_float;\r
2476         firstPlaceVotes = RandomSelection_best_priority;\r
2477         //dprint("First place: ", ftos(firstPlace), "\n");\r
2478         //dprint("First place votes: ", ftos(firstPlaceVotes), "\n");\r
2479 \r
2480         RandomSelection_Init();\r
2481         for(i = 0; i < mapvote_count_real; ++i) if(mapvote_maps[i] != "")\r
2482                 if(i != firstPlace)\r
2483                         RandomSelection_Add(world, i, string_null, 1, mapvote_votes[i]);\r
2484         secondPlace = RandomSelection_chosen_float;\r
2485         secondPlaceVotes = RandomSelection_best_priority;\r
2486         //dprint("Second place: ", ftos(secondPlace), "\n");\r
2487         //dprint("Second place votes: ", ftos(secondPlaceVotes), "\n");\r
2488 \r
2489         if(firstPlace == -1)\r
2490                 error("No first place in map vote... WTF?");\r
2491 \r
2492         if(secondPlace == -1 || time > mapvote_timeout || (mapvote_voters_real - firstPlaceVotes) < firstPlaceVotes)\r
2493                 return MapVote_Finished(firstPlace);\r
2494 \r
2495         if(mapvote_keeptwotime)\r
2496                 if(time > mapvote_keeptwotime || (mapvote_voters_real - firstPlaceVotes - secondPlaceVotes) < secondPlaceVotes)\r
2497                 {\r
2498                         float didntvote;\r
2499                         MapVote_TouchMask();\r
2500                         mapvote_message = "Now decide between the TOP TWO!";\r
2501                         mapvote_keeptwotime = 0;\r
2502                         result = strcat(":vote:keeptwo:", mapvote_maps[firstPlace]);\r
2503                         result = strcat(result, ":", ftos(firstPlaceVotes));\r
2504                         result = strcat(result, ":", mapvote_maps[secondPlace]);\r
2505                         result = strcat(result, ":", ftos(secondPlaceVotes), "::");\r
2506                         didntvote = mapvote_voters;\r
2507                         for(i = 0; i < mapvote_count; ++i)\r
2508                                 if(mapvote_maps[i] != "")\r
2509                                 {\r
2510                                         didntvote -= mapvote_votes[i];\r
2511                                         if(i != firstPlace)\r
2512                                                 if(i != secondPlace)\r
2513                                                 {\r
2514                                                         result = strcat(result, ":", mapvote_maps[i]);\r
2515                                                         result = strcat(result, ":", ftos(mapvote_votes[i]));\r
2516                                                         if(i < mapvote_count_real)\r
2517                                                         {\r
2518                                                                 strunzone(mapvote_maps[i]);\r
2519                                                                 mapvote_maps[i] = "";\r
2520                                                                 strunzone(mapvote_maps_pakfile[i]);\r
2521                                                                 mapvote_maps_pakfile[i] = "";\r
2522                                                         }\r
2523                                                 }\r
2524                                 }\r
2525                         result = strcat(result, ":didn't vote:", ftos(didntvote));\r
2526                         if(cvar("sv_eventlog"))\r
2527                                 GameLogEcho(result);\r
2528                 }\r
2529 \r
2530         return FALSE;\r
2531 }\r
2532 void MapVote_Tick()\r
2533 {\r
2534         float keeptwo;\r
2535         float totalvotes;\r
2536 \r
2537         keeptwo = mapvote_keeptwotime;\r
2538         MapVote_CheckRules_1(); // count\r
2539         if(MapVote_CheckRules_2()) // decide\r
2540                 return;\r
2541 \r
2542         totalvotes = 0;\r
2543         FOR_EACH_REALCLIENT(other)\r
2544         {\r
2545                 // hide scoreboard again\r
2546                 if(other.health != 2342)\r
2547                 {\r
2548                         other.health = 2342;\r
2549                         other.impulse = 0;\r
2550                         if(clienttype(other) == CLIENTTYPE_REAL)\r
2551                         {\r
2552                                 msg_entity = other;\r
2553                                 WriteByte(MSG_ONE, SVC_FINALE);\r
2554                                 WriteString(MSG_ONE, "");\r
2555                         }\r
2556                 }\r
2557 \r
2558                 // clear possibly invalid votes\r
2559                 if(mapvote_maps[other.mapvote - 1] == "")\r
2560                         other.mapvote = 0;\r
2561                 // use impulses as new vote\r
2562                 if(other.impulse >= 1 && other.impulse <= mapvote_count)\r
2563                         if(mapvote_maps[other.impulse - 1] != "")\r
2564                         {\r
2565                                 other.mapvote = other.impulse;\r
2566                                 MapVote_TouchVotes(other);\r
2567                         }\r
2568                 other.impulse = 0;\r
2569 \r
2570                 if(other.mapvote)\r
2571                         ++totalvotes;\r
2572         }\r
2573 \r
2574         MapVote_CheckRules_1(); // just count\r
2575 }\r
2576 void MapVote_Start()\r
2577 {\r
2578         if(mapvote_run)\r
2579                 return;\r
2580 \r
2581         MapInfo_Enumerate();\r
2582         if(MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))\r
2583                 mapvote_run = TRUE;\r
2584 }\r
2585 void MapVote_Think()\r
2586 {\r
2587         if(!mapvote_run)\r
2588                 return;\r
2589 \r
2590         if(alreadychangedlevel)\r
2591                 return;\r
2592 \r
2593         if(time < mapvote_nextthink)\r
2594                 return;\r
2595         //dprint("tick\n");\r
2596 \r
2597         mapvote_nextthink = time + 0.5;\r
2598 \r
2599         if(!mapvote_initialized)\r
2600         {\r
2601                 if(cvar("rescan_pending") == 1)\r
2602                 {\r
2603                         cvar_set("rescan_pending", "2");\r
2604                         localcmd("fs_rescan\nrescan_pending 3\n");\r
2605                         return;\r
2606                 }\r
2607                 else if(cvar("rescan_pending") == 2)\r
2608                 {\r
2609                         return;\r
2610                 }\r
2611                 else if(cvar("rescan_pending") == 3)\r
2612                 {\r
2613                         // now build missing mapinfo files\r
2614                         if(!MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1))\r
2615                                 return;\r
2616 \r
2617                         // we're done, start the timer\r
2618                         cvar_set("rescan_pending", "0");\r
2619                 }\r
2620 \r
2621                 mapvote_initialized = TRUE;\r
2622                 if(DoNextMapOverride())\r
2623                         return;\r
2624                 if(!cvar("g_maplist_votable") || player_count <= 0)\r
2625                 {\r
2626                         GotoNextMap();\r
2627                         return;\r
2628                 }\r
2629                 MapVote_Init();\r
2630         }\r
2631 \r
2632         MapVote_Tick();\r
2633 };\r
2634 \r
2635 string GotoMap(string m)\r
2636 {\r
2637         if(!MapInfo_CheckMap(m))\r
2638                 return "The map you chose is not available on this server.";\r
2639         cvar_set("nextmap", m);\r
2640         cvar_set("timelimit", "-1");\r
2641         if(mapvote_initialized || alreadychangedlevel)\r
2642         {\r
2643                 if(DoNextMapOverride())\r
2644                         return "Map switch initiated.";\r
2645                 else\r
2646                         return "Hm... no. For some reason I like THIS map more.";\r
2647         }\r
2648         else\r
2649                 return "Map switch will happen after scoreboard.";\r
2650 }\r
2651 \r
2652 \r
2653 void EndFrame()\r
2654 {\r
2655         float altime;\r
2656         FOR_EACH_REALCLIENT(self)\r
2657         {\r
2658                 if(self.classname == "spectator")\r
2659                 {\r
2660                         if(self.enemy.typehitsound)\r
2661                                 play2(self, "misc/typehit.wav");\r
2662                         else if(self.enemy.hitsound && self.cvar_cl_hitsound)\r
2663                                 play2(self, "misc/hit.wav");\r
2664                 }\r
2665                 else\r
2666                 {\r
2667                         if(self.typehitsound)\r
2668                                 play2(self, "misc/typehit.wav");\r
2669                         else if(self.hitsound && self.cvar_cl_hitsound)\r
2670                                 play2(self, "misc/hit.wav");\r
2671                 }\r
2672         }\r
2673         altime = time + frametime * (1 + cvar("g_antilag_nudge"));\r
2674         // add 1 frametime because after this, engine SV_Physics\r
2675         // increases time by a frametime and then networks the frame\r
2676         // add another frametime because client shows everything with\r
2677         // 1 frame of lag (cl_nolerp 0). The last +1 however should not be\r
2678         // needed!\r
2679         FOR_EACH_CLIENT(self)\r
2680         {\r
2681                 self.hitsound = FALSE;\r
2682                 self.typehitsound = FALSE;\r
2683                 antilag_record(self, altime);\r
2684         }\r
2685 }\r
2686 \r
2687 \r
2688 /*\r
2689  * RedirectionThink:\r
2690  * returns TRUE if redirecting\r
2691  */\r
2692 float redirection_timeout;\r
2693 float redirection_nextthink;\r
2694 float RedirectionThink()\r
2695 {\r
2696         float clients_found;\r
2697 \r
2698         if(redirection_target == "")\r
2699                 return FALSE;\r
2700 \r
2701         if(!redirection_timeout)\r
2702         {\r
2703                 cvar_set("sv_public", "-2");\r
2704                 redirection_timeout = time + 0.6; // this will only try twice... should be able to keep more clients\r
2705                 if(redirection_target == "self")\r
2706                         bprint("^3SERVER NOTICE:^7 restarting the server\n");\r
2707                 else\r
2708                         bprint("^3SERVER NOTICE:^7 redirecting everyone to ", redirection_target, "\n");\r
2709         }\r
2710 \r
2711         if(time < redirection_nextthink)\r
2712                 return TRUE;\r
2713 \r
2714         redirection_nextthink = time + 1;\r
2715 \r
2716         clients_found = 0;\r
2717         FOR_EACH_REALCLIENT(self)\r
2718         {\r
2719                 print("Redirecting: sending connect command to ", self.netname, "\n");\r
2720                 if(redirection_target == "self")\r
2721                         stuffcmd(self, "\ndisconnect; reconnect\n");\r
2722                 else\r
2723                         stuffcmd(self, strcat("\ndisconnect; connect ", redirection_target, "\n"));\r
2724                 ++clients_found;\r
2725         }\r
2726 \r
2727         print("Redirecting: ", ftos(clients_found), " clients left.\n");\r
2728 \r
2729         if(time > redirection_timeout || clients_found == 0)\r
2730                 localcmd("\nwait; wait; wait; quit\n");\r
2731 \r
2732         return TRUE;\r
2733 }\r
2734 \r
2735 void TargetMusic_RestoreGame();\r
2736 void RestoreGame()\r
2737 {\r
2738         // Loaded from a save game\r
2739         // some things then break, so let's work around them...\r
2740 \r
2741         // Progs DB (capture records)\r
2742         ServerProgsDB = db_load("server.db");\r
2743 \r
2744         // Mapinfo\r
2745         MapInfo_Shutdown();\r
2746         MapInfo_Enumerate();\r
2747         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 1);\r
2748         WeaponStats_Init();\r
2749 \r
2750         TargetMusic_RestoreGame();\r
2751 }\r
2752 \r
2753 void SV_Shutdown()\r
2754 {\r
2755         if(gameover > 1) // shutting down already?\r
2756                 return;\r
2757 \r
2758         gameover = 2; // 2 = server shutting down\r
2759 \r
2760         if(world_initialized > 0)\r
2761         {\r
2762                 world_initialized = 0;\r
2763                 print("Saving persistent data...\n");\r
2764                 Ban_SaveBans();\r
2765                 if(!cheatcount_total)\r
2766                         db_save(ServerProgsDB, "server.db");\r
2767                 if(cvar("developer"))\r
2768                         db_save(TemporaryDB, "server-temp.db");\r
2769                 CheatShutdown(); // must be after cheatcount check\r
2770                 db_close(ServerProgsDB);\r
2771                 db_close(TemporaryDB);\r
2772                 print("done!\n");\r
2773                 // tell the bot system the game is ending now\r
2774                 bot_endgame();\r
2775 \r
2776                 WeaponStats_Shutdown();\r
2777                 MapInfo_Shutdown();\r
2778         }\r
2779         else if(world_initialized == 0)\r
2780         {\r
2781                 print("NOTE: crashed before even initializing the world, not saving persistent data\n");\r
2782         }\r
2783 }\r