]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/gamec/g_world.c
Added: a field ".winning" that's true if and only if the player entity has
[divverent/nexuiz.git] / data / qcsrc / server / gamec / g_world.c
1 string GetMapname();
2
3 void SetDefaultAlpha()
4 {
5         if(cvar("g_cloaked"))
6         {
7                 default_player_alpha = cvar("g_balance_cloaked_alpha");
8         }
9         else
10         {
11                 default_player_alpha = cvar("g_player_alpha");
12                 if(default_player_alpha <= 0)
13                         default_player_alpha = 1;
14         }
15 }
16
17 void worldspawn (void)
18 {
19
20         // Precache all player models
21         // Workaround for "invisible players"
22         precache_model("models/player/carni.zym");
23         precache_model("models/player/crash.zym");
24         precache_model("models/player/grunt.zym");
25         precache_model("models/player/headhunter.zym");
26         precache_model("models/player/insurrectionist.zym");
27         precache_model("models/player/jeandarc.zym");
28         precache_model("models/player/lurk.zym");
29         precache_model("models/player/lycanthrope.zym");
30         precache_model("models/player/marine.zym");
31         precache_model("models/player/nexus.zym");
32         precache_model("models/player/pyria.zym");
33         precache_model("models/player/shock.zym");
34         precache_model("models/player/skadi.zym");
35         precache_model("models/player/specop.zym");
36         precache_model("models/player/visitant.zym");
37
38         //precache_model ("progs/beam.mdl");
39         precache_model ("models/bullet.mdl");
40         precache_model ("models/casing_bronze.mdl");
41         precache_model ("models/casing_shell.mdl");
42         precache_model ("models/casing_steel.mdl");
43         precache_model ("models/ebomb.mdl");
44         precache_model ("models/elaser.mdl");
45         precache_model ("models/flash.md3");
46         precache_model ("models/gibs/bloodyskull.md3");
47         precache_model ("models/gibs/chunk.mdl");
48         precache_model ("models/gibs/eye.md3");
49         precache_model ("models/gibs/gib1.md3");
50         //precache_model ("models/gibs/gib2.md3");
51         //precache_model ("models/gibs/gib3.md3");
52         //precache_model ("models/gibs/gib4.md3");
53         precache_model ("models/gibs/gib5.md3");
54         //precache_model ("models/gibs/gib6.md3");
55         precache_model ("models/gibs/gib1.mdl");
56         precache_model ("models/gibs/gib2.mdl");
57         precache_model ("models/gibs/gib3.mdl");
58         precache_model ("models/grenademodel.md3");
59         precache_model ("models/hagarmissile.mdl");
60         precache_model ("models/items/a_bullets.mdl");
61         precache_model ("models/items/a_cells.md3");
62         precache_model ("models/items/a_rockets.md3");
63         precache_model ("models/items/a_shells.md3");
64         precache_model ("models/items/g_a1.md3");
65         precache_model ("models/items/g_a25.md3");
66         precache_model ("models/items/g_h1.md3");
67         precache_model ("models/items/g_h25.md3");
68         precache_model ("models/items/g_h100.md3");
69         precache_model ("models/items/g_invincible.md3");
70         precache_model ("models/items/g_strength.md3");
71         precache_model ("models/laser.mdl");
72         precache_model ("models/misc/chatbubble.spr");
73         precache_model ("models/misc/teambubble.spr");
74         precache_model ("models/nexflash.md3");
75         precache_model ("models/plasma.mdl");
76         precache_model ("models/plasmatrail.mdl");
77         precache_model ("models/rocket.md3");
78         precache_model ("models/sprites/grenexpl.spr");
79         precache_model ("models/sprites/hagar.spr");
80         precache_model ("models/sprites/muzzleflash.spr32");
81         precache_model ("models/sprites/electrocombo.spr32");
82         //precache_model ("models/sprites/plasmahitwall.spr32");
83         //precache_model ("models/sprites/plasmashot.spr32");
84         precache_model ("models/sprites/rockexpl.spr");
85         precache_model ("models/tracer.mdl");
86         precache_model ("models/uziflash.md3");
87         precache_model ("models/weapons/g_crylink.md3");
88         precache_model ("models/weapons/g_electro.md3");
89         precache_model ("models/weapons/g_gl.md3");
90         precache_model ("models/weapons/g_hagar.md3");
91         precache_model ("models/weapons/g_nex.md3");
92         precache_model ("models/weapons/g_rl.md3");
93         precache_model ("models/weapons/g_shotgun.md3");
94         precache_model ("models/weapons/g_uzi.md3");
95         precache_model ("models/weapons/v_crylink.md3");
96         precache_model ("models/weapons/v_electro.md3");
97         precache_model ("models/weapons/v_gl.md3");
98         precache_model ("models/weapons/v_hagar.md3");
99         precache_model ("models/weapons/v_laser.md3");
100         precache_model ("models/weapons/v_nex.md3");
101         precache_model ("models/weapons/v_rl.md3");
102         precache_model ("models/weapons/v_shotgun.md3");
103         precache_model ("models/weapons/v_uzi.md3");
104         precache_model ("models/weapons/w_crylink.zym");
105         precache_model ("models/weapons/w_electro.zym");
106         precache_model ("models/weapons/w_gl.zym");
107         precache_model ("models/weapons/w_hagar.zym");
108         precache_model ("models/weapons/w_laser.zym");
109         precache_model ("models/weapons/w_nex.zym");
110         precache_model ("models/weapons/w_rl.zym");
111         precache_model ("models/weapons/w_shotgun.zym");
112         precache_model ("models/weapons/w_uzi.zym");
113
114         // laser for laser-guided weapons
115         precache_model ("models/laser_dot.mdl");
116
117         precache_sound ("misc/null.wav");
118         precache_sound ("misc/armor1.wav");
119         precache_sound ("misc/armor25.wav");
120         precache_sound ("misc/armorimpact.wav");
121         precache_sound ("misc/bodyimpact1.wav");
122         precache_sound ("misc/bodyimpact2.wav");
123         precache_sound ("misc/gib.wav");
124         precache_sound ("misc/gib_splat01.wav");
125         precache_sound ("misc/gib_splat02.wav");
126         precache_sound ("misc/gib_splat03.wav");
127         precache_sound ("misc/gib_splat04.wav");
128         //precache_sound ("misc/h2ohit.wav");
129         precache_sound ("misc/hit.wav");
130         precache_sound ("misc/footstep01.wav");
131         precache_sound ("misc/footstep02.wav");
132         precache_sound ("misc/footstep03.wav");
133         precache_sound ("misc/footstep04.wav");
134         precache_sound ("misc/footstep05.wav");
135         precache_sound ("misc/footstep06.wav");
136         precache_sound ("misc/hitground1.ogg");
137         precache_sound ("misc/hitground2.ogg");
138         precache_sound ("misc/hitground3.ogg");
139         precache_sound ("misc/hitground4.ogg");
140         precache_sound ("misc/itempickup.ogg");
141         precache_sound ("misc/itemrespawn.ogg");
142         precache_sound ("misc/jumppad.ogg");
143         precache_sound ("misc/mediumhealth.ogg");
144         precache_sound ("misc/megahealth.ogg");
145         precache_sound ("misc/minihealth.ogg");
146         precache_sound ("misc/powerup.ogg");
147         precache_sound ("misc/powerup_shield.ogg");
148         precache_sound ("misc/talk.wav");
149         precache_sound ("misc/teleport.ogg");
150         precache_sound ("plats/medplat1.wav");
151         precache_sound ("plats/medplat2.wav");
152         precache_sound ("player/lava.wav");
153         precache_sound ("player/slime.wav");
154         precache_sound ("weapons/crylink_fire.ogg");
155         precache_sound ("weapons/electro_bounce.ogg");
156         precache_sound ("weapons/electro_fire.ogg");
157         precache_sound ("weapons/electro_fire2.ogg");
158         precache_sound ("weapons/electro_fly.wav");
159         precache_sound ("weapons/electro_impact.ogg");
160         precache_sound ("weapons/electro_impact_combo.ogg");
161         //precache_sound ("weapons/grenade_bounce.ogg");
162         precache_sound ("weapons/grenade_bounce1.ogg");
163         precache_sound ("weapons/grenade_bounce2.ogg");
164         precache_sound ("weapons/grenade_bounce3.ogg");
165         precache_sound ("weapons/grenade_fire.ogg");
166         precache_sound ("weapons/grenade_impact.ogg");
167         precache_sound ("weapons/hagar_fire.ogg");
168         precache_sound ("weapons/hagexp1.ogg");
169         precache_sound ("weapons/hagexp2.ogg");
170         precache_sound ("weapons/hagexp3.ogg");
171         precache_sound ("weapons/hook_fire.ogg");
172         precache_sound ("weapons/hook_impact.ogg");
173         precache_sound ("weapons/lasergun_fire.ogg");
174         precache_sound ("weapons/laserimpact.ogg");
175         precache_sound ("weapons/nexfire.ogg");
176         precache_sound ("weapons/neximpact.ogg");
177         precache_sound ("weapons/ric1.ogg");
178         precache_sound ("weapons/ric2.ogg");
179         precache_sound ("weapons/ric3.ogg");
180         precache_sound ("weapons/rocket_fire.ogg");
181         precache_sound ("weapons/rocket_fly.wav");
182         precache_sound ("weapons/rocket_impact.ogg");
183         precache_sound ("weapons/rocket_det.ogg");
184         precache_sound ("weapons/shotgun_fire.ogg");
185         precache_sound ("weapons/tink1.ogg");
186         precache_sound ("weapons/uzi_fire.ogg");
187         precache_sound ("weapons/weapon_switch.ogg");
188         precache_sound ("weapons/weaponpickup.ogg");
189         precache_sound ("weapons/strength_fire.ogg");
190
191         //precache_sound ("announce/male/kill10.ogg");
192         //precache_sound ("announce/male/kill15.ogg");
193         //precache_sound ("announce/male/kill20.ogg");
194         //precache_sound ("announce/male/kill25.ogg");
195         //precache_sound ("announce/male/kill3.ogg");
196         //precache_sound ("announce/male/kill30.ogg");
197         //precache_sound ("announce/male/kill4.ogg");
198         //precache_sound ("announce/male/kill5.ogg");
199         //precache_sound ("announce/male/kill6.ogg");
200         //precache_sound ("announce/male/mapkill1.ogg");
201         //precache_sound ("announce/robotic/last_second_save.ogg");
202         //precache_sound ("announce/robotic/narrowly_averted.ogg");
203         //precache_sound ("minstagib/mockery.ogg");
204
205         // announcer sounds - male
206         precache_sound ("announcer/male/03kills.ogg");
207         precache_sound ("announcer/male/05kills.ogg");
208         precache_sound ("announcer/male/10kills.ogg");
209         precache_sound ("announcer/male/15kills.ogg");
210         precache_sound ("announcer/male/20kills.ogg");
211         precache_sound ("announcer/male/25kills.ogg");
212         precache_sound ("announcer/male/30kills.ogg");
213         precache_sound ("announcer/male/botlike.ogg");
214         precache_sound ("announcer/male/electrobitch.ogg");
215         precache_sound ("announcer/male/welcome.ogg");
216         precache_sound ("announcer/male/yoda.ogg");
217
218         // announcer sounds - robotic
219         precache_sound ("announcer/robotic/1fragleft.ogg");
220         precache_sound ("announcer/robotic/1minuteremains.ogg");
221         precache_sound ("announcer/robotic/2fragsleft.ogg");
222         precache_sound ("announcer/robotic/3fragsleft.ogg");
223         precache_sound ("announcer/robotic/lastsecond.ogg");
224         precache_sound ("announcer/robotic/narrowly.ogg");
225
226         // plays music for the level if there is any
227         if (self.noise)
228         {
229                 precache_sound (self.noise);
230                 ambientsound ('0 0 0', self.noise, 1.00, ATTN_NONE);
231         }
232
233                 // 0 normal
234         lightstyle(0, "m");
235
236         // 1 FLICKER (first variety)
237         lightstyle(1, "mmnmmommommnonmmonqnmmo");
238
239         // 2 SLOW STRONG PULSE
240         lightstyle(2, "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba");
241
242         // 3 CANDLE (first variety)
243         lightstyle(3, "mmmmmaaaaammmmmaaaaaabcdefgabcdefg");
244
245         // 4 FAST STROBE
246         lightstyle(4, "mamamamamama");
247
248         // 5 GENTLE PULSE 1
249         lightstyle(5,"jklmnopqrstuvwxyzyxwvutsrqponmlkj");
250
251         // 6 FLICKER (second variety)
252         lightstyle(6, "nmonqnmomnmomomno");
253
254         // 7 CANDLE (second variety)
255         lightstyle(7, "mmmaaaabcdefgmmmmaaaammmaamm");
256
257         // 8 CANDLE (third variety)
258         lightstyle(8, "mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa");
259
260         // 9 SLOW STROBE (fourth variety)
261         lightstyle(9, "aaaaaaaazzzzzzzz");
262
263         // 10 FLUORESCENT FLICKER
264         lightstyle(10, "mmamammmmammamamaaamammma");
265
266         // 11 SLOW PULSE NOT FADE TO BLACK
267         lightstyle(11, "abcdefghijklmnopqrrqponmlkjihgfedcba");
268
269         // styles 32-62 are assigned by the light program for switchable lights
270
271         // 63 testing
272         lightstyle(63, "a");
273
274         player_count = 0;
275         lms_dead_count = 0;
276         lms_lowest_lives = 0;
277         lms_next_place = 0;
278
279         InitGameplayMode();
280         //if (cvar("g_domination"))
281         //      dom_init();
282
283         // Urrebot setting
284         urrebots_navopt = cvar("urrebots_navopt");
285
286         local entity head;
287         head = nextent(world);
288         maxclients = 0;
289         while(head)
290         {
291                 maxclients++;
292                 head = nextent(head);
293         }
294
295         GameLogInit(); // prepare everything
296         if(cvar("sv_eventlog"))
297         {
298                 local string s;
299                 GameLogEcho(":logversion:2", FALSE);
300                 s = strcat(cvar_string("sv_eventlog_files_counter"), ".");
301                 s = strcat(s, ftos(random()));
302                 GameLogEcho(strcat(":gamestart:", GetMapname(), ":", s), FALSE);
303                 s = ":gameinfo:mutators:LIST";
304                 if(cvar("g_grappling_hook"))
305                         s = strcat(s, ":grappling_hook");
306                 if(!cvar("g_use_ammunition"))
307                         s = strcat(s, ":no_use_ammunition");
308                 if(!cvar("g_pickup_items"))
309                         s = strcat(s, ":no_pickup_items");
310                 if(cvar("g_instagib"))
311                         s = strcat(s, ":instagib");
312                 if(cvar("g_rocketarena"))
313                         s = strcat(s, ":rockerarena");
314                 if(cvar("g_nixnex"))
315                         s = strcat(s, ":nixnex");
316                 if(cvar("g_vampire"))
317                         s = strcat(s, ":vampire");
318                 if(cvar("g_laserguided_missile"))
319                         s = strcat(s, ":laserguided_missile");
320                 if(cvar("g_norecoil"))
321                         s = strcat(s, ":norecoil");
322                 if(cvar("g_midair"))
323                         s = strcat(s, ":midair");
324                 if(cvar("g_minstagib"))
325                         s = strcat(s, ":minstagib");
326                 GameLogEcho(s, FALSE);
327                 GameLogEcho(":gameinfo:end", FALSE);
328         }
329
330         cvar_set("nextmap", "");
331
332         SetDefaultAlpha();
333
334         if(cvar("g_campaign"))
335                 CampaignInit();
336 }
337
338 void light (void)
339 {
340         makestatic (self);
341 }
342
343 float( string pFilename ) TryFile =
344 {
345         local float lHandle;
346         dprint("TryFile(\"", pFilename, "\")\n");
347         lHandle = fopen( pFilename, FILE_READ );
348         if( lHandle != -1 ) {
349                 fclose( lHandle );
350                 return TRUE;
351         } else {
352                 return FALSE;
353         }
354 };
355
356 string GetMapname()
357 {
358         if (game == GAME_DEATHMATCH)
359                 return strcat("dm_", mapname);
360         else if (game == GAME_TEAM_DEATHMATCH)
361                 return strcat("tdm_", mapname);
362         else if (game == GAME_DOMINATION)
363                 return strcat("dom_", mapname);
364         else if (game == GAME_CTF)
365                 return strcat("ctf_", mapname);
366         else if (game == GAME_RUNEMATCH)
367                 return strcat("rune_", mapname);
368         else if (game == GAME_LMS)
369                 return strcat("lms_", mapname);
370         return strcat("dm_", mapname);
371 }
372
373 float GetMaplistPosition()
374 {
375         float pos, f;
376         string map, s;
377
378         map = strzone(GetMapname());
379         pos = 0;
380         f = tokenize(cvar_string("g_maplist"));
381
382         while(pos < f)
383         {
384                 s = strzone(argv(pos));
385                 if(s == map)
386                 {
387                         strunzone(s);
388                         strunzone(map);
389                         return pos;
390                 }
391                 strunzone(s);
392                 pos++;
393         }
394
395         strunzone(map);
396         // resume normal maplist rotation if current map is not in g_maplist
397         return cvar("g_maplist_index");          
398 }
399
400 void() GotoNextMap =
401 {
402         //local string nextmap;
403         //local float n, nummaps;
404         //local string s;
405         string exit_cfg;
406         if (alreadychangedlevel)
407                 return;
408         alreadychangedlevel = TRUE;
409         if (cvar("samelevel")) // if samelevel is set, stay on same level
410         {
411                 // 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)
412                 //localcmd(strcat("exec \"maps/", mapname, ".mapcfg\"\n"));
413                 // so instead just restart the current map using the restart command (DOES NOT WORK PROPERLY WITH exit_cfg STUFF)
414                 localcmd("restart\n");
415                 //changelevel (mapname);
416                 return;
417         }
418
419         // if an exit cfg is defined by exiting map, exec it.
420         exit_cfg = cvar_string("exit_cfg");
421         if(exit_cfg != "")
422                 localcmd(strcat("exec \"", exit_cfg, "\"\n"));
423
424         ResetGameCvars();
425
426
427         if (cvar("lastlevel"))
428         {
429                 localcmd(strcat("set lastlevel 0\n"));
430                 localcmd(strcat("togglemenu\n"));
431         }
432         else
433         {
434                 // method 0
435                 local float lCurrent;
436                 local float lSize;
437                 local float lOldCurrent;
438                 local float lBeforeCurrent;
439                 local float pass;
440
441                 if(TryFile(strcat("maps/", cvar_string("nextmap"), ".mapcfg")))
442                 {
443                         localcmd(strcat("exec \"maps/", cvar_string("nextmap"), ".mapcfg\"\n"));
444                         return;
445                 }
446
447                 pass = 0;
448                 while (pass < 2)
449                 {
450                         pass = pass + 1;
451                         local string temp;
452                         temp = cvar_string( "g_maplist" );
453                         if(temp == "")
454                         {
455                                 bprint( "Maplist is empty!  Resetting it to default map list.\n" );
456                                 cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
457                                 temp = cvar_string( "g_maplist" );
458                         }
459                         temp = strzone(temp);
460                         dprint("g_maplist is ", temp, "\n");
461                         lCurrent = max(0, GetMaplistPosition());
462                         lSize = tokenize( temp );
463                         lOldCurrent = lCurrent;
464                         dprint(ftos(lOldCurrent), " / ", ftos(lSize), " (start)\n");
465
466                         // if we want a random map selection...
467                         if(cvar("g_maplist_selectrandom") && lSize > 1)
468                         {
469                                 lBeforeCurrent = lCurrent - 1;
470                                 if(lBeforeCurrent < 0)
471                                         lBeforeCurrent = lSize - 1;
472                                 lCurrent = ceil(random() * (lSize - 1)) - 1; // random in 0..lsize-2
473                                 if(lCurrent >= lBeforeCurrent)
474                                         lCurrent += 1;
475                                 // choose any map except for the current one
476                         }
477
478                         while( 1 ) {
479                                 local string lFilename;
480
481                                 lCurrent = lCurrent + 1;
482                                 dprint(ftos(lCurrent), " / ", ftos(lSize), "\n");
483                                 if( lCurrent >= lSize ) {
484                                         lCurrent = 0;
485                                 }
486                                 if( lOldCurrent == lCurrent ) {
487                                         // we couldn't find a valid map at all
488                                         if (pass == 1)
489                                         {
490                                                 bprint( "Maplist is bad/messed up. Not one good mapcfg can be found in it!  Resetting it to default map list.\n" );
491                                                 cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
492                                                 // let the loop restart with the default list now
493                                         }
494                                         else
495                                         {
496                                                 bprint( "Both g_maplist and g_maplist_defaultlist are messed up!  Complain to developers!\n" );
497                                                 localcmd( "disconnect\n" );
498                                         }
499                                         break;
500                                 }
501
502                                 cvar_set( "g_maplist_index", ftos( lCurrent ) );
503
504                                 lFilename = strcat( "maps/", argv( lCurrent ), ".mapcfg" );
505                                 if( TryFile( lFilename ) ) {
506                                         localcmd(strcat("exec \"", lFilename ,"\"\n"));
507                                         pass = 2; // exit the outer loop
508                                         break;
509                                 } else {
510                                         dprint( "Couldn't find '", lFilename, "'..\n" );
511                                 }
512                                 //changelevel( argv( lCurrent ) );
513                         }
514                         strunzone(temp);
515                 }
516
517                 /*
518                 // method 1
519
520                 //local entity pos;
521                 local float fh;
522                 local string line;
523
524                 // restart current map if no cycle is found
525                 nextmap = strzone(mapname);
526                 fh = fopen("maplist.cfg", FILE_READ);
527                 if (fh >= 0)
528                 {
529                         while (1)
530                         {
531                                 line = fgets(fh);
532                                 if (nextmap == mapname)
533                                 {
534                                         strunzone(nextmap);
535                                         nextmap = strzone(line);
536                                 }
537                                 if (!line)
538                                         break;
539                                 if (line == mapname)
540                                 {
541                                         line = fgets(fh);
542                                         if (!line)
543                                                 break;
544                                         strunzone(nextmap);
545                                         nextmap = strzone(line);
546                                         break;
547                                 }
548                         }
549                         fclose(fh);
550                 }
551                 changelevel (nextmap);
552                 strunzone(nextmap);*/
553
554                 // method 3
555                 /*
556                 s = cvar_string("g_maplist");
557                 nummaps = tokenize(s);
558                 // if no map list, restart current one
559                 nextmap = mapname;
560                 if (nummaps >= 1)
561                 {
562                         n = 0;
563                         while (n < nummaps)
564                         {
565                                 if (argv(n) == mapname)
566                                         break;
567                                 n = n + 1;
568                         }
569                         n = n + 1;
570                         if (n >= nummaps)
571                                 n = 0;
572                         nextmap = argv(n);
573                 }
574                 changelevel (nextmap);
575                 */
576         }
577 };
578
579
580 /*
581 ============
582 IntermissionThink
583
584 When the player presses attack or jump, change to the next level
585 ============
586 */
587 void() IntermissionThink =
588 {
589         if (time < intermission_exittime)
590                 return;
591
592         if (time < intermission_exittime + 10 && !self.button0 && !self.button1 && !self.button2 && !self.button3)
593                 return;
594
595         GotoNextMap ();
596 };
597
598 /*
599 ============
600 FindIntermission
601
602 Returns the entity to view from
603 ============
604 */
605 /*
606 entity() FindIntermission =
607 {
608         local   entity spot;
609         local   float cyc;
610
611 // look for info_intermission first
612         spot = find (world, classname, "info_intermission");
613         if (spot)
614         {       // pick a random one
615                 cyc = random() * 4;
616                 while (cyc > 1)
617                 {
618                         spot = find (spot, classname, "info_intermission");
619                         if (!spot)
620                                 spot = find (spot, classname, "info_intermission");
621                         cyc = cyc - 1;
622                 }
623                 return spot;
624         }
625
626 // then look for the start position
627         spot = find (world, classname, "info_player_start");
628         if (spot)
629                 return spot;
630
631 // testinfo_player_start is only found in regioned levels
632         spot = find (world, classname, "testplayerstart");
633         if (spot)
634                 return spot;
635
636 // then look for the start position
637         spot = find (world, classname, "info_player_deathmatch");
638         if (spot)
639                 return spot;
640
641         //objerror ("FindIntermission: no spot");
642         return world;
643 };
644 */
645
646 /*
647 ===============================================================================
648
649 RULES
650
651 ===============================================================================
652 */
653
654 void() DumpStats =
655 {
656         local float file;
657         local string s;
658
659         if(cvar("_printstats"))
660                 cvar_set("_printstats", "0");
661         else if(!gameover)
662                 return;
663
664         if(gameover)
665                 s = ":scores:";
666         else
667                 s = ":status:";
668
669         s = strcat(s, GetMapname(), ":", ftos(rint(time)));
670
671         if(cvar("sv_eventlog") && gameover)
672                 GameLogEcho(s, FALSE);
673         else if(cvar("sv_logscores_console"))
674                 ServerConsoleEcho(s, FALSE);
675         if(cvar("sv_logscores_file"))
676         {
677                 file = fopen(cvar_string("sv_logscores_filename"), FILE_APPEND);
678                 fputs(file, strcat(s, "\n"));
679         }
680
681         other = findchainflags(flags, FL_CLIENT);
682         while (other)
683         {
684                 if ((clienttype(other) == CLIENTTYPE_REAL) || (clienttype(other) == CLIENTTYPE_BOT && cvar("sv_logscores_bots")))
685                 {
686                         s = strcat(":player:", ftos(other.frags), ":");
687                         s = strcat(s, ftos(other.deaths), ":");
688                         s = strcat(s, ftos(rint(time - other.jointime)), ":");
689                         s = strcat(s, ftos(other.team), ":");
690
691                         if(cvar("sv_logscores_file"))
692                                 fputs(file, strcat(s, other.netname, "\n"));
693                         if(cvar("sv_eventlog") && gameover)
694                                 GameLogEcho(strcat(s, ftos(other.playerid), ":", other.netname), TRUE);
695                         else if(cvar("sv_logscores_console"))
696                                 ServerConsoleEcho(strcat(s, other.netname), TRUE);
697                 }
698                 other = other.chain;
699         }
700
701         if(cvar("sv_eventlog") && gameover)
702                 GameLogEcho(":end", FALSE);
703         else if(cvar("sv_logscores_console"))
704                 ServerConsoleEcho(":end", FALSE);
705         if(cvar("sv_logscores_file"))
706         {
707                 fputs(file, ":end\n");
708                 fclose(file);
709         }
710 }
711
712
713 /*
714 go to the next level for deathmatch
715 only called if a time or frag limit has expired
716 */
717 void() NextLevel =
718 {
719         gameover = TRUE;
720
721         intermission_running = 1;
722
723 // enforce a wait time before allowing changelevel
724         intermission_exittime = time + cvar("sv_mapchange_delay");
725
726         WriteByte (MSG_ALL, SVC_CDTRACK);
727         WriteByte (MSG_ALL, 3);
728         WriteByte (MSG_ALL, 3);
729
730         //pos = FindIntermission ();
731
732         VoteReset();
733
734         DumpStats();
735
736         if(cvar("sv_eventlog"))
737                 GameLogEcho(":gameover", FALSE);
738
739         GameLogClose();
740
741         other = findchainflags(flags, FL_CLIENT);
742         while (other != world)
743         {
744                 //other.nextthink = time + 0.5;
745                 other.takedamage = DAMAGE_NO;
746                 other.solid = SOLID_NOT;
747                 other.movetype = MOVETYPE_NONE;
748                 other.angles = other.v_angle;
749                 other.angles_x = other.angles_x * -1;
750
751                 self = other;
752                 weapon_action(other.weapon, WR_IDLE);
753
754                 if(clienttype(other) == CLIENTTYPE_REAL)
755                 if(cvar("sv_autoscreenshot"))
756                         stuffcmd(other, "screenshot\necho \"^5A screenshot has been taken at request of the server.\"\n");
757
758                 if(other.winning)
759                         bprint(strcat(other.netname, " wins.\n"));
760
761                 /*
762                 if (pos != world);
763                 {
764                         other.modelindex = 0;
765                         other.weaponentity = world; // remove weapon model
766                         other.view_ofs = '0 0 0';
767                         other.angles = other.v_angle = pos.mangle;
768                         if (!other.angles)
769                         {
770                                 other.angles = other.v_angle = pos.angles;
771                                 other.v_angle_x = other.v_angle_x * -1;
772                         }
773                         other.fixangle = TRUE;          // turn this way immediately
774                         setorigin (other, pos.origin);
775                 }
776                 */
777                 other = other.chain;
778         }
779
780         if(cvar("g_campaign"))
781                 CampaignFinish();
782
783         WriteByte (MSG_ALL, SVC_INTERMISSION);
784 };
785
786 /*
787 ============
788 CheckRules_Player
789
790 Exit deathmatch games upon conditions
791 ============
792 */
793 void() CheckRules_Player =
794 {
795         if (gameover)   // someone else quit the game already
796                 return;
797
798         // fixme: don't check players; instead check dom_team and ctf_team entities
799         //   (div0: and that in CheckRules_World please)
800 };
801
802 float checkrules_oneminutewarning;
803 float checkrules_leaderfrags;
804 float tdm_max_score, tdm_old_score;
805
806 float checkrules_equality;
807 float checkrules_overtimewarning;
808 float checkrules_overtimeend;
809
810 void() InitiateOvertime =
811 {
812         if(!checkrules_overtimeend)
813                 checkrules_overtimeend = time + 60 * cvar("timelimit_maxovertime");
814 }
815
816 float WINNING_NO = 0; // no winner, but time limits may terminate the game
817 float WINNING_YES = 1; // winner found
818 float WINNING_NEVER = 2; // no winner, enter overtime if time limit is reached
819 float WINNING_STARTOVERTIME = 3; // no winner, enter overtime NOW
820
821 float(float fraglimitreached, float equality) GetWinningCode =
822 {
823         if(equality)
824                 if(fraglimitreached)
825                         return WINNING_STARTOVERTIME;
826                 else
827                         return WINNING_NEVER;
828         else
829                 if(fraglimitreached)
830                         return WINNING_YES;
831                 else
832                         return WINNING_NO;
833 }
834
835 // set the .winning flag for exactly those players with a given field value
836 void(.float field, float value) SetWinners =
837 {
838         entity head;
839         head = findchain(classname, "player");
840         while (head)
841         {
842                 head.winning = (head.field == value);
843                 head = head.chain;
844         }
845 }
846
847 // set the .winning flag for those players with a given field value
848 void(.float field, float value) AddWinners =
849 {
850         entity head;
851         head = findchain(classname, "player");
852         while (head)
853         {
854                 if(head.field == value)
855                         head.winning = 1;
856                 head = head.chain;
857         }
858 }
859
860 // clear the .winning flags
861 void(void) ClearWinners =
862 {
863         entity head;
864         head = findchain(classname, "player");
865         while (head)
866         {
867                 head.winning = 0;
868                 head = head.chain;
869         }
870 }
871
872 // LMS winning condition: game terminates if and only if there's at most one
873 // one player who's living lives. Top two scores being equal cancels the time
874 // limit.
875 float() WinningCondition_LMS =
876 {
877         entity head;
878
879         if(lms_dead_count < 0)
880                 lms_dead_count = 0;
881
882         if(player_count > 1 && lms_dead_count >= player_count - 1)
883                 return WINNING_YES; // He's the last man standing!
884
885         if((player_count == 1 && lms_dead_count == 1))
886                 return WINNING_YES; // All dead... (n:n is handled by the test above)
887
888         // When we get here, we have at least two players who are actually LIVING,
889         // or one player who is still waiting for a victim to join the server. Now
890         // check if the top two players have equal score.
891
892         checkrules_leaderfrags = 0;
893         head = findchain(classname, "player");
894         checkrules_equality = FALSE;
895         while (head)
896         {
897                 if(head.frags > checkrules_leaderfrags)
898                         checkrules_leaderfrags = head.frags;
899                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
900                         checkrules_equality = TRUE;
901                 head = head.chain;
902         }
903
904         SetWinners(frags, checkrules_leaderfrags);
905
906         // The top two players have the same amount of lives? No timelimit then,
907         // enter overtime...
908
909         if(checkrules_equality)
910                 return WINNING_NEVER;
911
912         // Top two have different scores? Way to go for our beloved TIMELIMIT!
913         return WINNING_NO;
914 }
915
916 // DM winning condition: game terminates if a player reached the fraglimit,
917 // unless the first two players have the same score. The latter case also
918 // breaks the time limit.
919 float(float fraglimit) WinningCondition_MaxIndividualScore =
920 {
921         float checkrules_oldleaderfrags;
922         entity head;
923
924         checkrules_oldleaderfrags = checkrules_leaderfrags;
925         checkrules_leaderfrags = 0;
926         head = findchain(classname, "player");
927         checkrules_equality = FALSE;
928         while (head)
929         {
930                 if(head.frags > checkrules_leaderfrags)
931                         checkrules_leaderfrags = head.frags;
932                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
933                         checkrules_equality = TRUE;
934                 head = head.chain;
935         }
936
937         SetWinners(frags, checkrules_leaderfrags);
938
939         if (!cvar("g_runematch"))
940                 if (checkrules_leaderfrags != checkrules_oldleaderfrags)
941                 {
942                         if (checkrules_leaderfrags == fraglimit - 1)
943                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
944                         else if (checkrules_leaderfrags == fraglimit - 2)
945                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
946                         else if (checkrules_leaderfrags == fraglimit - 3)
947                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
948                 }
949
950         return GetWinningCode(fraglimit && checkrules_leaderfrags >= fraglimit, checkrules_equality);
951 }
952
953 float(float fraglimit) WinningConditionBase_Teamplay =
954 {
955         tdm_old_score = tdm_max_score;
956         tdm_max_score = max(team1_score, team2_score, team3_score, team4_score);
957
958         checkrules_equality =
959         (
960                 (tdm_max_score > 0)
961                 &&
962                 (
963                           (team1_score == tdm_max_score)
964                         + (team2_score == tdm_max_score)
965                         + (team3_score == tdm_max_score)
966                         + (team4_score == tdm_max_score)
967                         >= 2));
968
969         ClearWinners();
970         if(team1_score == tdm_max_score)
971                 AddWinners(team, COLOR_TEAM1);
972         if(team2_score == tdm_max_score)
973                 AddWinners(team, COLOR_TEAM2);
974         if(team3_score == tdm_max_score)
975                 AddWinners(team, COLOR_TEAM3);
976         if(team4_score == tdm_max_score)
977                 AddWinners(team, COLOR_TEAM4);
978
979         if(!cvar("g_runematch") && !cvar("g_domination"))
980                 if(tdm_max_score != tdm_old_score)
981                 {
982                         if(tdm_max_score == fraglimit - 1)
983                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
984                         else if(tdm_max_score == fraglimit - 2)
985                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
986                         else if(tdm_max_score == fraglimit - 3)
987                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
988                 }
989
990         return GetWinningCode(fraglimit && tdm_max_score >= fraglimit, checkrules_equality);
991 }
992
993 // TDM winning condition: game terminates if a team's score sum reached the
994 // fraglimit, unless the first two teams have the same total score. The latter
995 // case also breaks the time limit.
996 float(float fraglimit) WinningCondition_MaxTeamSum =
997 {
998         entity head;
999
1000         team1_score = team2_score = team3_score = team4_score = 0;
1001
1002         head = findchain(classname, "player");
1003         while (head)
1004         {
1005                 if(head.team == COLOR_TEAM1)
1006                         team1_score += head.frags;
1007                 else if(head.team == COLOR_TEAM2)
1008                         team2_score += head.frags;
1009                 else if(head.team == COLOR_TEAM3)
1010                         team3_score += head.frags;
1011                 else if(head.team == COLOR_TEAM4)
1012                         team4_score += head.frags;
1013                 head = head.chain;
1014         }
1015
1016         return WinningConditionBase_Teamplay(fraglimit);
1017 }
1018
1019 // DOM/CTF winning condition: game terminates if the max of a team's players'
1020 // score reached the fraglimit, unless the first two teams have the same
1021 // maximum score. The latter case also breaks the time limit.
1022 float(float fraglimit) WinningCondition_MaxTeamMax =
1023 {
1024         entity head;
1025
1026         team1_score = team2_score = team3_score = team4_score = 0;
1027
1028         head = findchain(classname, "player");
1029         while (head)
1030         {
1031                 if(head.team == COLOR_TEAM1)
1032                 {
1033                         if(head.frags > team1_score)
1034                                 team1_score = head.frags;
1035                 }
1036                 else if(head.team == COLOR_TEAM2)
1037                 {
1038                         if(head.frags > team2_score)
1039                                 team2_score = head.frags;
1040                 }
1041                 else if(head.team == COLOR_TEAM3)
1042                 {
1043                         if(head.frags > team3_score)
1044                                 team3_score = head.frags;
1045                 }
1046                 else if(head.team == COLOR_TEAM4)
1047                 {
1048                         if(head.frags > team4_score)
1049                                 team4_score = head.frags;
1050                 }
1051                 head = head.chain;
1052         }
1053
1054         return WinningConditionBase_Teamplay(fraglimit);
1055 }
1056
1057
1058 // Add/remove bots if needed
1059 void() CheckRules_Minplayers =
1060 {
1061         float f;
1062
1063         if(cvar("minplayers") >= maxclients)
1064                 cvar_set("minplayers", ftos(maxclients - 1));
1065
1066         f = cvar("minplayers") - (player_count - bot_number);
1067         if((player_count - bot_number) < 1)
1068                 f = 0;
1069
1070         if(cvar("bot_number") != f)
1071         {
1072                 if(cvar("minplayers") != f)
1073                         cvar_set("bot_number", ftos(f));
1074                 else
1075                         cvar_set("bot_number", "0");
1076         }
1077 }
1078
1079
1080 /*
1081 ============
1082 CheckRules_World
1083
1084 Exit deathmatch games upon conditions
1085 ============
1086 */
1087 void() CheckRules_World =
1088 {
1089         local float status;
1090         local float timelimit;
1091         local float fraglimit;
1092
1093         VoteThink();
1094
1095         SetDefaultAlpha();
1096
1097         if (intermission_running)
1098                 if (time >= intermission_exittime + 60)
1099                 {
1100                         GotoNextMap();
1101                         return;
1102                 }
1103
1104         if (gameover)   // someone else quit the game already
1105                 return;
1106
1107         DumpStats();
1108
1109         timelimit = cvar("timelimit") * 60;
1110         fraglimit = cvar("fraglimit");
1111
1112         if (timelimit && time >= timelimit)
1113                 InitiateOvertime();
1114
1115         if (checkrules_overtimeend && time >= checkrules_overtimeend)
1116         {
1117                 NextLevel();
1118                 return;
1119         }
1120
1121         if(!checkrules_overtimewarning && checkrules_overtimeend)
1122         {
1123                 checkrules_overtimewarning = TRUE;
1124                 //sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1125                 bcenterprint("^3Now playing ^1OVERTIME^3!\n\n^3Keep fragging until we have a ^1winner^3!");
1126         }
1127
1128         if (!checkrules_oneminutewarning && timelimit && time > timelimit - 60)
1129         {
1130                 checkrules_oneminutewarning = TRUE;
1131                 sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1132         }
1133
1134         if(cvar("minplayers"))
1135                 CheckRules_Minplayers();
1136
1137         status = WINNING_NO;
1138         if(cvar("g_lms"))
1139         {
1140                 status = WinningCondition_LMS();
1141         }
1142         else
1143         {
1144                 if(teams_matter)
1145                 {
1146                         if(cvar("g_tdm") || cvar("g_runematch"))
1147                                 status = WinningCondition_MaxTeamSum(fraglimit);
1148                         else if(cvar("g_ctf") || cvar("g_domination"))
1149                                 status = WinningCondition_MaxTeamMax(fraglimit);
1150                         else
1151                         {
1152                                 dprint("div0: How can this happen?\n");
1153                                 status = WinningCondition_MaxTeamMax(fraglimit);
1154                         }
1155                 }
1156                 else
1157                         status = WinningCondition_MaxIndividualScore(fraglimit);
1158         }
1159
1160         if(status == WINNING_STARTOVERTIME)
1161         {
1162                 status = WINNING_NEVER;
1163                 InitiateOvertime();
1164         }
1165
1166         if(checkrules_overtimeend)
1167                 if(status != WINNING_NEVER)
1168                         status = WINNING_YES;
1169
1170         if(status == WINNING_YES)
1171                 NextLevel();
1172 };