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