aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/game/GameBase.java
blob: 871e084a423dd4730ef73b56a3bc72dbbe250fdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

// Created on 30.11.2003 by RST.
// $Id: GameBase.java,v 1.3 2004-07-09 06:50:49 hzi Exp $

/** Father of all Objects. */

package jake2.game;

import java.util.StringTokenizer;

import jake2.*;
import jake2.client.*;
import jake2.qcommon.Com;
import jake2.server.*;
import jake2.util.*;

public class GameBase extends Globals
{
	public static cplane_t dummyplane = new cplane_t(); 
	public static game_locals_t game = new game_locals_t();
	public static level_locals_t level = new level_locals_t();
	public static game_import_t gi = new game_import_t();
	public static game_export_t globals = new game_export_t();
	public static spawn_temp_t st = new spawn_temp_t();

	public static int sm_meat_index;
	public static int snd_fry;
	public static int meansOfDeath;

	public static edict_t g_edicts[] = new edict_t[MAX_EDICTS];
	static {
		for (int n = 0; n < MAX_EDICTS; n++)
			g_edicts[n] = new edict_t(n);
	}

	public static cvar_t deathmatch = new cvar_t();
	public static cvar_t coop = new cvar_t();
	public static cvar_t dmflags = new cvar_t();
	public static cvar_t skill; // = new cvar_t();
	public static cvar_t fraglimit = new cvar_t();
	public static cvar_t timelimit = new cvar_t();
	public static cvar_t password = new cvar_t();
	public static cvar_t spectator_password = new cvar_t();
	public static cvar_t needpass = new cvar_t();
	public static cvar_t maxclients = new cvar_t();
	public static cvar_t maxspectators = new cvar_t();
	public static cvar_t maxentities = new cvar_t();
	public static cvar_t g_select_empty = new cvar_t();
	public static cvar_t dedicated = new cvar_t();

	public static cvar_t filterban = new cvar_t();

	public static cvar_t sv_maxvelocity = new cvar_t();
	public static cvar_t sv_gravity = new cvar_t();

	public static cvar_t sv_rollspeed = new cvar_t();
	public static cvar_t sv_rollangle = new cvar_t();
	public static cvar_t gun_x = new cvar_t();
	public static cvar_t gun_y = new cvar_t();
	public static cvar_t gun_z = new cvar_t();

	public static cvar_t run_pitch = new cvar_t();
	public static cvar_t run_roll = new cvar_t();
	public static cvar_t bob_up = new cvar_t();
	public static cvar_t bob_pitch = new cvar_t();
	public static cvar_t bob_roll = new cvar_t();

	public static cvar_t sv_cheats = new cvar_t();

	public static cvar_t flood_msgs = new cvar_t();
	public static cvar_t flood_persecond = new cvar_t();
	public static cvar_t flood_waitdelay = new cvar_t();

	public static cvar_t sv_maplist = new cvar_t();

	public final static float STOP_EPSILON = 0.1f;

	static field_t fields_ent[] =
		new field_t[] {
			new field_t("classname", F_LSTRING),
			new field_t("model", F_LSTRING),
			new field_t("spawnflags", F_INT),
			new field_t("speed", F_FLOAT),
			new field_t("accel", F_FLOAT),
			new field_t("decel", F_FLOAT),
			new field_t("target", F_LSTRING),
			new field_t("targetname", F_LSTRING),
			new field_t("pathtarget", F_LSTRING),
			new field_t("deathtarget", F_LSTRING),
			new field_t("killtarget", F_LSTRING),
			new field_t("combattarget", F_LSTRING),
			new field_t("message", F_LSTRING),
			new field_t("team", F_LSTRING),
			new field_t("wait", F_FLOAT),
			new field_t("delay", F_FLOAT),
			new field_t("random", F_FLOAT),
			new field_t("move_origin", F_VECTOR),
			new field_t("move_angles", F_VECTOR),
			new field_t("style", F_INT),
			new field_t("count", F_INT),
			new field_t("health", F_INT),
			new field_t("sounds", F_INT),
			new field_t("light", F_IGNORE),
			new field_t("dmg", F_INT),
			new field_t("mass", F_INT),
			new field_t("volume", F_FLOAT),
			new field_t("attenuation", F_FLOAT),
			new field_t("map", F_LSTRING),
			new field_t("origin", F_VECTOR),
			new field_t("angles", F_VECTOR),
			new field_t("angle", F_ANGLEHACK),
			new field_t("goalentity", F_EDICT, FFL_NOSPAWN),
			new field_t("movetarget", F_EDICT, FFL_NOSPAWN),
			new field_t("enemy", F_EDICT, FFL_NOSPAWN),
			new field_t("oldenemy", F_EDICT, FFL_NOSPAWN),
			new field_t("activator", F_EDICT, FFL_NOSPAWN),
			new field_t("groundentity", F_EDICT, FFL_NOSPAWN),
			new field_t("teamchain", F_EDICT, FFL_NOSPAWN),
			new field_t("teammaster", F_EDICT, FFL_NOSPAWN),
			new field_t("owner", F_EDICT, FFL_NOSPAWN),
			new field_t("mynoise", F_EDICT, FFL_NOSPAWN),
			new field_t("mynoise2", F_EDICT, FFL_NOSPAWN),
			new field_t("target_ent", F_EDICT, FFL_NOSPAWN),
			new field_t("chain", F_EDICT, FFL_NOSPAWN),
			new field_t("prethink", F_FUNCTION, FFL_NOSPAWN),
			new field_t("think", F_FUNCTION, FFL_NOSPAWN),
			new field_t("blocked", F_FUNCTION, FFL_NOSPAWN),
			new field_t("touch", F_FUNCTION, FFL_NOSPAWN),
			new field_t("use", F_FUNCTION, FFL_NOSPAWN),
			new field_t("pain", F_FUNCTION, FFL_NOSPAWN),
			new field_t("die", F_FUNCTION, FFL_NOSPAWN),
			new field_t("stand", F_FUNCTION, FFL_NOSPAWN),
			new field_t("idle", F_FUNCTION, FFL_NOSPAWN),
			new field_t("search", F_FUNCTION, FFL_NOSPAWN),
			new field_t("walk", F_FUNCTION, FFL_NOSPAWN),
			new field_t("run", F_FUNCTION, FFL_NOSPAWN),
			new field_t("dodge", F_FUNCTION, FFL_NOSPAWN),
			new field_t("attack", F_FUNCTION, FFL_NOSPAWN),
			new field_t("melee", F_FUNCTION, FFL_NOSPAWN),
			new field_t("sight", F_FUNCTION, FFL_NOSPAWN),
			new field_t("checkattack", F_FUNCTION, FFL_NOSPAWN),
			new field_t("currentmove", F_MMOVE, FFL_NOSPAWN),
			new field_t("endfunc", F_FUNCTION, FFL_NOSPAWN),
			new field_t("item", F_ITEM)
		//need for item field in edict struct, FFL_SPAWNTEMP item will be skipped on saves
	};

	// temp spawn vars -- only valid when the spawn function is called
	static field_t fields_st[] =
		{
			new field_t("lip", F_INT, FFL_SPAWNTEMP),
			new field_t("distance", F_INT, FFL_SPAWNTEMP),
			new field_t("height", F_INT, FFL_SPAWNTEMP),
			new field_t("noise", F_LSTRING, FFL_SPAWNTEMP),
			new field_t("pausetime", F_FLOAT, FFL_SPAWNTEMP),
			new field_t("item", F_LSTRING, FFL_SPAWNTEMP),
			new field_t("gravity", F_LSTRING, FFL_SPAWNTEMP),
			new field_t("sky", F_LSTRING, FFL_SPAWNTEMP),
			new field_t("skyrotate", F_FLOAT, FFL_SPAWNTEMP),
			new field_t("skyaxis", F_VECTOR, FFL_SPAWNTEMP),
			new field_t("minyaw", F_FLOAT, FFL_SPAWNTEMP),
			new field_t("maxyaw", F_FLOAT, FFL_SPAWNTEMP),
			new field_t("minpitch", F_FLOAT, FFL_SPAWNTEMP),
			new field_t("maxpitch", F_FLOAT, FFL_SPAWNTEMP),
			new field_t("nextmap", F_LSTRING, FFL_SPAWNTEMP),
			};

	/**
	 * Slide off of the impacting object
	 * returns the blocked flags (1 = floor, 2 = step / wall).
	 */
	public static int ClipVelocity(float[] in, float[] normal, float[] out, float overbounce)
	{
		float backoff;
		float change;
		int i, blocked;

		blocked = 0;
		if (normal[2] > 0)
			blocked |= 1; // floor
		if (normal[2] == 0.0f)
			blocked |= 2; // step

		backoff = Math3D.DotProduct(in, normal) * overbounce;

		for (i = 0; i < 3; i++)
		{
			change = normal[i] * backoff;
			out[i] = in[i] - change;
			if (out[i] > -STOP_EPSILON && out[i] < STOP_EPSILON)
				out[i] = 0;
		}

		return blocked;
	}

	/**
	SV_FlyMove
	
	The basic solid body movement clip that slides along multiple planes
	Returns the clipflags if the velocity was modified (hit something solid)
	1 = floor
	2 = wall / step
	4 = dead stop
	*/
	public final static int MAX_CLIP_PLANES = 5;

	/*
	=============
	G_Find
	
	Searches all active entities for the next one that holds
	the matching string at fieldofs (use the FOFS() macro) in the structure.
	
	Searches beginning at the edict after from, or the beginning if null
	null will be returned if the end of the list is reached.
	
	=============
	*/

	/** 
	 * Finds an edict.
	 * Call with null as from parameter to search from array beginning.
	 */

	public static EdictIterator G_Find(EdictIterator from, EdictFindFilter eff, String s)
	{

		if (from == null)
			from = new EdictIterator(0);
		else
			from.i++;

		for (; from.i < globals.num_edicts; from.i++)
		{
			from.o = g_edicts[from.i];
			if (from.o.classname == null)
			{
				Com.Printf("edict with classname = null" + from.o.index);
			}

			if (!from.o.inuse)
				continue;

			if (eff.matches(from.o, s))
				return from;
		}

		return null;
	}

	// comfort version (rst)
	public static edict_t G_FindEdict(EdictIterator from, EdictFindFilter eff, String s)
	{
		EdictIterator ei = G_Find(from, eff, s);
		if (ei == null)
			return null;
		else
			return ei.o;
	}

	/** 
	 * Returns entities that have origins within a spherical area.
	*/
	public static EdictIterator findradius(EdictIterator from, float[] org, float rad)
	{
		float[] eorg = { 0, 0, 0 };
		int j;

		if (from == null)
			from = new EdictIterator(0);
		else
			from.i++;

		for (; from.i < globals.num_edicts; from.i++)
		{
			from.o = g_edicts[from.i];
			if (!from.o.inuse)
				continue;

			if (from.o.solid == SOLID_NOT)
				continue;

			for (j = 0; j < 3; j++)
				eorg[j] = org[j] - (from.o.s.origin[j] + (from.o.mins[j] + from.o.maxs[j]) * 0.5f);

			if (Math3D.VectorLength(eorg) > rad)
				continue;
			return from;
		}

		return null;
	}

	/**
	 * Searches all active entities for the next one that holds
	 * the matching string at fieldofs (use the FOFS() macro) in the structure.
	 *
	 *	Searches beginning at the edict after from, or the beginning if null 
	 *	null will be returned if the end of the list is reached.
	 */

	public static int MAXCHOICES = 8;

	public static edict_t G_PickTarget(String targetname)
	{
		int num_choices = 0;
		edict_t choice[] = new edict_t[MAXCHOICES];

		if (targetname == null)
		{
			gi.dprintf("G_PickTarget called with null targetname\n");
			return null;
		}

		EdictIterator es = null;

		while ((es = G_Find(es, findByTarget, targetname)) != null)
		{
			choice[num_choices++] = es.o;
			if (num_choices == MAXCHOICES)
				break;
		}

		if (num_choices == 0)
		{
			gi.dprintf("G_PickTarget: target " + targetname + " not found\n");
			return null;
		}

		return choice[Lib.rand() % num_choices];
	}

	public static float[] VEC_UP = { 0, -1, 0 };
	public static float[] MOVEDIR_UP = { 0, 0, 1 };
	public static float[] VEC_DOWN = { 0, -2, 0 };
	public static float[] MOVEDIR_DOWN = { 0, 0, -1 };

	public static void G_SetMovedir(float[] angles, float[] movedir)
	{
		if (Math3D.VectorCompare(angles, VEC_UP) != 0)
		{
			Math3D.VectorCopy(MOVEDIR_UP, movedir);
		}
		else if (Math3D.VectorCompare(angles, VEC_DOWN) != 0)
		{
			Math3D.VectorCopy(MOVEDIR_DOWN, movedir);
		}
		else
		{
			Math3D.AngleVectors(angles, movedir, null, null);
		}

		Math3D.VectorClear(angles);
	}

	public static String G_CopyString(String in)
	{
		return new String(in);
	}

	/*
	============
	G_TouchTriggers
	
	============
	*/
	public static void G_TouchTriggers(edict_t ent)
	{
		int i, num;
		edict_t touch[] = new edict_t[MAX_EDICTS], hit;

		// dead things don't activate triggers!
		if ((ent.client != null || (ent.svflags & SVF_MONSTER) != 0) && (ent.health <= 0))
			return;

		num = gi.BoxEdicts(ent.absmin, ent.absmax, touch, MAX_EDICTS, AREA_TRIGGERS);

		// be careful, it is possible to have an entity in this
		// list removed before we get to it (killtriggered)
		for (i = 0; i < num; i++)
		{
			hit = touch[i];
			if (!hit.inuse)
				continue;

			if (hit.touch == null)
				continue;
			hit.touch.touch(hit, ent, GameBase.dummyplane, null);
		}
	}

	public static pushed_t pushed[] = new pushed_t[MAX_EDICTS];
	static {
		for (int n = 0; n < MAX_EDICTS; n++)
			pushed[n] = new pushed_t();
	}

	public static int pushed_p;

	public static edict_t obstacle;

	/*
	=============
	M_CheckBottom
	
	Returns false if any part of the bottom of the entity is off an edge that
	is not a staircase.
	
	=============
	*/
	public static int c_yes, c_no;

	public static int STEPSIZE = 18;

	//	  ============================================================================
	/*
	================
	G_RunEntity
	
	================
	*/
	public static void G_RunEntity(edict_t ent)
	{
		if (ent.prethink != null)
			ent.prethink.think(ent);

		switch ((int) ent.movetype)
		{
			case MOVETYPE_PUSH :
			case MOVETYPE_STOP :
				SV.SV_Physics_Pusher(ent);
				break;
			case MOVETYPE_NONE :
				SV.SV_Physics_None(ent);
				break;
			case MOVETYPE_NOCLIP :
				SV.SV_Physics_Noclip(ent);
				break;
			case MOVETYPE_STEP :
				SV.SV_Physics_Step(ent);
				break;
			case MOVETYPE_TOSS :
			case MOVETYPE_BOUNCE :
			case MOVETYPE_FLY :
			case MOVETYPE_FLYMISSILE :
				SV.SV_Physics_Toss(ent);
				break;
			default :
				gi.error("SV_Physics: bad movetype " + (int) ent.movetype);
		}
	}

	/*
	================
	SV_NewChaseDir
	
	================
	*/
	public static int DI_NODIR = -1;
	public static void assert1(boolean cond)
	{
		if (!cond)
		{

			try
			{

				int a[] = null;
				int b = a[0];
			}
			catch (Exception e)
			{
				System.err.println("assertion failed!");
				e.printStackTrace();
			}

		}
	}

	public static void ClearBounds(float[] mins, float[] maxs)
	{
		mins[0] = mins[1] = mins[2] = 99999;
		maxs[0] = maxs[1] = maxs[2] = -99999;
	}

	public static void AddPointToBounds(float[] v, float[] mins, float[] maxs)
	{
		int i;
		float val;

		for (i = 0; i < 3; i++)
		{
			val = v[i];
			if (val < mins[i])
				mins[i] = val;
			if (val > maxs[i])
				maxs[i] = val;
		}
	}

	public static EdictFindFilter findByTarget = new EdictFindFilter()
	{
		public boolean matches(edict_t e, String s)
		{
			if (e.targetname == null)
				return false;
			return e.targetname.equalsIgnoreCase(s);
		}
	};

	public static EdictFindFilter findByClass = new EdictFindFilter()
	{
		public boolean matches(edict_t e, String s)
		{
			return e.classname.equalsIgnoreCase(s);
		}
	};

	//===================================================================

	public static void ShutdownGame()
	{
		gi.dprintf("==== ShutdownGame ====\n");

		//gi.FreeTags (TAG_LEVEL);
		//gi.FreeTags (TAG_GAME);
	}

	//======================================================================

	/*
	=================
	ClientEndServerFrames
	=================
	*/
	public static void ClientEndServerFrames()
	{
		int i;
		edict_t ent;

		// calc the player views now that all pushing
		// and damage has been added
		for (i = 0; i < maxclients.value; i++)
		{
			ent = g_edicts[1 + i];
			if (!ent.inuse || null == ent.client)
				continue;
			Game.ClientEndServerFrame(ent);
		}

	}

	/*
	=================
	CreateTargetChangeLevel
	
	Returns the created target changelevel
	=================
	*/
	public static edict_t CreateTargetChangeLevel(String map)
	{
		edict_t ent;

		ent = Game.G_Spawn();
		ent.classname = "target_changelevel";
		level.nextmap = map;
		ent.map = level.nextmap;
		return ent;
	}

	/*
	=================
	EndDMLevel
	
	The timelimit or fraglimit has been exceeded
	=================
	*/
	public static void EndDMLevel()
	{
		edict_t ent;
		//char * s, * t, * f;
		//static const char * seps = " ,\n\r";
		String s, t, f;
		String seps = " ,\n\r";

		// stay on same level flag
		if (((int) dmflags.value & DF_SAME_LEVEL) != 0)
		{
			Game.BeginIntermission(CreateTargetChangeLevel(level.mapname));
			return;
		}

		// see if it's in the map list
		if (sv_maplist.string.length() > 0)
		{
			s = sv_maplist.string;
			f = null;
			StringTokenizer tk = new StringTokenizer(s, seps);
			t = tk.nextToken();
			//t = strtok(s, seps);
			while (t != null)
			{
				if (Q_stricmp(t, level.mapname) == 0)
				{
					// it's in the list, go to the next one
					t = tk.nextToken();
					if (t == null)
					{ // end of list, go to first one
						if (f == null) // there isn't a first one, same level
							Game.BeginIntermission(CreateTargetChangeLevel(level.mapname));
						else
							Game.BeginIntermission(CreateTargetChangeLevel(f));
					}
					else
						Game.BeginIntermission(CreateTargetChangeLevel(t));
					return;
				}
				if (f == null)
					f = t;
				t = tk.nextToken();
			}

		}

		if (level.nextmap.length() > 0) // go to a specific map
			Game.BeginIntermission(CreateTargetChangeLevel(level.nextmap));
		else
		{ // search for a changelevel
			EdictIterator edit = null;
			edit = G_Find(edit, findByClass, "target_changelevel");
			if (edit == null)
			{ // the map designer didn't include a changelevel,
				// so create a fake ent that goes back to the same level
				Game.BeginIntermission(CreateTargetChangeLevel(level.mapname));
				return;
			}
			ent = edit.o;
			Game.BeginIntermission(ent);
		}
	}

	/*
	=================
	CheckNeedPass
	=================
	*/
	public static void CheckNeedPass()
	{
		int need;

		// if password or spectator_password has changed, update needpass
		// as needed
		if (password.modified || spectator_password.modified)
		{
			password.modified = spectator_password.modified = false;

			need = 0;

			if ((password.string.length() > 0) && 0 != Q_stricmp(password.string, "none"))
				need |= 1;
			if ((spectator_password.string.length() > 0) && 0 != Q_stricmp(spectator_password.string, "none"))
				need |= 2;

			gi.cvar_set("needpass", "" + need);
		}
	}

	/*
	=================
	CheckDMRules
	=================
	*/
	public static void CheckDMRules()
	{
		int i;
		gclient_t cl;

		if (level.intermissiontime != 0)
			return;

		if (0 == deathmatch.value)
			return;

		if (timelimit.value != 0)
		{
			if (level.time >= timelimit.value * 60)
			{
				gi.bprintf(PRINT_HIGH, "Timelimit hit.\n");
				EndDMLevel();
				return;
			}
		}

		if (fraglimit.value != 0)
		{
			for (i = 0; i < maxclients.value; i++)
			{
				cl = game.clients[i];
				if (!g_edicts[i + 1].inuse)
					continue;

				if (cl.resp.score >= fraglimit.value)
				{
					gi.bprintf(PRINT_HIGH, "Fraglimit hit.\n");
					EndDMLevel();
					return;
				}
			}
		}
	}

	/*
	=============
	ExitLevel
	=============
	*/
	public static void ExitLevel()
	{
		int i;
		edict_t ent;
		//char command[256];
		String command;

		command = "gamemap \"" + level.changemap + "\"\n";
		gi.AddCommandString(command);
		level.changemap = null;
		level.exitintermission = false;
		level.intermissiontime = 0;
		ClientEndServerFrames();

		// clear some things before going to next level
		for (i = 0; i < maxclients.value; i++)
		{
			ent = g_edicts[1 + i];
			if (!ent.inuse)
				continue;
			if (ent.health > ent.client.pers.max_health)
				ent.health = ent.client.pers.max_health;
		}

	}

	/*
	================
	G_RunFrame
	
	Advances the world by 0.1 seconds
	================
	*/
	public static void G_RunFrame()
	{
		int i;
		edict_t ent;

		level.framenum++;
		level.time = level.framenum * FRAMETIME;

		// choose a client for monsters to target this frame
		Game.AI_SetSightClient();

		// exit intermissions

		if (level.exitintermission)
		{
			ExitLevel();
			return;
		}

		//
		// treat each object in turn
		// even the world gets a chance to think
		//

		for (i = 0; i < globals.num_edicts; i++)
		{
			ent = g_edicts[i];
			if (!ent.inuse)
				continue;

			level.current_entity = ent;

			VectorCopy(ent.s.origin, ent.s.old_origin);

			// if the ground entity moved, make sure we are still on it
			if ((ent.groundentity != null) && (ent.groundentity.linkcount != ent.groundentity_linkcount))
			{
				ent.groundentity = null;
				if (0 == (ent.flags & (FL_SWIM | FL_FLY)) && (ent.svflags & SVF_MONSTER) != 0)
				{
					M.M_CheckGround(ent);
				}
			}

			if (i > 0 && i <= maxclients.value)
			{
				Game.ClientBeginServerFrame(ent);
				continue;
			}


			G_RunEntity(ent);
		}

		// see if it is time to end a deathmatch
		CheckDMRules();

		// see if needpass needs updated
		CheckNeedPass();

		// build the playerstate_t structures for all players   
		ClientEndServerFrames();
	}

	/*
	=================
	GetGameAPI
	
	Returns a pointer to the structure with all entry points
	and global variables
	=================
	*/

	public static game_export_t GetGameApi(game_import_t imp)
	{
		gi = imp;

		gi.pointcontents = new pmove_t.PointContentsAdapter()
		{
			public int pointcontents(float[] o)
			{
				return SV_WORLD.SV_PointContents(o);
			}
		};

		globals.apiversion = GAME_API_VERSION;
		/*
		globals.Init = InitGame;
		globals.Shutdown = ShutdownGame;
		globals.SpawnEntities = SpawnEntities;
		
		globals.WriteGame = WriteGame;
		globals.ReadGame = ReadGame;
		globals.WriteLevel = WriteLevel;
		globals.ReadLevel = ReadLevel;
		
		globals.ClientThink = ClientThink;
		globals.ClientConnect = ClientConnect;
		globals.ClientUserinfoChanged = ClientUserinfoChanged;
		globals.ClientDisconnect = ClientDisconnect;
		globals.ClientBegin = ClientBegin;
		globals.ClientCommand = ClientCommand;		
		globals.RunFrame = G_RunFrame;		
		globals.ServerCommand = ServerCommand;
		*/

		return globals;
	}
}