File: main.C

package info (click to toggle)
battleball 2.0-13
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,016 kB
  • ctags: 3,097
  • sloc: cpp: 15,310; makefile: 48; csh: 34
file content (821 lines) | stat: -rw-r--r-- 27,305 bytes parent folder | download | duplicates (3)
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
// Copyright (c) 1997 Philip A. Hardin (pahardin@cs.utexas.edu)
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License v2 or later.


// BattleBall
// Developed 10/16/93 to 09/09/97
// by Philip A. Hardin



#include <stdio.h>       // to get sprintf()
#include <string.h>      // to get strncpy()
#include <time.h>        // to get time(time_t *)
#include "bb.h"
#include "player.h"


const int
  MAXTEAMS= NUMINSIGNIA,        // max # of teams is the # of team insignias
  MAXMEMBERS= 4,                // max # of team members per team
  MAXPLAYERS= MAXTEAMS*MAXMEMBERS, // max # of total players
  TREES= 12,                    // default # of trees
  MTNS= 8;                      // default # of mountains

const coord
  HQDIST= 100,                  // default dist from hq to center of playfield
  MINHQDIST= 50,                // min     dist from hq to center of playfield
  MAXHQDIST= 450;               // max     dist from hq to center of playfield



/*=========================================================================*/
// The states of a game round
enum stateOfRound {
  counting,             // timer is counting down to begin playing
                        // (teams shown in text area)
  playing,              // playing the round; no goal scored yet
                        // (game status shown in text area)
  toppling,             // the (first) hq is now toppling over
                        // (teams & scores shown in text area)
  bouncing,             // the hq has fallen & bounced at least once
                        // (teams & updated scores shown in text area)
  roundEnding,          // the round is over (but _not_ the game)
  gameEnding            // a team has won, game is over
};


struct roundInfo
{ int   loserTeamNum,   // team whose hq was hit; -1 if no hq hit
        winnerTeamNum;  // team which hit ball before a goal was scored;
                        // -1 if no hq hit yet
  stateOfRound  state,  // state of game round
                prevState; // state of game round from last iteration
  long  cycles;         // # of iterations this round has been going
  int   timer;          // in seconds
};


/*=========================================================================*/
// BattleBall class.
// This is the main class for the game.  A single object of this class exists
// for the duration of the game.


class battleBall {
  char*         progName;
  int           numPlayers;             // starts w/ 0
  player        players[MAXPLAYERS];
  int           numTeams;               // starts w/ 0
  team          teams[MAXTEAMS];
  int           pointsToWin;            // points required to win game
  gobList       sceneryGobs;            // the "scenery" game objects, such
                                        // as trees, mntns, playfield, etc.
  coord         hqDist;                 // HQ-to-playfield-center distance
  bounGob       *bounds;                // "boundary" (playfield) game object
  ballGob       *ball;                  // ball game object
  hrznGob       *horizon;               // horizon game object
  tranGob       *train;                 // train game object; NULL if none
  int           intrinsicDelay;         // delay between each iteration (ms)
  int           startupDelay;           // delay before game starts (seconds)
  int           testIterations;         // =0: normal game play
                                        // >0: number of iterations to play
                                        //     before game automatically
                                        //     ends (used for testing)
  bool          doubleBuffer;           // double buffer game windows
  bool          keepInBounds;           // keep vhcls, etc. in  playfield
  bool          wantTrain;              // user wants the train in the game
  int           flybys;                 // 0->none, 1->few, 2->many
  gobList       track;                  // train track
  int           numTrees;               // number of tree game objects
  int           numMtns;                // number of mountain game objects


 public:
  battleBall(int argc, char *argv[]);
  ~battleBall();

  void  Play();

 private:
  void  ReadCmdLine(int argc, char *argv[]);
  void  ShowHelp();
  void  ShowBriefHelp();
  void  ActGobs(gobList& gobs);
  void  AddTeam(char *list);
  void  OpenAllDisplays();
  void  CreateWindows(int argc, char *argv[]);
  void  PlayOneRound(const gobList& sceneryGobs, int startTime, bool& done);
  void  InitForRound(gobList& gobs, int startTime, roundInfo& ri);
  void  FreeRound(gobList& gobs);
  void  InitTrack(gobList& gobs);
  void  InitScenery(gobList& gobs);
  void  ShowTeams(bbGfxTarget& gt);
  void  ShowStatus(bool);
  void  DrawTextArea(const roundInfo& ri, player& dude);
  void  DrawStartingMsg(bbGfxTarget& gt, int startTime);
  void  GetNextState(gobList& gobs, roundInfo& ri);
  void  DoFlyby(gobList& gobs);
};


/*-------------------------------------------------------------------------*/
battleBall::battleBall(int argc, char *argv[])
  : progName(argv[0]),
    numPlayers(0),
    numTeams(0),
    pointsToWin(3),
    train(NULL),
    intrinsicDelay(30),
    startupDelay(0),
    testIterations(0),
    hqDist(HQDIST),
    doubleBuffer(true),
    keepInBounds(true),
    wantTrain(false),
    flybys(1),
    numTrees(TREES),
    numMtns(MTNS)
{
  srand(time(0));
  team::insigniaRandomizer= rand()%NUMINSIGNIA;
  ReadCmdLine(argc,argv);
  InitFixed();  // init sine & cosine lookup tables
  InitRegions(numTeams, gob::fancy, hqDist, team::insigniaRandomizer);
  OpenAllDisplays();
  CreateWindows(argc,argv);
  InitScenery(sceneryGobs);
}


/*-------------------------------------------------------------------------*/
battleBall::~battleBall() {
  forii(numPlayers)
    if (players[i].active)
      players[i].CloseXStuff();
}


/*-------------------------------------------------------------------------*/
void battleBall::Play() {
  bool          done= false;
  XEvent        event;

  // The following loop seems to be necessary for X windows
  // to not lose initial drawing commands.

  forii(numPlayers)
    if (players[i].active)
      if (XPending(players[i].gt.disp) >0)
        XNextEvent(players[i].gt.disp, &event);

  while (not done) {
    PlayOneRound(sceneryGobs,startupDelay,done);
    startupDelay= 0;
  }
}


/*-------------------------------------------------------------------------*/
/*                           private methods                               */
/*-------------------------------------------------------------------------*/

// Parse BattleBall's command line, setting game options and creating
// players and teams

void battleBall::ReadCmdLine(int argc, char *argv[]) {
  bool  helpRequested= false;

  for (int i=1; i <argc; i++) {
    char *s= argv[i];
    if (!strcmp(s,"-ff")) {
      if (i+1 <argc) {
	char c= *argv[++i];
        if (c=='b')                  gob::ffire= blocked; 
	else if (c=='d')             gob::ffire= dangerous; 
	else if (c=='t')             gob::ffire= transparent;
	else cout << "bad friendly fire option '" << c
		  << "':  use (b)locked, (d)angerous, or (t)ransparent"
		  << endl;
      }
    }
    else if (!strcmp(s,"-flyby"))   flybys= 2;
    else if (!strcmp(s,"-grav"))
    { if (i+1 <argc)                 gob::gravity= -atof(argv[++i]);
      if (gob::gravity >-0.01)       gob::gravity= -0.01;
      if (gob::gravity <-0.20)       gob::gravity= -0.20;
    }
    else if (!strcmp(s,"-ag")) {
      if (i+1 <argc) player::SetGunnerDefaults(argv[++i]);
    }
    else if (!strcmp(s,"-h"))         helpRequested= true;
    else if (!strcmp(s,"-help"))      helpRequested= true;
    else if (!strcmp(s,"-id"))
         { if (i+1 <argc)             intrinsicDelay= atoi(argv[++i]);}
    else if (!strcmp(s,"-mtns")) {
      if (i+1 <argc) numMtns= atoi(argv[++i]);
    }
    else if (!strcmp(s,"-noag"))      player::autoGunnerAllowed= false;
    else if (!strcmp(s,"-noap"))      player::autoPilotAllowed= false;
    else if (!strcmp(s,"-nobang"))    vhclGob::useBangs= false;
    else if (!strcmp(s,"-noflyby"))   flybys= 0;
    else if (!strcmp(s,"-nopause"))   player::pauseAllowed= false;
    else if (!strcmp(s,"-noresize"))  player::refitWindow= false;
    else if (!strcmp(s,"-noshade"))   player::shadowsOn= false;
    else if (!strcmp(s,"-out"))       keepInBounds= false;
    else if (!strcmp(s,"-pts"))
         { if (i+1 <argc)             pointsToWin= atoi(argv[++i]);}
    else if (!strcmp(s,"-rad"))
    { if (i+1 <argc)                  hqDist= atoi(argv[++i]);
      if (hqDist <MINHQDIST)          hqDist= MINHQDIST;
      if (hqDist >MAXHQDIST)          hqDist= MAXHQDIST;
    }
    else if (!strcmp(s,"-sb"))        doubleBuffer= false;
    else if (!strcmp(s,"-sd"))
        {if (i+1 <argc)               startupDelay= atoi(argv[++i]);}
    else if (!strcmp(s,"-simple"))    gob::fancy= false;
    else if (!strcmp(s,"-snum")) {
      if (i+1 <argc)                  player::shellsPerPlayer= atoi(argv[++i]);
    }
    else if (!strcmp(s,"-spow")) {
      if (i+1 <argc)                  shllGob::power= atof(argv[++i]);
    }
    else if (!strcmp(s,"-svel"))
    { if (i+1 <argc)                  vhclGob::muzzleVel= atof(argv[++i]);
      if (vhclGob::muzzleVel <0.5)    vhclGob::muzzleVel= 0.5;
      if (vhclGob::muzzleVel >5.0)    vhclGob::muzzleVel= 5.0;
    }
    else if (!strcmp(s,"-insig")) {
      if (i+1 <argc)
	team::insigniaRandomizer= atoi(argv[++i]);
    }
    else if (!strcmp(s,"-train"))     wantTrain= true;
    else if (!strcmp(s,"-trees")) {
      if (i+1 <argc) numTrees= atoi(argv[++i]);
    }
    else if (!strcmp(s,"-wf"))        gob::classSolid= false;
    else if (!strcmp(s,"-z")) {
      if (i+1 <argc) {               testIterations= atoi(argv[++i]);
                                     srand(testIterations);
      }
    }
    else if (s[0]=='-') cout << "Unrecognized flag: " << s << "\n";
    else if (numTeams <MAXTEAMS)
      AddTeam(argv[i]);
  }

  if (wantTrain)
    players[numPlayers++]= player("comp",-1,0); // teamNum==-1 -> train dude

  if (helpRequested) {
    ShowHelp();
    exit(1);
  }

  else if (numTeams==0) {
    ShowBriefHelp();
    exit(1);
  }
}


/*-------------------------------------------------------------------------*/
void battleBall::ShowBriefHelp() {
  cout << "Usage:  battleball [options...] XDisplay[,XDisplay] [XDisplay]...\n";
  cout << "Type battleball -help for more info.\n";
}


/*-------------------------------------------------------------------------*/
void battleBall::ShowHelp() {
  cout <<
"                                BattleBall 2.0\n"
"                      A 3-D multiplayer game for X11 Windows\n"
"                              by Philip A. Hardin    \n\n";

  cout <<
"SYNTAX\n\n"
"    battleball [options...] XDisplay[,XDisplay] [XDisplay]...\n\n"

"DESCRIPTION\n\n"
"    BattleBall is a 3-D multiplayer game for X11 Windows.  Teams of human\n"
"    and computer players use tanks and helicopters to play soccer on a\n"
"    large playfield.\n\n"

"OPTIONS\n\n"
"    -ag ##l     Set players' auto-gunner settings.  The argument of this\n"
"                option is three characters: a single-digit number\n"
"                specifying firing accuracy, a single-digit number\n"
"                specifying firing frequency, and one of the following\n"
"                letters specifying target selection:\n"
"                  a - fire at all targets\n"
"                  b - fire only at the ball\n"
"                  v - fire only at vehicles\n"
"                  n - no targets (i.e. do not fire)\n"
"                This option affects players which appear after it on the\n"
"                command line.  The default is 43a.\n"
"    -ff d|b|t   Make accidental 'friendly fire' from fellow teammates\n"
"                  d - dangerous (the default)\n"
"                  b - blocked\n"
"                  t - transparent\n"
"                If you dislike your teammates, use the 'dangerous' option.\n"
"    -grav #     Set gravity.  Defaults to 0.031 m/iteration^2.\n"
"    -help       Show this help screen\n"
"    -id #       Set the inter-frame delay.  If the game runs too slow, set\n"
"                this lower; if the game seems to lag behind your\n"
"                keystrokes, set this higher.  Defaults to 30 milliseconds.\n"
"    -mtns #     Set the number of mountains.  Defaults to 8.\n"
"    -noag       Disable human players' auto-gunner capability.\n"
"    -noap       Disable human players' auto-pilot capability.\n"
"    -nobang     Disable 'bangs' (the flashes at the end of a gun barrel)\n"
"    -noflyby    Disable aircraft fly-bys.\n"
"    -nopause    Disable players' use of the pause ('P') key.\n"
"    -noresize   Do not automatically resize the window to fit the graphics.\n"
"    -noshade    Disable shadows.  Uses less cpu time.\n"
"    -out        Allow vehicles to go outside of the playfield.\n"
"    -pts #      Set number of points required to win.  Defaults to 3.\n"
"    -rad #      Set the 'radius' of the playfield.  Defaults to 100 meters.\n"
"    -sb         Single-buffer the game windows.\n"
"    -sd #       Set the startup delay.  The game will wait for this many\n"
"                seconds for players to get ready.\n"
"    -simple     Use simpler graphics.  Uses less cpu time.\n"
"    -snum #     Set number of shells per player.  Defaults to 3.\n"
"    -spow #     Set shell power.  Defaults to 1.6.\n"
"    -svel #     Set shell muzzle velocity.  Defaults to 1.5 meters/frame.\n"
"                At higher velocities, some collisions may not be reliably\n"
"                detected.\n"
"    -train      Include a train running on a track around the playfield.\n"
"    -trees #    Set the number of trees.  Defaults to 12.\n"
"    -wf         Use wireframe rendering.  Uses less cpu time.\n\n"

"PLAYERS & TEAMS\n\n"
"    Human players are created by specifying X displays on the command line.\n"
"    Computer players are created by specifying 'comp' in place of the X\n"
"    displays.\n\n"

"    Commas between X displays put players on the same team; spaces between\n"
"    X displays separate teams.  Teams may have any mix of human and\n"
"    computer players.\n\n"

"    Computer players may be created with specific auto-gunner settings.\n"
"    Use 'comp', followed by (no space) the three characters used in the -ag\n"
"    option, e.g. 'comp67b'.  This overrides the -ag option.\n\n"

"EXAMPLES\n\n"
"    battleball mitre:0\n"
"                Creates one team which has one human player on the X display\n"
"                'mitre:0'.\n\n"

"    battleball flavio:0  chirp:0,roar:0\n"
"                Sets up a one-player team against a two-player team.\n\n"

"    battleball parrot:0,comp  raven:0  comp,comp\n"
"                Creates three teams with a mix of human and computer\n"
"                players.\n\n"
"    battleball dunce:0  comp75a  comp,comp\n"
"                Creates three teams.  The first computer player gets\n"
"                special auto-gunner settings; the other computer players\n"
"                get default settings.\n\n"

"WEB PAGE\n\n"
"    Source code and pre-compiled executables are available from the\n"
"    BattleBall web page at  http://www.cs.utexas.edu/users/pahardin/bb.html\n\n"
;
}


/*-------------------------------------------------------------------------*/
// Add a team of one or more players to the game
// (This includes adding the players to the game)

void battleBall::AddTeam(char *list) {
  int   numMembers= 0;

  do {
    int dispNameLen= strcspn(list,",");  // num chars before next comma
    char* newDispName= new char[dispNameLen+1];
    strncpy(newDispName,list,dispNameLen);
    newDispName[dispNameLen]= '\0';
    if (numPlayers <MAXPLAYERS) {
      players[numPlayers]= player(newDispName,numTeams,numMembers);
      numMembers++;
      numPlayers++;
    }
    delete newDispName;

    list += dispNameLen +1;
  }
  while (list[-1] != ((char)('\0')));

  teams[numTeams]= team(numTeams);
  numTeams++;
}


/*-------------------------------------------------------------------------*/
// Tries to open the X displays of all the players.  If any of the displays
// can't be opened, reports the unopened ones, closes the opened ones, and
// terminates the program.

void battleBall::OpenAllDisplays() {
  bool success= true;

  forii(numPlayers)
    if (not players[i].computerPlayer)
      if (not players[i].gt.OpenDisplay(players[i].dispName))
        success= false;

  if (not success)
  { fori(numPlayers)
      if (not players[i].computerPlayer and players[i].gt.disp != NULL) {
        cout << "Closing " << players[i].dispName << "\n";
        XCloseDisplay(players[i].gt.disp);
      }
    exit (-1);
  }
}


/*-------------------------------------------------------------------------*/
void battleBall::CreateWindows(int argc, char *argv[]) {
  forii(numPlayers)
    if (not players[i].computerPlayer) {
      players[i].gt.CreateWindow(argc, argv);
      players[i].gt.AllocColors(bbColorNames,totalColors);
    }
}


/*-------------------------------------------------------------------------*/
// Show the teams and their players in the text subwindow

void battleBall::ShowTeams(bbGfxTarget& gt) {
  char  temp[80];

  gt.cursor= pt2d(0,0);
  gt.rightmost= 0;
  forii(numTeams) {
    gt << "       Score";
    sprintf(temp, "       %d", teams[i].score);
    gt << temp;
    gt.Box(0,0,6,2);
    gt.SetForeground(gt.Colors()[teams[i].colorNums[c_teamSecondary]]);
    Draw(&insignia2Rgns[i],gt,
	 pt2d(8+(gt.cursor.x+3)*gt.fontSize.x, gt.gfxSize.y+gt.fontSize.y),
	 0,pt2d(4,-4),true);
    gt.SetForeground(gt.Colors()[teams[i].colorNums[c_teamPrimary]]);
    Draw(&insigniaRgns[i],gt,
	 pt2d(8+(gt.cursor.x+3)*gt.fontSize.x, gt.gfxSize.y+gt.fontSize.y),
	 0,pt2d(4,-4),true);
    gt.SetForeground(gt.Black());
    forij(numPlayers) {
      player& dude= players[j];
      if (dude.TeamNum()==i)
	gt << dude.dispName;
    }
    gt.Tab(2);
  }
}


/*-------------------------------------------------------------------------*/
// Show the status display in all players' text subwindows if
// showingStatus is true; otherwise show the team list display in all
// players' text subwindows.

void battleBall::ShowStatus(bool showingStatus) {
  forii(numPlayers)
    players[i].textAreaShowingStatus= showingStatus;
}


/*-------------------------------------------------------------------------*/
// Draw the contents of the text subwindow

void battleBall::DrawTextArea(const roundInfo& ri, player& dude) {
  if (dude.textAreaShowingStatus)
    dude.DrawStatus(teams);
  else {
    if (ri.state != ri.prevState or dude.textAreaNeedsRedrawing) {
      dude.ClearTextArea();
      ShowTeams(dude.gt);
      dude.textAreaNeedsRedrawing= false;
    }
  }
}


/*-------------------------------------------------------------------------*/
// Create the game objects for the train track

void battleBall::InitTrack(gobList& gobs) {
  double trackData[]= {
    40,0,0, 40,0,0.063, 100,0,0, 40,0,-0.063, 40,0,0,
    40,0.166,0, 40,0.333,0, 40,0.5,0, 40,0.666,0, 40,0.833,0,
    259.5,1,0,
    40,1.166,0, 40,1.333,0, 40,1.5,0, 40,1.666,0, 40,1.833,0,
    0
  };
    
  pt3d pos= pt3d(-129.2,0,0);
  double *data= trackData;
  while (*data != 0) {
    double len= *data++;
    ang3d ang= ang3d((int)(data[0]*MA_PI), (int)(data[1]*MA_PI), 0);
    data += 2;
    railGob *rail= new railGob(tcomp(pos,ang),len);
    track.push_back(rail);
    gobs.push_back(rail);
    pos += pt3d(len,0,0) >> ang;
    if (pos.z <0.001) pos.z= 0;
  }

  gobs.push_back(new pllrGob(pt3d(-46.4,0,0)));
  gobs.push_back(new pllrGob(pt3d( 46.4,0,0)));
}


/*-------------------------------------------------------------------------*/
// Create the "scenery" game objects which only need to be created once per
// game, as opposed to once per game round.

void battleBall::InitScenery(gobList& gobs) {
  tcomp pos;
  bool  blocked;
  gobList::iterator gi;

  gobs.push_back(bounds= new bounGob(tcomp(pt3d(0,0)),keepInBounds));
//  gobs.push_back(new dstrGob(pt3d(-3*hqDist/2,2*hqDist,0),
//                             pt3d(1./8,0)));

  if (wantTrain) InitTrack(gobs);
		 
  forii(numMtns +numTrees) {
    do
    { if (i <numMtns)
        pos.Cart()= pt3d((rand()%256)*(HQDIST/16) +4*HQDIST, 0, 0);
      else
        pos.Cart()= pt3d(drand48()*2*hqDist, 0, 0);
      pos.Cart() >>= rand()%(2*MA_PI);

      blocked= false;
      for_(gi,gobs)
        if ((**gi).Contains(pos.Cart()))
          blocked= true;
    }
    while (blocked);

    pos.Ang()= rand()%(MA_PI/2);
    if (i <numMtns)
      gobs.push_back(new mntnGob(pos));
    else
      gobs.push_back(new treeGob(pos));
  }

  horizon= new hrznGob(pt3d(0,0));
  gobs.push_back(horizon);
}


/*-------------------------------------------------------------------------*/
// Initialize the game for a new game round

void battleBall::InitForRound(gobList& gobs, int startTime, roundInfo& ri) {
  gob::gobs= &gobs;

  forii(numTeams)
    teams[i].InitForRound(gobs,hqDist,numTeams);

  fori(numPlayers) {
    players[i].InitForRound(gobs,hqDist,numTeams,&track);
    if (testIterations >0) {
      players[i].autoPilot=  player::autoPilotAllowed;
      players[i].autoGunner= player::autoGunnerAllowed;
    }
    if (players[i].TeamNum()==-1)
      train= (tranGob*) players[i].Vhcl();
  }

  ball= new ballGob(pt3d(0,0),pt3d(0,0),-1);
  gobs.push_back(ball);

  ri.loserTeamNum= -1;
  ri.cycles= 0;
  if (startTime) {
    ri.timer= startTime +1;
    ri.state= ri.prevState= counting;
    ShowStatus(false);
  } else {
    ri.timer= 0;
    ri.state= ri.prevState= playing;
  }
}


/*-------------------------------------------------------------------------*/
// Free things when a game round ends

void battleBall::FreeRound(gobList& gobs) {
//  gobList::iterator gi;

  // free the gobs which were created during the play of this round
  // (i.e. _don't_ free gobs which were created by InitScenery() )
  /* -PAH
  for_(gi,gobs)
    if ((**gi).type != boun and (**gi).type != tree and
        (**gi).type != mntn and (**gi).type != hrzn)
      delete *gi;
      */
}


/*-------------------------------------------------------------------------*/
// Draw a message while the startup delay counts down

void battleBall::DrawStartingMsg(bbGfxTarget& gt, int startTime)
{ char  msg[60];
  sprintf(msg," Game starts in %d seconds ",startTime);
  XDrawImageString(gt.disp,gt.win,gt.gc,
		   (int)(29*gt.fontSize.x), (int)(2*gt.fontSize.y),
		   msg,strlen(msg));
  XDrawRectangle(gt.disp,gt.win,gt.gc,
                 (int)(29*gt.fontSize.x -1), (int)gt.fontSize.y +2,
                 (int)(strlen(msg)*gt.fontSize.x+1), (int)gt.fontSize.y);
}


/*-------------------------------------------------------------------------*/
// Advance the game round to its next state when appropriate

void battleBall::GetNextState(gobList& gobs, roundInfo& ri)
{ int   i;
  bldgGob *hq=NULL;

  ri.prevState= ri.state;
  if (ri.loserTeamNum != -1)
    hq= teams[ri.loserTeamNum].hq;
  if (testIterations >0)
    testIterations--;
  (ri.cycles)++;

  switch (ri.state) {
    case counting:
      if (ri.timer >0) ri.timer--;
      if (ri.timer==0) {
	ri.state= playing;
	ShowStatus(true);
      }
      break;

    case playing:
      for(i= 0; i <numTeams; i++)
        if (teams[i].hq->vel.Ang().xz != 0) {
	  ri.loserTeamNum= i;
          ri.winnerTeamNum= ball->teamNum;
          ri.state= toppling;
	  ShowStatus(false);
          break;
        }
      break;
      
    case toppling:
      if (hq->pos.Ang().xz != -MA_PI/2)
        hq->Topple();
      else {
        for(i= 0; i <numTeams; i++)
          if (i != ri.loserTeamNum)
            teams[i].score++;
        if (numTeams >=3 and
            ri.winnerTeamNum != -1 and
            ri.winnerTeamNum != ri.loserTeamNum)
          teams[ri.winnerTeamNum].score++;
        ri.state= bouncing;
      }
      break;

    case bouncing:
      if (hq->Altitude()==0 and hq->vel.Cart().z==0)
      { ri.state= roundEnding;
        for(i= 0; i <numTeams; i++)
          if (teams[i].score >= pointsToWin)
            ri.state= gameEnding;
      }
      break;

    case roundEnding:
    case gameEnding:
      break;
  }
}


/*-------------------------------------------------------------------------*/
// Make each game object "act", i.e. make it do whatever it does, for this
// iteration.  Depending on the game object and its state, "acting" can
// include morphing, setting its velocity, moving, colliding with other
// game objects, etc.

void battleBall::ActGobs(gobList& gobs) {
  gobList::iterator gi= gobs.begin();
  gobList::iterator nxtgob= gi;
  int numGobs= gobs.size();

  while (numGobs-- >0) {
    nxtgob++;
    gobs.Update((**gi).Act(),gi);
    gi=nxtgob;
  }
}


/*-------------------------------------------------------------------------*/
// Create an airplane or saucer ship game object to do a fly-by over the
// playfield.

void battleBall::DoFlyby(gobList& gobs) {
  bool isSaucer= rand()%2;
  tcomp pos(rand()%(2*MA_PI));
  tcomp vel= isSaucer ? pt3d(1.5,0) : pt3d(1,0);
  pos.Cart()= pt3d(-384*vel.Cart().x,0,24) >> pos.Ang();

  wingGob* g;
  if (isSaucer)
    g= new saucGob(pos,vel,-1);  // dummy team number - does it matter? -PAH
  else
    g= new plneGob(pos,vel,-1);

  g->animDir= 1;
  g->ctrl= vel;
  gobs.push_back(g);
}


/*-------------------------------------------------------------------------*/
// Play one full game round.
// Out: done = true if no human players are playing any longer

void battleBall::PlayOneRound(const gobList& sceneryGobs, int startTime,
                              bool& done) {
  gobList       gobs;
  int           numActivePlayers= numPlayers;
  roundInfo     ri;

  gobs= sceneryGobs;
  InitForRound(gobs,startTime,ri);

  while (ri.state != roundEnding and numActivePlayers >0) {
    if (not player::paused) {
      GetNextState(gobs,ri);
      if ((ri.cycles %512)==0 and
	  vhclGob::testVhcl==NULL and
	  (flybys==2 or (flybys==1 and (rand() & 0x700)==0x700))
	  )
	DoFlyby(gobs);
    }

    forii(numPlayers) {
      if (not player::paused and ri.state != counting)
        players[i].AutoPlay(gobs,numTeams,teams,*ball);
      if (testIterations==1)
        if (players[i].active)
          players[i].CloseXStuff();     // also sets active= false
      if (players[i].active)
        players[i].HandleEvents(gobs,train);
    }

    if (not player::paused) ActGobs(gobs);

    numActivePlayers= 0;
    fori(numPlayers) {
      player& dude= players[i];
      if (dude.active) {
        numActivePlayers++;
        dude.DrawView(gobs,teams,i,doubleBuffer,horizon);
        if (ri.state==counting)
          DrawStartingMsg(dude.gt,ri.timer);
        DrawTextArea(ri,dude);
        XFlush(dude.gt.disp);
      }
    }

    if (ri.state==counting)
      SleepFor(990*1000);
    else if (intrinsicDelay >0)
      SleepFor((intrinsicDelay)*1000);
  }

  done= (numActivePlayers==0);
  FreeRound(gobs);
}


/*=========================================================================*/
// A long and complicated main() function!

int
main (int argc, char *argv[]) {
  battleBall bb(argc,argv);
  bb.Play();
  return 0;
}