]> icculus.org git repositories - divverent/nexuiz.git/blob - qcsrc/server/gamec/g_world.c
Redone campaign mode for better integration with the menu code.
[divverent/nexuiz.git] / 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         if(cvar("g_campaign"))
280                 CampaignPreInit();
281
282         InitGameplayMode();
283         //if (cvar("g_domination"))
284         //      dom_init();
285
286         // Urrebot setting
287         urrebots_navopt = cvar("urrebots_navopt");
288
289         local entity head;
290         head = nextent(world);
291         maxclients = 0;
292         while(head)
293         {
294                 maxclients++;
295                 head = nextent(head);
296         }
297
298         GameLogInit(); // prepare everything
299         if(cvar("sv_eventlog"))
300         {
301                 local string s;
302                 GameLogEcho(":logversion:2", FALSE);
303                 s = strcat(cvar_string("sv_eventlog_files_counter"), ".");
304                 s = strcat(s, ftos(random()));
305                 GameLogEcho(strcat(":gamestart:", GetMapname(), ":", s), FALSE);
306                 s = ":gameinfo:mutators:LIST";
307                 if(cvar("g_grappling_hook"))
308                         s = strcat(s, ":grappling_hook");
309                 if(!cvar("g_use_ammunition"))
310                         s = strcat(s, ":no_use_ammunition");
311                 if(!cvar("g_pickup_items"))
312                         s = strcat(s, ":no_pickup_items");
313                 if(cvar("g_instagib"))
314                         s = strcat(s, ":instagib");
315                 if(cvar("g_rocketarena"))
316                         s = strcat(s, ":rockerarena");
317                 if(cvar("g_nixnex"))
318                         s = strcat(s, ":nixnex");
319                 if(cvar("g_vampire"))
320                         s = strcat(s, ":vampire");
321                 if(cvar("g_laserguided_missile"))
322                         s = strcat(s, ":laserguided_missile");
323                 if(cvar("g_norecoil"))
324                         s = strcat(s, ":norecoil");
325                 if(cvar("g_midair"))
326                         s = strcat(s, ":midair");
327                 if(cvar("g_minstagib"))
328                         s = strcat(s, ":minstagib");
329                 GameLogEcho(s, FALSE);
330                 GameLogEcho(":gameinfo:end", FALSE);
331         }
332
333         cvar_set("nextmap", "");
334
335         SetDefaultAlpha();
336
337         if(cvar("g_campaign"))
338                 CampaignPostInit();
339 }
340
341 void light (void)
342 {
343         makestatic (self);
344 }
345
346 float( string pFilename ) TryFile =
347 {
348         local float lHandle;
349         dprint("TryFile(\"", pFilename, "\")\n");
350         lHandle = fopen( pFilename, FILE_READ );
351         if( lHandle != -1 ) {
352                 fclose( lHandle );
353                 return TRUE;
354         } else {
355                 return FALSE;
356         }
357 };
358
359 string GetMapname()
360 {
361         if (game == GAME_DEATHMATCH)
362                 return strcat("dm_", mapname);
363         else if (game == GAME_TEAM_DEATHMATCH)
364                 return strcat("tdm_", mapname);
365         else if (game == GAME_DOMINATION)
366                 return strcat("dom_", mapname);
367         else if (game == GAME_CTF)
368                 return strcat("ctf_", mapname);
369         else if (game == GAME_RUNEMATCH)
370                 return strcat("rune_", mapname);
371         else if (game == GAME_LMS)
372                 return strcat("lms_", mapname);
373         return strcat("dm_", mapname);
374 }
375
376 float GetMaplistPosition()
377 {
378         float pos, f;
379         string map, s;
380
381         map = strzone(GetMapname());
382         pos = 0;
383         f = tokenize(cvar_string("g_maplist"));
384
385         while(pos < f)
386         {
387                 s = strzone(argv(pos));
388                 if(s == map)
389                 {
390                         strunzone(s);
391                         strunzone(map);
392                         return pos;
393                 }
394                 strunzone(s);
395                 pos++;
396         }
397
398         strunzone(map);
399         // resume normal maplist rotation if current map is not in g_maplist
400         return cvar("g_maplist_index");          
401 }
402
403 void() GotoNextMap =
404 {
405         //local string nextmap;
406         //local float n, nummaps;
407         //local string s;
408         string exit_cfg;
409         if (alreadychangedlevel)
410                 return;
411         alreadychangedlevel = TRUE;
412
413         if(cvar("g_campaign"))
414         {
415                 CampaignPostIntermission();
416                 return;
417         }
418         
419         if (cvar("samelevel")) // if samelevel is set, stay on same level
420         {
421                 // 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)
422                 //localcmd(strcat("exec \"maps/", mapname, ".mapcfg\"\n"));
423                 // so instead just restart the current map using the restart command (DOES NOT WORK PROPERLY WITH exit_cfg STUFF)
424                 localcmd("restart\n");
425                 //changelevel (mapname);
426                 return;
427         }
428
429         // if an exit cfg is defined by exiting map, exec it.
430         exit_cfg = cvar_string("exit_cfg");
431         if(exit_cfg != "")
432                 localcmd(strcat("exec \"", exit_cfg, "\"\n"));
433
434         ResetGameCvars();
435
436
437         if (cvar("lastlevel"))
438         {
439                 localcmd(strcat("set lastlevel 0\n"));
440                 localcmd(strcat("togglemenu\n"));
441         }
442         else
443         {
444                 // method 0
445                 local float lCurrent;
446                 local float lSize;
447                 local float lOldCurrent;
448                 local float lBeforeCurrent;
449                 local float pass;
450
451                 if(TryFile(strcat("maps/", cvar_string("nextmap"), ".mapcfg")))
452                 {
453                         localcmd(strcat("exec \"maps/", cvar_string("nextmap"), ".mapcfg\"\n"));
454                         return;
455                 }
456
457                 pass = 0;
458                 while (pass < 2)
459                 {
460                         pass = pass + 1;
461                         local string temp;
462                         temp = cvar_string( "g_maplist" );
463                         if(temp == "")
464                         {
465                                 bprint( "Maplist is empty!  Resetting it to default map list.\n" );
466                                 cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
467                                 temp = cvar_string( "g_maplist" );
468                         }
469                         temp = strzone(temp);
470                         dprint("g_maplist is ", temp, "\n");
471                         lCurrent = max(0, GetMaplistPosition());
472                         lSize = tokenize( temp );
473                         lOldCurrent = lCurrent;
474                         dprint(ftos(lOldCurrent), " / ", ftos(lSize), " (start)\n");
475
476                         // if we want a random map selection...
477                         if(cvar("g_maplist_selectrandom") && lSize > 1)
478                         {
479                                 lBeforeCurrent = lCurrent - 1;
480                                 if(lBeforeCurrent < 0)
481                                         lBeforeCurrent = lSize - 1;
482                                 lCurrent = ceil(random() * (lSize - 1)) - 1; // random in 0..lsize-2
483                                 if(lCurrent >= lBeforeCurrent)
484                                         lCurrent += 1;
485                                 // choose any map except for the current one
486                         }
487
488                         while( 1 ) {
489                                 local string lFilename;
490
491                                 lCurrent = lCurrent + 1;
492                                 dprint(ftos(lCurrent), " / ", ftos(lSize), "\n");
493                                 if( lCurrent >= lSize ) {
494                                         lCurrent = 0;
495                                 }
496                                 if( lOldCurrent == lCurrent ) {
497                                         // we couldn't find a valid map at all
498                                         if (pass == 1)
499                                         {
500                                                 bprint( "Maplist is bad/messed up. Not one good mapcfg can be found in it!  Resetting it to default map list.\n" );
501                                                 cvar_set("g_maplist", cvar_string("g_maplist_defaultlist"));
502                                                 // let the loop restart with the default list now
503                                         }
504                                         else
505                                         {
506                                                 bprint( "Both g_maplist and g_maplist_defaultlist are messed up!  Complain to developers!\n" );
507                                                 localcmd( "disconnect\n" );
508                                         }
509                                         break;
510                                 }
511
512                                 cvar_set( "g_maplist_index", ftos( lCurrent ) );
513
514                                 lFilename = strcat( "maps/", argv( lCurrent ), ".mapcfg" );
515                                 if( TryFile( lFilename ) ) {
516                                         localcmd(strcat("exec \"", lFilename ,"\"\n"));
517                                         pass = 2; // exit the outer loop
518                                         break;
519                                 } else {
520                                         dprint( "Couldn't find '", lFilename, "'..\n" );
521                                 }
522                                 //changelevel( argv( lCurrent ) );
523                         }
524                         strunzone(temp);
525                 }
526
527                 /*
528                 // method 1
529
530                 //local entity pos;
531                 local float fh;
532                 local string line;
533
534                 // restart current map if no cycle is found
535                 nextmap = strzone(mapname);
536                 fh = fopen("maplist.cfg", FILE_READ);
537                 if (fh >= 0)
538                 {
539                         while (1)
540                         {
541                                 line = fgets(fh);
542                                 if (nextmap == mapname)
543                                 {
544                                         strunzone(nextmap);
545                                         nextmap = strzone(line);
546                                 }
547                                 if (!line)
548                                         break;
549                                 if (line == mapname)
550                                 {
551                                         line = fgets(fh);
552                                         if (!line)
553                                                 break;
554                                         strunzone(nextmap);
555                                         nextmap = strzone(line);
556                                         break;
557                                 }
558                         }
559                         fclose(fh);
560                 }
561                 changelevel (nextmap);
562                 strunzone(nextmap);*/
563
564                 // method 3
565                 /*
566                 s = cvar_string("g_maplist");
567                 nummaps = tokenize(s);
568                 // if no map list, restart current one
569                 nextmap = mapname;
570                 if (nummaps >= 1)
571                 {
572                         n = 0;
573                         while (n < nummaps)
574                         {
575                                 if (argv(n) == mapname)
576                                         break;
577                                 n = n + 1;
578                         }
579                         n = n + 1;
580                         if (n >= nummaps)
581                                 n = 0;
582                         nextmap = argv(n);
583                 }
584                 changelevel (nextmap);
585                 */
586         }
587 };
588
589
590 /*
591 ============
592 IntermissionThink
593
594 When the player presses attack or jump, change to the next level
595 ============
596 */
597 void() IntermissionThink =
598 {
599         if (time < intermission_exittime)
600                 return;
601
602         if (time < intermission_exittime + 10 && !self.button0 && !self.button1 && !self.button2 && !self.button3)
603                 return;
604
605         GotoNextMap ();
606 };
607
608 /*
609 ============
610 FindIntermission
611
612 Returns the entity to view from
613 ============
614 */
615 /*
616 entity() FindIntermission =
617 {
618         local   entity spot;
619         local   float cyc;
620
621 // look for info_intermission first
622         spot = find (world, classname, "info_intermission");
623         if (spot)
624         {       // pick a random one
625                 cyc = random() * 4;
626                 while (cyc > 1)
627                 {
628                         spot = find (spot, classname, "info_intermission");
629                         if (!spot)
630                                 spot = find (spot, classname, "info_intermission");
631                         cyc = cyc - 1;
632                 }
633                 return spot;
634         }
635
636 // then look for the start position
637         spot = find (world, classname, "info_player_start");
638         if (spot)
639                 return spot;
640
641 // testinfo_player_start is only found in regioned levels
642         spot = find (world, classname, "testplayerstart");
643         if (spot)
644                 return spot;
645
646 // then look for the start position
647         spot = find (world, classname, "info_player_deathmatch");
648         if (spot)
649                 return spot;
650
651         //objerror ("FindIntermission: no spot");
652         return world;
653 };
654 */
655
656 /*
657 ===============================================================================
658
659 RULES
660
661 ===============================================================================
662 */
663
664 void() DumpStats =
665 {
666         local float file;
667         local string s;
668
669         if(cvar("_printstats"))
670                 cvar_set("_printstats", "0");
671         else if(!gameover)
672                 return;
673
674         if(gameover)
675                 s = ":scores:";
676         else
677                 s = ":status:";
678
679         s = strcat(s, GetMapname(), ":", ftos(rint(time)));
680
681         if(cvar("sv_eventlog") && gameover)
682                 GameLogEcho(s, FALSE);
683         else if(cvar("sv_logscores_console"))
684                 ServerConsoleEcho(s, FALSE);
685         if(cvar("sv_logscores_file"))
686         {
687                 file = fopen(cvar_string("sv_logscores_filename"), FILE_APPEND);
688                 fputs(file, strcat(s, "\n"));
689         }
690
691         other = findchainflags(flags, FL_CLIENT);
692         while (other)
693         {
694                 if ((clienttype(other) == CLIENTTYPE_REAL) || (clienttype(other) == CLIENTTYPE_BOT && cvar("sv_logscores_bots")))
695                 {
696                         s = strcat(":player:", ftos(other.frags), ":");
697                         s = strcat(s, ftos(other.deaths), ":");
698                         s = strcat(s, ftos(rint(time - other.jointime)), ":");
699                         s = strcat(s, ftos(other.team), ":");
700
701                         if(cvar("sv_logscores_file"))
702                                 fputs(file, strcat(s, other.netname, "\n"));
703                         if(cvar("sv_eventlog") && gameover)
704                                 GameLogEcho(strcat(s, ftos(other.playerid), ":", other.netname), TRUE);
705                         else if(cvar("sv_logscores_console"))
706                                 ServerConsoleEcho(strcat(s, other.netname), TRUE);
707                 }
708                 other = other.chain;
709         }
710
711         if(cvar("sv_eventlog") && gameover)
712                 GameLogEcho(":end", FALSE);
713         else if(cvar("sv_logscores_console"))
714                 ServerConsoleEcho(":end", FALSE);
715         if(cvar("sv_logscores_file"))
716         {
717                 fputs(file, ":end\n");
718                 fclose(file);
719         }
720 }
721
722
723 /*
724 go to the next level for deathmatch
725 only called if a time or frag limit has expired
726 */
727 void() NextLevel =
728 {
729         gameover = TRUE;
730
731         intermission_running = 1;
732
733 // enforce a wait time before allowing changelevel
734         intermission_exittime = time + cvar("sv_mapchange_delay");
735
736         WriteByte (MSG_ALL, SVC_CDTRACK);
737         WriteByte (MSG_ALL, 3);
738         WriteByte (MSG_ALL, 3);
739
740         //pos = FindIntermission ();
741
742         VoteReset();
743
744         DumpStats();
745
746         if(cvar("sv_eventlog"))
747                 GameLogEcho(":gameover", FALSE);
748
749         GameLogClose();
750
751         other = findchainflags(flags, FL_CLIENT);
752         while (other != world)
753         {
754                 //other.nextthink = time + 0.5;
755                 other.takedamage = DAMAGE_NO;
756                 other.solid = SOLID_NOT;
757                 other.movetype = MOVETYPE_NONE;
758                 other.angles = other.v_angle;
759                 other.angles_x = other.angles_x * -1;
760
761                 self = other;
762                 weapon_action(other.weapon, WR_IDLE);
763
764                 if(clienttype(other) == CLIENTTYPE_REAL)
765                 if(cvar("sv_autoscreenshot"))
766                         stuffcmd(other, "screenshot\necho \"^5A screenshot has been taken at request of the server.\"\n");
767
768                 if(other.winning)
769                         bprint(strcat(other.netname, " wins.\n"));
770
771                 /*
772                 if (pos != world);
773                 {
774                         other.modelindex = 0;
775                         other.weaponentity = world; // remove weapon model
776                         other.view_ofs = '0 0 0';
777                         other.angles = other.v_angle = pos.mangle;
778                         if (!other.angles)
779                         {
780                                 other.angles = other.v_angle = pos.angles;
781                                 other.v_angle_x = other.v_angle_x * -1;
782                         }
783                         other.fixangle = TRUE;          // turn this way immediately
784                         setorigin (other, pos.origin);
785                 }
786                 */
787                 other = other.chain;
788         }
789
790         if(cvar("g_campaign"))
791                 CampaignPreIntermission();
792
793         WriteByte (MSG_ALL, SVC_INTERMISSION);
794 };
795
796 /*
797 ============
798 CheckRules_Player
799
800 Exit deathmatch games upon conditions
801 ============
802 */
803 void() CheckRules_Player =
804 {
805         if (gameover)   // someone else quit the game already
806                 return;
807
808         // fixme: don't check players; instead check dom_team and ctf_team entities
809         //   (div0: and that in CheckRules_World please)
810 };
811
812 float checkrules_oneminutewarning;
813 float checkrules_leaderfrags;
814 float tdm_max_score, tdm_old_score;
815
816 float checkrules_equality;
817 float checkrules_overtimewarning;
818 float checkrules_overtimeend;
819
820 void() InitiateOvertime =
821 {
822         if(!checkrules_overtimeend)
823                 checkrules_overtimeend = time + 60 * cvar("timelimit_maxovertime");
824 }
825
826 float WINNING_NO = 0; // no winner, but time limits may terminate the game
827 float WINNING_YES = 1; // winner found
828 float WINNING_NEVER = 2; // no winner, enter overtime if time limit is reached
829 float WINNING_STARTOVERTIME = 3; // no winner, enter overtime NOW
830
831 float(float fraglimitreached, float equality) GetWinningCode =
832 {
833         if(equality)
834                 if(fraglimitreached)
835                         return WINNING_STARTOVERTIME;
836                 else
837                         return WINNING_NEVER;
838         else
839                 if(fraglimitreached)
840                         return WINNING_YES;
841                 else
842                         return WINNING_NO;
843 }
844
845 // set the .winning flag for exactly those players with a given field value
846 void(.float field, float value) SetWinners =
847 {
848         entity head;
849         head = findchain(classname, "player");
850         while (head)
851         {
852                 head.winning = (head.field == value);
853                 head = head.chain;
854         }
855 }
856
857 // set the .winning flag for those players with a given field value
858 void(.float field, float value) AddWinners =
859 {
860         entity head;
861         head = findchain(classname, "player");
862         while (head)
863         {
864                 if(head.field == value)
865                         head.winning = 1;
866                 head = head.chain;
867         }
868 }
869
870 // clear the .winning flags
871 void(void) ClearWinners =
872 {
873         entity head;
874         head = findchain(classname, "player");
875         while (head)
876         {
877                 head.winning = 0;
878                 head = head.chain;
879         }
880 }
881
882 // LMS winning condition: game terminates if and only if there's at most one
883 // one player who's living lives. Top two scores being equal cancels the time
884 // limit.
885 float() WinningCondition_LMS =
886 {
887         entity head;
888
889         if(lms_dead_count < 0)
890                 lms_dead_count = 0;
891
892         if(player_count > 1 && lms_dead_count >= player_count - 1)
893                 return WINNING_YES; // He's the last man standing!
894
895         if((player_count == 1 && lms_dead_count == 1))
896                 return WINNING_YES; // All dead... (n:n is handled by the test above)
897
898         // When we get here, we have at least two players who are actually LIVING,
899         // or one player who is still waiting for a victim to join the server. Now
900         // check if the top two players have equal score.
901
902         checkrules_leaderfrags = 0;
903         head = findchain(classname, "player");
904         checkrules_equality = FALSE;
905         while (head)
906         {
907                 if(head.frags > checkrules_leaderfrags)
908                         checkrules_leaderfrags = head.frags;
909                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
910                         checkrules_equality = TRUE;
911                 head = head.chain;
912         }
913
914         SetWinners(frags, checkrules_leaderfrags);
915
916         // The top two players have the same amount of lives? No timelimit then,
917         // enter overtime...
918
919         if(checkrules_equality)
920                 return WINNING_NEVER;
921
922         // Top two have different scores? Way to go for our beloved TIMELIMIT!
923         return WINNING_NO;
924 }
925
926 // DM winning condition: game terminates if a player reached the fraglimit,
927 // unless the first two players have the same score. The latter case also
928 // breaks the time limit.
929 float(float fraglimit) WinningCondition_MaxIndividualScore =
930 {
931         float checkrules_oldleaderfrags;
932         entity head;
933
934         checkrules_oldleaderfrags = checkrules_leaderfrags;
935         checkrules_leaderfrags = 0;
936         head = findchain(classname, "player");
937         checkrules_equality = FALSE;
938         while (head)
939         {
940                 if(head.frags > checkrules_leaderfrags)
941                         checkrules_leaderfrags = head.frags;
942                 else if(head.frags > 0 && head.frags == checkrules_leaderfrags)
943                         checkrules_equality = TRUE;
944                 head = head.chain;
945         }
946
947         SetWinners(frags, checkrules_leaderfrags);
948
949         if (!cvar("g_runematch"))
950                 if (checkrules_leaderfrags != checkrules_oldleaderfrags)
951                 {
952                         if (checkrules_leaderfrags == fraglimit - 1)
953                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
954                         else if (checkrules_leaderfrags == fraglimit - 2)
955                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
956                         else if (checkrules_leaderfrags == fraglimit - 3)
957                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
958                 }
959
960         return GetWinningCode(fraglimit && checkrules_leaderfrags >= fraglimit, checkrules_equality);
961 }
962
963 float(float fraglimit) WinningConditionBase_Teamplay =
964 {
965         tdm_old_score = tdm_max_score;
966         tdm_max_score = max(team1_score, team2_score, team3_score, team4_score);
967
968         checkrules_equality =
969         (
970                 (tdm_max_score > 0)
971                 &&
972                 (
973                           (team1_score == tdm_max_score)
974                         + (team2_score == tdm_max_score)
975                         + (team3_score == tdm_max_score)
976                         + (team4_score == tdm_max_score)
977                         >= 2));
978
979         ClearWinners();
980         if(team1_score == tdm_max_score)
981                 AddWinners(team, COLOR_TEAM1);
982         if(team2_score == tdm_max_score)
983                 AddWinners(team, COLOR_TEAM2);
984         if(team3_score == tdm_max_score)
985                 AddWinners(team, COLOR_TEAM3);
986         if(team4_score == tdm_max_score)
987                 AddWinners(team, COLOR_TEAM4);
988
989         if(!cvar("g_runematch") && !cvar("g_domination"))
990                 if(tdm_max_score != tdm_old_score)
991                 {
992                         if(tdm_max_score == fraglimit - 1)
993                                 sound(world, CHAN_AUTO, "announcer/robotic/1fragleft.ogg", 1, ATTN_NONE);
994                         else if(tdm_max_score == fraglimit - 2)
995                                 sound(world, CHAN_AUTO, "announcer/robotic/2fragsleft.ogg", 1, ATTN_NONE);
996                         else if(tdm_max_score == fraglimit - 3)
997                                 sound(world, CHAN_AUTO, "announcer/robotic/3fragsleft.ogg", 1, ATTN_NONE);
998                 }
999
1000         return GetWinningCode(fraglimit && tdm_max_score >= fraglimit, checkrules_equality);
1001 }
1002
1003 // TDM winning condition: game terminates if a team's score sum reached the
1004 // fraglimit, unless the first two teams have the same total score. The latter
1005 // case also breaks the time limit.
1006 float(float fraglimit) WinningCondition_MaxTeamSum =
1007 {
1008         entity head;
1009
1010         team1_score = team2_score = team3_score = team4_score = 0;
1011
1012         head = findchain(classname, "player");
1013         while (head)
1014         {
1015                 if(head.team == COLOR_TEAM1)
1016                         team1_score += head.frags;
1017                 else if(head.team == COLOR_TEAM2)
1018                         team2_score += head.frags;
1019                 else if(head.team == COLOR_TEAM3)
1020                         team3_score += head.frags;
1021                 else if(head.team == COLOR_TEAM4)
1022                         team4_score += head.frags;
1023                 head = head.chain;
1024         }
1025
1026         return WinningConditionBase_Teamplay(fraglimit);
1027 }
1028
1029 // DOM/CTF winning condition: game terminates if the max of a team's players'
1030 // score reached the fraglimit, unless the first two teams have the same
1031 // maximum score. The latter case also breaks the time limit.
1032 float(float fraglimit) WinningCondition_MaxTeamMax =
1033 {
1034         entity head;
1035
1036         team1_score = team2_score = team3_score = team4_score = 0;
1037
1038         head = findchain(classname, "player");
1039         while (head)
1040         {
1041                 if(head.team == COLOR_TEAM1)
1042                 {
1043                         if(head.frags > team1_score)
1044                                 team1_score = head.frags;
1045                 }
1046                 else if(head.team == COLOR_TEAM2)
1047                 {
1048                         if(head.frags > team2_score)
1049                                 team2_score = head.frags;
1050                 }
1051                 else if(head.team == COLOR_TEAM3)
1052                 {
1053                         if(head.frags > team3_score)
1054                                 team3_score = head.frags;
1055                 }
1056                 else if(head.team == COLOR_TEAM4)
1057                 {
1058                         if(head.frags > team4_score)
1059                                 team4_score = head.frags;
1060                 }
1061                 head = head.chain;
1062         }
1063
1064         return WinningConditionBase_Teamplay(fraglimit);
1065 }
1066
1067
1068 // Add/remove bots if needed
1069 void() CheckRules_Minplayers =
1070 {
1071         float f;
1072
1073         if(cvar("minplayers") >= maxclients)
1074                 cvar_set("minplayers", ftos(maxclients - 1));
1075
1076         f = cvar("minplayers") - (player_count - bot_number);
1077         if((player_count - bot_number) < 1)
1078                 f = 0;
1079
1080         if(cvar("bot_number") != f)
1081         {
1082                 if(cvar("minplayers") != f)
1083                         cvar_set("bot_number", ftos(f));
1084                 else
1085                         cvar_set("bot_number", "0");
1086         }
1087 }
1088
1089
1090 /*
1091 ============
1092 CheckRules_World
1093
1094 Exit deathmatch games upon conditions
1095 ============
1096 */
1097 void() CheckRules_World =
1098 {
1099         local float status;
1100         local float timelimit;
1101         local float fraglimit;
1102
1103         VoteThink();
1104
1105         SetDefaultAlpha();
1106
1107         if (intermission_running)
1108                 if (time >= intermission_exittime + 60)
1109                 {
1110                         GotoNextMap();
1111                         return;
1112                 }
1113
1114         if (gameover)   // someone else quit the game already
1115                 return;
1116
1117         DumpStats();
1118
1119         timelimit = cvar("timelimit") * 60;
1120         fraglimit = cvar("fraglimit");
1121
1122         if (timelimit && time >= timelimit)
1123                 InitiateOvertime();
1124
1125         if (checkrules_overtimeend && time >= checkrules_overtimeend)
1126         {
1127                 NextLevel();
1128                 return;
1129         }
1130
1131         if(!checkrules_overtimewarning && checkrules_overtimeend)
1132         {
1133                 checkrules_overtimewarning = TRUE;
1134                 //sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1135                 bcenterprint("^3Now playing ^1OVERTIME^3!\n\n^3Keep fragging until we have a ^1winner^3!");
1136         }
1137
1138         if (!checkrules_oneminutewarning && timelimit && time > timelimit - 60)
1139         {
1140                 checkrules_oneminutewarning = TRUE;
1141                 sound(world, CHAN_AUTO, "announcer/robotic/1minuteremains.ogg", 1, ATTN_NONE);
1142         }
1143
1144         if(cvar("minplayers"))
1145                 CheckRules_Minplayers();
1146
1147         status = WINNING_NO;
1148         if(cvar("g_lms"))
1149         {
1150                 status = WinningCondition_LMS();
1151         }
1152         else
1153         {
1154                 if(teams_matter)
1155                 {
1156                         if(cvar("g_tdm") || cvar("g_runematch"))
1157                                 status = WinningCondition_MaxTeamSum(fraglimit);
1158                         else if(cvar("g_ctf") || cvar("g_domination"))
1159                                 status = WinningCondition_MaxTeamMax(fraglimit);
1160                         else
1161                         {
1162                                 dprint("div0: How can this happen?\n");
1163                                 status = WinningCondition_MaxTeamMax(fraglimit);
1164                         }
1165                 }
1166                 else
1167                         status = WinningCondition_MaxIndividualScore(fraglimit);
1168         }
1169
1170         if(status == WINNING_STARTOVERTIME)
1171         {
1172                 status = WINNING_NEVER;
1173                 InitiateOvertime();
1174         }
1175
1176         if(checkrules_overtimeend)
1177                 if(status != WINNING_NEVER)
1178                         status = WINNING_YES;
1179
1180         if(status == WINNING_YES)
1181                 NextLevel();
1182 };