From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / hacks / penetrate.c
1 /* Copyright (c) 1999 Adam Miller adum@aya.yale.edu
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10
11  * penetrate simulates the arcade classic with the cities and the stuff
12  * shooting down from the sky and stuff. The computer plays against itself,
13  * desperately defending the forces of good against those thingies raining
14  * down. Bonus cities are awarded at ever-increasing intervals. Every five
15  * levels appears a bonus round. The computer player gets progressively
16  * more intelligent as the game progresses. Better aim, more economical with
17  * ammo, and better target selection. Points are in the bottom right, and
18  * high score is in the bottom left. Start with -smart to have the computer
19  * player skip the learning process.
20
21  Version: 0.2
22  -- fixed an AI bug that was keeping the computer player a tad weak
23  Version: 0.1
24  -- first release
25
26  */
27
28 #include "screenhack.h"
29
30 #define kSleepTime 10000 
31
32 #define font_height(font)               (font->ascent + font->descent)
33
34 #define kCityPause 500000
35 #define kLevelPause 1
36 #define SCORE_MISSILE 100
37 #define kFirstBonus 5000
38 #define kMinRate 30
39 #define kMaxRadius 100
40
41 typedef struct {
42   int alive;
43   int x, y;
44   int startx, starty;
45   int endx, endy;
46   int dcity;
47   float pos;
48   int enemies;
49   int jenis;
50   int splits;
51   XColor color;
52 } Missile;
53
54 typedef struct {
55   int alive;
56   int x, y, rad, oflaser;
57   int max, outgoing;
58   XColor color;
59 } Boom;
60
61 typedef struct {
62   int alive;
63   int x;
64   XColor color;
65 } City;
66
67 typedef struct {
68   int alive;
69   int x, y;
70   int startx, starty;
71   int endx, endy;
72   int oldx, oldy;
73   int oldx2, oldy2;
74   float velx, vely, fposx, fposy;
75   float lenMul;
76   XColor color;
77   int target;
78 } Laser;
79
80 #define kMaxMissiles 256
81 #define kMaxBooms 512
82 #define kMaxLasers 128
83 #define kBoomRad 40
84 #define kNumCities 5
85
86 #define kLaserLength 12
87
88 #define kMissileSpeed 0.003
89 #define kLaserSpeed (kMissileSpeed * 6)
90
91
92 struct state {
93   Display *dpy;
94   Window window;
95
96    XFontStruct *font, *scoreFont;
97    GC draw_gc, erase_gc, level_gc;
98    unsigned int default_fg_pixel;
99    XColor scoreColor;
100
101    int bgrowth;
102    int lrate, startlrate;
103    long loop;
104    long score, highscore;
105    long nextBonus;
106    int numBonus;
107    int bround;
108    long lastLaser;
109    int gamez;
110    int aim;
111    int econpersen;
112    int choosypersen;
113    int carefulpersen;
114    int smart;
115    Colormap cmap;
116
117    Missile missile[kMaxMissiles];
118    Boom boom[kMaxBooms];
119    City city[kNumCities];
120    Laser laser[kMaxLasers];
121    int blive[kNumCities];
122
123    int level, levMissiles, levFreq;
124
125    int draw_xlim, draw_ylim;
126    int draw_reset;
127 };
128
129
130 static void Explode(struct state *st, int x, int y, int max, XColor color, int oflaser)
131 {
132   int i;
133   Boom *m = 0;
134   for (i=0;i<kMaxBooms;i++)
135          if (!st->boom[i].alive) {
136                 m = &st->boom[i];
137                 break;
138          }
139   if (!m)
140          return;
141
142   m->alive = 1;
143   m->x = x;
144   m->y = y;
145   m->rad = 0;
146   if (max > kMaxRadius)
147          max = kMaxRadius;
148   m->max = max;
149   m->outgoing = 1;
150   m->color = color;
151   m->oflaser = oflaser;
152 }
153
154 static void launch (struct state *st, int xlim, int ylim, int src)
155 {
156   int i;
157   Missile *m = 0, *msrc;
158   for (i=0;i<kMaxMissiles;i++)
159          if (!st->missile[i].alive) {
160                 m = &st->missile[i];
161                 break;
162          }
163   if (!m)
164          return;
165
166   m->alive = 1;
167   m->startx = (random() % xlim);
168   m->starty = 0;
169   m->endy = ylim;
170   m->pos = 0.0;
171   m->jenis = random() % 360;
172   m->splits = 0;
173   if (m->jenis < 50) {
174     int j = ylim * 0.4;
175     if (j) {
176          m->splits = random() % j;
177          if (m->splits < ylim * 0.08)
178                 m->splits = 0;
179     }
180   }
181
182   /* special if we're from another missile */
183   if (src >= 0) {
184          int dc = random() % (kNumCities - 1);
185          msrc = &st->missile[src];
186          if (dc == msrc->dcity)
187                 dc++;
188          m->dcity = dc;
189          m->startx = msrc->x;
190          m->starty = msrc->y;
191          if (m->starty > ylim * 0.4 || m->splits <= m->starty)
192                 m->splits = 0;  /* too far down already */
193          m->jenis = msrc->jenis;
194   }
195   else
196          m->dcity = random() % kNumCities;
197   m->endx = st->city[m->dcity].x + (random() % 20) - 10;
198   m->x = m->startx;
199   m->y = m->starty;
200   m->enemies = 0;
201
202   if (!mono_p) {
203          hsv_to_rgb (m->jenis, 1.0, 1.0,
204                                          &m->color.red, &m->color.green, &m->color.blue);
205          m->color.flags = DoRed | DoGreen | DoBlue;
206          if (!XAllocColor (st->dpy, st->cmap, &m->color)) {
207                 m->color.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
208                 m->color.red = m->color.green = m->color.blue = 0xFFFF;
209          }
210   }
211 }
212
213 #define kExpHelp 0.2
214 #define kSpeedDiff 3.5
215 #define kMaxToGround 0.75
216 static int fire(struct state *st, int xlim, int ylim)
217 {
218   int i, j, cnt = 0;
219   int dcity;
220   long dx, dy, ex, ey;
221   Missile *mis = 0;
222   Laser *m = 0;
223   int untargeted = 0;
224   int choosy = 0, economic = 0, careful = 0;
225   int suitor[kMaxMissiles];
226   int livecity = 0;
227   int ytargetmin = ylim * 0.75;
228   int deepest = 0;
229   int misnum = 0;
230
231   choosy = (random() % 100) < st->choosypersen;
232   economic = (random() % 100) < st->econpersen;
233   careful = (random() % 100) < st->carefulpersen;
234
235   /* count our cities */
236   for (i=0;i<kNumCities;i++)
237          livecity += st->city[i].alive;
238   if (livecity == 0)
239          return 1;  /* no guns */
240
241   for (i=0;i<kMaxLasers;i++)
242          if (!st->laser[i].alive) {
243                 m = &st->laser[i];
244                 break;
245          }
246   if (!m)
247          return 1;
248
249   /* if no missiles on target, no need to be choosy */
250   if (choosy) {
251          int choo = 0;
252          for (j=0;j<kMaxMissiles;j++) {
253                 mis = &st->missile[j];
254                 if (!mis->alive || (mis->y > ytargetmin))
255                   continue;
256                 if (st->city[mis->dcity].alive)
257                   choo++;
258          }
259          if (choo == 0)
260                 choosy = 0;
261   }
262
263   for (j=0;j<kMaxMissiles;j++) {
264          mis = &st->missile[j];
265          suitor[j] = 0;
266          if (!mis->alive || (mis->y > ytargetmin))
267                 continue;
268          if (choosy && (st->city[mis->dcity].alive == 0))
269                 continue;
270          ey = mis->starty + ((float) (mis->endy - mis->starty)) * (mis->pos + kExpHelp + (1.0 - mis->pos) / kSpeedDiff);
271          if (ey > ylim * kMaxToGround)
272                 continue;  /* too far down */
273          cnt++;
274          suitor[j] = 1;
275   }
276
277   /* count missiles that are on target and not being targeted */
278   if (choosy && economic)
279          for (j=0;j<kMaxMissiles;j++)
280                 if (suitor[j] && st->missile[j].enemies == 0)
281                   untargeted++;
282
283   if (economic)
284          for (j=0;j<kMaxMissiles;j++) {
285                 if (suitor[j] && cnt > 1)
286                   if (st->missile[j].enemies > 0)
287                          if (st->missile[j].enemies > 1 || untargeted == 0) {
288                                 suitor[j] = 0;
289                                 cnt--;
290                          }
291                 /* who's closest? biggest threat */
292                 if (suitor[j] && st->missile[j].y > deepest)
293                   deepest = st->missile[j].y;
294          }
295
296   if (deepest > 0 && careful) {
297          /* only target deepest missile */
298          cnt = 1;
299          for (j=0;j<kMaxMissiles;j++)
300                 if (suitor[j] && st->missile[j].y != deepest)
301                   suitor[j] = 0;
302   }
303
304   if (cnt == 0)
305          return 1;  /* no targets available */
306   cnt = random() % cnt;
307   for (j=0;j<kMaxMissiles;j++)
308          if (suitor[j])
309                 if (cnt-- == 0) {
310                   mis = &st->missile[j];
311                   misnum = j;
312                   break;
313                 }
314
315   if (mis == 0)
316          return 1;  /* shouldn't happen */
317
318   dcity = random() % livecity;
319   for (j=0;j<kNumCities;j++)
320          if (st->city[j].alive)
321                 if (dcity-- == 0) {
322                   dcity = j;
323                   break;
324                 }
325   m->startx = st->city[dcity].x;
326   m->starty = ylim;
327   ex = mis->startx + ((float) (mis->endx - mis->startx)) * (mis->pos + kExpHelp + (1.0 - mis->pos) / kSpeedDiff);
328   ey = mis->starty + ((float) (mis->endy - mis->starty)) * (mis->pos + kExpHelp + (1.0 - mis->pos) / kSpeedDiff);
329   m->endx = ex + random() % 16 - 8 + (random() % st->aim) - st->aim / 2;
330   m->endy = ey + random() % 16 - 8 + (random() % st->aim) - st->aim / 2;
331   if (ey > ylim * kMaxToGround)
332          return 0;  /* too far down */
333   mis->enemies++;
334   m->target = misnum;
335   m->x = m->startx;
336   m->y = m->starty;
337   m->oldx = -1;
338   m->oldy = -1;
339   m->oldx2 = -1;
340   m->oldy2 = -1;
341   m->fposx = m->x;
342   m->fposy = m->y;
343   dx = (m->endx - m->x);
344   dy = (m->endy - m->y);
345   m->velx = dx / 100.0;
346   m->vely = dy / 100.0;
347   m->alive = 1;
348   /* m->lenMul = (kLaserLength * kLaserLength) / (m->velx * m->velx + m->vely * m->vely); */
349   m->lenMul = -(kLaserLength / m->vely);
350
351   if (!mono_p) {
352          m->color.blue = 0x0000;
353          m->color.green = 0xFFFF;
354          m->color.red = 0xFFFF;
355          m->color.flags = DoRed | DoGreen | DoBlue;
356          if (!XAllocColor (st->dpy, st->cmap, &m->color)) {
357                 m->color.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
358                 m->color.red = m->color.green = m->color.blue = 0xFFFF;
359          }
360   }
361   return 1;
362 }
363
364 static void *
365 penetrate_init (Display *dpy, Window window)
366 {
367   struct state *st = (struct state *) calloc (1, sizeof(*st));
368   int i;
369   const char *levelfont = "-*-courier-*-r-*-*-*-380-*-*-*-*-*-*";
370   const char *scorefont = "-*-helvetica-*-r-*-*-*-180-*-*-*-*-*-*";
371   XGCValues gcv;
372   XWindowAttributes xgwa;
373
374   st->dpy = dpy;
375   st->window = window;
376
377   XGetWindowAttributes (st->dpy, st->window, &xgwa);
378   st->cmap = xgwa.colormap;
379
380   st->lrate = 80;
381   st->nextBonus = kFirstBonus;
382   st->aim = 180;
383
384   st->smart = get_boolean_resource(st->dpy, "smart","Boolean");
385   st->bgrowth = get_integer_resource (st->dpy, "bgrowth", "Integer");
386   st->lrate = get_integer_resource (st->dpy, "lrate", "Integer");
387   if (st->bgrowth < 0) st->bgrowth = 2;
388   if (st->lrate < 0) st->lrate = 2;
389   st->startlrate = st->lrate;
390
391   st->font = XLoadQueryFont(st->dpy, levelfont);
392   if (!st->font) {
393     fprintf (stderr, "%s: could not load font %s.\n", progname, levelfont);
394     st->font = XLoadQueryFont(st->dpy, scorefont);
395     if (! st->font)
396       st->font = XLoadQueryFont(st->dpy, "fixed");
397     if (! st->font) abort();
398   }
399
400   st->scoreFont = XLoadQueryFont(st->dpy, scorefont);
401   if (!st->scoreFont) {
402     fprintf (stderr, "%s: could not load font %s.\n", progname, scorefont);
403     st->scoreFont = XLoadQueryFont(st->dpy, levelfont);
404     if (! st->scoreFont)
405       st->scoreFont = XLoadQueryFont(st->dpy, "fixed");
406     if (! st->scoreFont) abort();
407   }
408
409   for (i = 0; i < kMaxMissiles; i++)
410     st->missile[i].alive = 0;
411
412   for (i = 0; i < kMaxLasers; i++)
413     st->laser[i].alive = 0;
414
415   for (i = 0; i < kMaxBooms; i++)
416     st->boom[i].alive = 0;
417
418   for (i = 0; i < kNumCities; i++) {
419          City *m = &st->city[i];
420     m->alive = 1;
421          m->color.red = m->color.green = m->color.blue = 0xFFFF;
422          m->color.blue = 0x1111; m->color.green = 0x8888;
423          m->color.flags = DoRed | DoGreen | DoBlue;
424          if (!XAllocColor (st->dpy, st->cmap, &m->color)) {
425                 m->color.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
426                 m->color.red = m->color.green = m->color.blue = 0xFFFF;
427          }
428   }
429
430   gcv.foreground = st->default_fg_pixel =
431     get_pixel_resource(st->dpy, st->cmap, "foreground", "Foreground");
432   gcv.font = st->scoreFont->fid;
433   st->draw_gc = XCreateGC(st->dpy, st->window, GCForeground | GCFont, &gcv);
434   gcv.font = st->font->fid;
435   st->level_gc = XCreateGC(st->dpy, st->window, GCForeground | GCFont, &gcv);
436   XSetForeground (st->dpy, st->level_gc, st->city[0].color.pixel);
437   gcv.foreground = get_pixel_resource(st->dpy, st->cmap, "background", "Background");
438   st->erase_gc = XCreateGC(st->dpy, st->window, GCForeground, &gcv);
439
440 # ifdef HAVE_JWXYZ
441   jwxyz_XSetAntiAliasing (st->dpy, st->erase_gc, False);
442   jwxyz_XSetAntiAliasing (st->dpy, st->draw_gc, False);
443 # endif
444
445
446   /* make a gray color for score */
447   if (!mono_p) {
448          st->scoreColor.red = st->scoreColor.green = st->scoreColor.blue = 0xAAAA;
449          st->scoreColor.flags = DoRed | DoGreen | DoBlue;
450          if (!XAllocColor (st->dpy, st->cmap, &st->scoreColor)) {
451                 st->scoreColor.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
452                 st->scoreColor.red = st->scoreColor.green = st->scoreColor.blue = 0xFFFF;
453          }
454   }
455
456   XClearWindow(st->dpy, st->window);
457   return st;
458 }
459
460 static void DrawScore(struct state *st, int xlim, int ylim)
461 {
462   char buf[16];
463   int width, height;
464   sprintf(buf, "%ld", st->score);
465   width = XTextWidth(st->scoreFont, buf, strlen(buf));
466   height = font_height(st->scoreFont);
467   XSetForeground (st->dpy, st->draw_gc, st->scoreColor.pixel);
468   XFillRectangle(st->dpy, st->window, st->erase_gc,
469                                   xlim - width - 6, ylim - height - 2, width + 6, height + 2);
470   XDrawString(st->dpy, st->window, st->draw_gc, xlim - width - 2, ylim - 2,
471                     buf, strlen(buf));
472
473   sprintf(buf, "%ld", st->highscore);
474   width = XTextWidth(st->scoreFont, buf, strlen(buf));
475   XFillRectangle(st->dpy, st->window, st->erase_gc,
476                                   4, ylim - height - 2, width + 4, height + 2);
477   XDrawString(st->dpy, st->window, st->draw_gc, 4, ylim - 2,
478                     buf, strlen(buf));
479 }
480
481 static void AddScore(struct state *st, int xlim, int ylim, long dif)
482 {
483   int i, sumlive = 0;
484   for (i=0;i<kNumCities;i++)
485          sumlive += st->city[i].alive;
486   if (sumlive == 0)
487          return;   /* no cities, not possible to score */
488
489   st->score += dif;
490   if (st->score > st->highscore)
491          st->highscore = st->score;
492   DrawScore(st, xlim, ylim);
493 }
494
495 static void DrawCity(struct state *st, int x, int y, XColor col)
496 {
497          XSetForeground (st->dpy, st->draw_gc, col.pixel);
498          XFillRectangle(st->dpy, st->window, st->draw_gc,
499                                   x - 30, y - 40, 60, 40);
500          XFillRectangle(st->dpy, st->window, st->draw_gc,
501                                                  x - 20, y - 50, 10, 10);
502          XFillRectangle(st->dpy, st->window, st->draw_gc,
503                                   x + 10, y - 50, 10, 10);
504 }
505
506 static void DrawCities(struct state *st, int xlim, int ylim)
507 {
508   int i, x;
509   for (i = 0; i < kNumCities; i++) {
510          City *m = &st->city[i];
511          if (!m->alive)
512                 continue;
513          x = (i + 1) * (xlim / (kNumCities + 1));
514          m->x = x;
515
516          DrawCity(st, x, ylim, m->color);
517   }
518 }
519
520 static void LoopMissiles(struct state *st, int xlim, int ylim)
521 {
522   int i, j, max = 0;
523   for (i = 0; i < kMaxMissiles; i++) {
524          int old_x, old_y;
525          Missile *m = &st->missile[i];
526          if (!m->alive)
527                 continue;
528          old_x = m->x;
529          old_y = m->y;
530          m->pos += kMissileSpeed;
531          m->x = m->startx + ((float) (m->endx - m->startx)) * m->pos;
532          m->y = m->starty + ((float) (m->endy - m->starty)) * m->pos;
533
534       /* erase old one */
535
536          XSetLineAttributes(st->dpy, st->draw_gc, 4, 0,0,0);
537     XSetForeground (st->dpy, st->draw_gc, m->color.pixel);
538          XDrawLine(st->dpy, st->window, st->draw_gc,
539                                   old_x, old_y, m->x, m->y);
540
541          /* maybe split off a new missile? */
542          if (m->splits && (m->y > m->splits)) {
543                 m->splits = 0;
544                 launch(st, xlim, ylim, i);
545          }
546          
547          if (m->y >= ylim) {
548                 m->alive = 0;
549                 if (st->city[m->dcity].alive) {
550                   st->city[m->dcity].alive = 0;
551                   Explode(st, m->x, m->y, kBoomRad * 2, m->color, 0);
552                 }
553          }
554
555          /* check hitting explosions */
556          for (j=0;j<kMaxBooms;j++) {
557                 Boom *b = &st->boom[j];
558                 if (!b->alive)
559                   continue;
560                 else {
561                   int dx = abs(m->x - b->x);
562                   int dy = abs(m->y - b->y);
563                   int r = b->rad + 2;
564                   if ((dx < r) && (dy < r))
565                          if (dx * dx + dy * dy < r * r) {
566                                 m->alive = 0;
567                                 max = b->max + st->bgrowth - kBoomRad;
568                                 AddScore(st, xlim, ylim, SCORE_MISSILE);
569                   }
570                 }
571          }
572
573          if (m->alive == 0) {
574                 float my_pos;
575                 /* we just died */
576                 Explode(st, m->x, m->y, kBoomRad + max, m->color, 0);
577                 XSetLineAttributes(st->dpy, st->erase_gc, 4, 0,0,0);
578                 /* In a perfect world, we could simply erase a line from
579                    (m->startx, m->starty) to (m->x, m->y). This is not a
580                    perfect world. */
581                 old_x = m->startx;
582                 old_y = m->starty;
583                 my_pos = kMissileSpeed;
584                 while (my_pos <= m->pos) {
585                         m->x = m->startx + ((float) (m->endx - m->startx)) * my_pos;
586                         m->y = m->starty + ((float) (m->endy - m->starty)) * my_pos;
587                         XDrawLine(st->dpy, st->window, st->erase_gc, old_x, old_y, m->x, m->y);
588                         old_x = m->x;
589                         old_y = m->y;
590                         my_pos += kMissileSpeed;
591                 }
592          }
593   }
594 }
595
596 static void LoopLasers(struct state *st, int xlim, int ylim)
597 {
598   int i, j, miny = ylim * 0.8;
599   int x, y;
600   for (i = 0; i < kMaxLasers; i++) {
601          Laser *m = &st->laser[i];
602          if (!m->alive)
603                 continue;
604
605          if (m->oldx != -1) {
606                  XSetLineAttributes(st->dpy, st->erase_gc, 2, 0,0,0);
607                  XDrawLine(st->dpy, st->window, st->erase_gc,
608                                   m->oldx2, m->oldy2, m->oldx, m->oldy);
609          }
610
611          m->fposx += m->velx;
612          m->fposy += m->vely;
613          m->x = m->fposx;
614          m->y = m->fposy;
615          
616          x = m->fposx + (-m->velx * m->lenMul);
617          y = m->fposy + (-m->vely * m->lenMul);
618
619          m->oldx = x;
620          m->oldy = y;
621
622          XSetLineAttributes(st->dpy, st->draw_gc, 2, 0,0,0);
623     XSetForeground (st->dpy, st->draw_gc, m->color.pixel);
624          XDrawLine(st->dpy, st->window, st->draw_gc,
625                                   m->x, m->y, x, y);
626
627          m->oldx2 = m->x;
628          m->oldy2 = m->y;
629          m->oldx = x;
630          m->oldy = y;
631          
632          if (m->y < m->endy) {
633                 m->alive = 0;
634          }
635
636          /* check hitting explosions */
637          if (m->y < miny)
638                 for (j=0;j<kMaxBooms;j++) {
639                   Boom *b = &st->boom[j];
640                   if (!b->alive)
641                          continue;
642                   else {
643                          int dx = abs(m->x - b->x);
644                          int dy = abs(m->y - b->y);
645                          int r = b->rad + 2;
646                          if (b->oflaser)
647                                 continue;
648                          if ((dx < r) && (dy < r))
649                                 if (dx * dx + dy * dy < r * r) {
650                                   m->alive = 0;
651                                   /* one less enemy on this missile -- it probably didn't make it */
652                                   if (st->missile[m->target].alive)
653                                          st->missile[m->target].enemies--;
654                                 }
655                   }
656                 }
657          
658          if (m->alive == 0) {
659                 /* we just died */
660                 XDrawLine(st->dpy, st->window, st->erase_gc,
661                                   m->x, m->y, x, y);
662                 Explode(st, m->x, m->y, kBoomRad, m->color, 1);
663          }
664   }
665 }
666
667 static void LoopBooms(struct state *st, int xlim, int ylim)
668 {
669   int i;
670   for (i = 0; i < kMaxBooms; i++) {
671          Boom *m = &st->boom[i];
672          if (!m->alive)
673                 continue;
674          
675          if (st->loop & 1) {
676                 if (m->outgoing) {
677                   m->rad++;
678                   if (m->rad >= m->max)
679                          m->outgoing = 0;
680                   XSetLineAttributes(st->dpy, st->draw_gc, 1, 0,0,0);
681                   XSetForeground (st->dpy, st->draw_gc, m->color.pixel);
682                   XDrawArc(st->dpy, st->window, st->draw_gc, m->x - m->rad, m->y - m->rad, m->rad * 2, m->rad * 2, 0, 360 * 64);
683                 }
684                 else {
685                   XSetLineAttributes(st->dpy, st->erase_gc, 1, 0,0,0);
686                   XDrawArc(st->dpy, st->window, st->erase_gc, m->x - m->rad, m->y - m->rad, m->rad * 2, m->rad * 2, 0, 360 * 64);
687                   m->rad--;
688                   if (m->rad <= 0)
689                          m->alive = 0;
690                 }
691          }
692   }
693 }
694
695
696 /* after they die, let's change a few things */
697 static void Improve(struct state *st)
698 {
699   if (st->smart)
700          return;
701   if (st->level > 20)
702          return;  /* no need, really */
703   st->aim -= 4;
704   if (st->level <= 2) st->aim -= 8;
705   if (st->level <= 5) st->aim -= 6;
706   if (st->gamez < 3)
707          st->aim -= 10;
708   st->carefulpersen += 6;
709   st->choosypersen += 4;
710   if (st->level <= 5) st->choosypersen += 3;
711   st->econpersen += 4;
712   st->lrate -= 2;
713   if (st->startlrate < kMinRate) {
714          if (st->lrate < st->startlrate)
715                 st->lrate = st->startlrate;
716   }
717   else {
718          if (st->lrate < kMinRate)
719                 st->lrate = kMinRate;
720   }
721   if (st->level <= 5) st->econpersen += 3;
722   if (st->aim < 1) st->aim = 1;
723   if (st->choosypersen > 100) st->choosypersen = 100;
724   if (st->carefulpersen > 100) st->carefulpersen = 100;
725   if (st->econpersen > 100) st->econpersen = 100;
726 }
727
728 static void NewLevel(struct state *st, int xlim, int ylim)
729 {
730   char buf[32];
731   int width, i, sumlive = 0;
732   int liv[kNumCities];
733   int freecity = 0;
734
735   if (st->level == 0) {
736          st->level++;
737          goto END_LEVEL;
738   }
739
740   /* check for a free city */
741   if (st->score >= st->nextBonus) {
742          st->numBonus++;
743          st->nextBonus += kFirstBonus * st->numBonus;
744          freecity = 1;
745   }
746
747   for (i=0;i<kNumCities;i++) {
748          if (st->bround)
749                 st->city[i].alive = st->blive[i];
750          liv[i] = st->city[i].alive;
751          sumlive += liv[i];
752          if (!st->bround)
753                 st->city[i].alive = 0;
754   }
755
756   /* print out screen */
757   XFillRectangle(st->dpy, st->window, st->erase_gc,
758                                   0, 0, xlim, ylim);
759   if (st->bround)
760          sprintf(buf, "Bonus Round Over");
761   else {
762          if (sumlive || freecity)
763                 sprintf(buf, "Level %d Cleared", st->level);
764          else
765                 sprintf(buf, "GAME OVER");
766   }
767   if (st->level > 0) {
768          width = XTextWidth(st->font, buf, strlen(buf));
769          XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - width / 2, ylim / 2 - font_height(st->font) / 2,
770                                          buf, strlen(buf));
771          XSync(st->dpy, False);
772          usleep(1000000);
773   }
774
775   if (!st->bround) {
776          if (sumlive || freecity) {
777                 int sumwidth;
778                 /* draw live cities */
779                 XFillRectangle(st->dpy, st->window, st->erase_gc,
780                                                         0, ylim - 100, xlim, 100);
781
782                 sprintf(buf, "X %ld", st->level * 100L);
783                 /* how much they get */
784                 sumwidth = XTextWidth(st->font, buf, strlen(buf));
785                 /* add width of city */
786                 sumwidth += 60;
787                 /* add spacer */
788                 sumwidth += 40;
789                 DrawCity(st, xlim / 2 - sumwidth / 2 + 30, ylim * 0.70, st->city[0].color);
790                 XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - sumwidth / 2 + 40 + 60, ylim * 0.7, buf, strlen(buf));
791                 for (i=0;i<kNumCities;i++) {
792                   if (liv[i]) {
793                          st->city[i].alive = 1;
794                          AddScore(st, xlim, ylim, 100 * st->level);
795                          DrawCities(st, xlim, ylim);
796                          XSync(st->dpy, False);
797                          usleep(kCityPause);
798                   }
799                 }
800          }
801          else {
802                 /* we're dead */
803                 usleep(3000000);
804
805                 /* start new */
806                 st->gamez++;
807                 Improve(st);
808                 for (i=0;i<kNumCities;i++)
809                   st->city[i].alive = 1;
810                 st->level = 0;
811                 st->loop = 1;
812                 st->score = 0;
813                 st->nextBonus = kFirstBonus;
814                 st->numBonus = 0;
815                 DrawCities(st, xlim, ylim);
816          }
817   }
818
819   /* do free city part */
820   if (freecity && sumlive < 5) {
821          int ncnt = random() % (5 - sumlive) + 1;
822          for (i=0;i<kNumCities;i++)
823                 if (!st->city[i].alive)
824                   if (!--ncnt)
825                          st->city[i].alive = 1;
826          strcpy(buf, "Bonus City");
827          width = XTextWidth(st->font, buf, strlen(buf));
828          XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - width / 2, ylim / 4, buf, strlen(buf));
829          DrawCities(st, xlim, ylim);
830          XSync(st->dpy, False);
831          usleep(1000000);
832   }
833
834   XFillRectangle(st->dpy, st->window, st->erase_gc,
835                                           0, 0, xlim, ylim - 100);
836   
837   if (!st->bround)
838          st->level++;
839   if (st->level == 1) {
840          st->nextBonus = kFirstBonus;
841   }
842
843   if (st->level > 3 && (st->level % 5 == 1)) {
844          if (st->bround) {
845                 st->bround = 0;
846                 DrawCities(st, xlim, ylim);
847          }
848          else {
849                 /* bonus round */
850                 st->bround = 1;
851                 st->levMissiles = 20 + st->level * 10;
852                 st->levFreq = 10;
853                 for (i=0;i<kNumCities;i++)
854                   st->blive[i] = st->city[i].alive;
855                 sprintf(buf, "Bonus Round");
856                 width = XTextWidth(st->font, buf, strlen(buf));
857                 XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - width / 2, ylim / 2 - font_height(st->font) / 2, buf, strlen(buf));
858                 XSync(st->dpy, False);
859                 usleep(1000000);
860                 XFillRectangle(st->dpy, st->window, st->erase_gc,
861                                                         0, 0, xlim, ylim - 100);
862          }
863   }
864
865  END_LEVEL: ;
866
867   if (!st->bround) {
868          st->levMissiles = 5 + st->level * 3;
869          if (st->level > 5)
870                 st->levMissiles += st->level * 5;
871          /*  levMissiles = 2; */
872          st->levFreq = 120 - st->level * 5;
873          if (st->levFreq < 30)
874                 st->levFreq = 30;
875   }
876
877   /* ready to fire */
878   st->lastLaser = 0;
879 }
880
881
882 static unsigned long
883 penetrate_draw (Display *dpy, Window window, void *closure)
884 {
885   struct state *st = (struct state *) closure;
886   XWindowAttributes xgwa;
887
888   if (st->draw_reset)
889     {
890       st->draw_reset = 0;
891       DrawCities(st, st->draw_xlim, st->draw_ylim);
892     }
893
894   XGetWindowAttributes(st->dpy, st->window, &xgwa);
895   st->draw_xlim = xgwa.width;
896   st->draw_ylim = xgwa.height;
897
898   /* see if just started */
899   if (st->loop == 0) {
900          if (st->smart) {
901                 st->choosypersen = st->econpersen = st->carefulpersen = 100;
902                 st->lrate = kMinRate; st->aim = 1;
903          }
904          NewLevel(st, st->draw_xlim, st->draw_ylim);
905          DrawScore(st, st->draw_xlim, st->draw_ylim);
906   }
907
908   st->loop++;
909
910   if (st->levMissiles == 0) {
911          /* see if anything's still on the screen, to know when to end level */
912          int i;
913          for (i=0;i<kMaxMissiles;i++)
914                 if (st->missile[i].alive)
915                   goto END_CHECK;
916          for (i=0;i<kMaxBooms;i++)
917                 if (st->boom[i].alive)
918                   goto END_CHECK;
919          for (i=0;i<kMaxLasers;i++)
920                 if (st->laser[i].alive)
921                   goto END_CHECK;
922          /* okay, nothing's alive, start end of level countdown */
923          usleep(kLevelPause*1000000);
924          NewLevel(st, st->draw_xlim, st->draw_ylim);
925          goto END;
926   END_CHECK: ;
927   }
928   else if ((random() % st->levFreq) == 0) {
929          launch(st, st->draw_xlim, st->draw_ylim, -1);
930          st->levMissiles--;
931   }
932
933   if (st->loop - st->lastLaser >= st->lrate) {
934          if (fire(st, st->draw_xlim, st->draw_ylim))
935                 st->lastLaser = st->loop;
936   }
937
938   if ((st->loop & 7) == 0)
939     st->draw_reset = 1;
940
941   LoopMissiles(st, st->draw_xlim, st->draw_ylim);
942   LoopLasers(st, st->draw_xlim, st->draw_ylim);
943   LoopBooms(st, st->draw_xlim, st->draw_ylim);
944
945  END:
946   return kSleepTime;
947 }
948
949 static void
950 penetrate_reshape (Display *dpy, Window window, void *closure, 
951                  unsigned int w, unsigned int h)
952 {
953   XClearWindow (dpy, window);
954 }
955
956 static Bool
957 penetrate_event (Display *dpy, Window window, void *closure, XEvent *event)
958 {
959   return False;
960 }
961
962 static void
963 penetrate_free (Display *dpy, Window window, void *closure)
964 {
965   struct state *st = (struct state *) closure;
966   free (st);
967 }
968
969
970 static const char *penetrate_defaults [] = {
971   ".background: black",
972   ".foreground: white",
973   "*fpsTop:     true",
974   "*fpsSolid:   true",
975   "*bgrowth:    5",
976   "*lrate:      80",
977   "*smart:      False",
978   0
979 };
980
981 static XrmOptionDescRec penetrate_options [] = {
982   { "-bgrowth",         ".bgrowth",     XrmoptionSepArg, 0 },
983   { "-lrate",           ".lrate",       XrmoptionSepArg, 0 },
984   {"-smart",            ".smart",       XrmoptionNoArg, "True" },
985   { 0, 0, 0, 0 }
986 };
987
988 XSCREENSAVER_MODULE ("Penetrate", penetrate)