1 /* Copyright (c) 1999 Adam Miller adum@aya.yale.edu
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
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.
22 -- fixed an AI bug that was keeping the computer player a tad weak
28 #include "screenhack.h"
30 #define kSleepTime 10000
32 #define font_height(font) (font->ascent + font->descent)
34 #define kCityPause 500000
36 #define SCORE_MISSILE 100
37 #define kFirstBonus 5000
39 #define kMaxRadius 100
56 int x, y, rad, oflaser;
74 float velx, vely, fposx, fposy;
80 #define kMaxMissiles 256
82 #define kMaxLasers 128
86 #define kLaserLength 12
88 #define kMissileSpeed 0.003
89 #define kLaserSpeed (kMissileSpeed * 6)
96 XFontStruct *font, *scoreFont;
97 GC draw_gc, erase_gc, level_gc;
98 unsigned int default_fg_pixel;
102 int lrate, startlrate;
104 long score, highscore;
117 Missile missile[kMaxMissiles];
118 Boom boom[kMaxBooms];
119 City city[kNumCities];
120 Laser laser[kMaxLasers];
121 int blive[kNumCities];
123 int level, levMissiles, levFreq;
125 int draw_xlim, draw_ylim;
130 static void Explode(struct state *st, int x, int y, int max, XColor color, int oflaser)
134 for (i=0;i<kMaxBooms;i++)
135 if (!st->boom[i].alive) {
146 if (max > kMaxRadius)
151 m->oflaser = oflaser;
154 static void launch (struct state *st, int xlim, int ylim, int src)
157 Missile *m = 0, *msrc;
158 for (i=0;i<kMaxMissiles;i++)
159 if (!st->missile[i].alive) {
167 m->startx = (random() % xlim);
171 m->jenis = random() % 360;
176 m->splits = random() % j;
177 if (m->splits < ylim * 0.08)
181 /* special if we're from another missile */
183 int dc = random() % (kNumCities - 1);
184 msrc = &st->missile[src];
185 if (dc == msrc->dcity)
190 if (m->starty > ylim * 0.4 || m->splits <= m->starty)
191 m->splits = 0; /* too far down already */
192 m->jenis = msrc->jenis;
195 m->dcity = random() % kNumCities;
196 m->endx = st->city[m->dcity].x + (random() % 20) - 10;
202 hsv_to_rgb (m->jenis, 1.0, 1.0,
203 &m->color.red, &m->color.green, &m->color.blue);
204 m->color.flags = DoRed | DoGreen | DoBlue;
205 if (!XAllocColor (st->dpy, st->cmap, &m->color)) {
206 m->color.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
207 m->color.red = m->color.green = m->color.blue = 0xFFFF;
213 #define kSpeedDiff 3.5
214 #define kMaxToGround 0.75
215 static int fire(struct state *st, int xlim, int ylim)
223 int choosy = 0, economic = 0, careful = 0;
224 int suitor[kMaxMissiles];
226 int ytargetmin = ylim * 0.75;
230 choosy = (random() % 100) < st->choosypersen;
231 economic = (random() % 100) < st->econpersen;
232 careful = (random() % 100) < st->carefulpersen;
234 /* count our cities */
235 for (i=0;i<kNumCities;i++)
236 livecity += st->city[i].alive;
238 return 1; /* no guns */
240 for (i=0;i<kMaxLasers;i++)
241 if (!st->laser[i].alive) {
248 /* if no missiles on target, no need to be choosy */
251 for (j=0;j<kMaxMissiles;j++) {
252 mis = &st->missile[j];
253 if (!mis->alive || (mis->y > ytargetmin))
255 if (st->city[mis->dcity].alive)
262 for (j=0;j<kMaxMissiles;j++) {
263 mis = &st->missile[j];
265 if (!mis->alive || (mis->y > ytargetmin))
267 if (choosy && (st->city[mis->dcity].alive == 0))
269 ey = mis->starty + ((float) (mis->endy - mis->starty)) * (mis->pos + kExpHelp + (1.0 - mis->pos) / kSpeedDiff);
270 if (ey > ylim * kMaxToGround)
271 continue; /* too far down */
276 /* count missiles that are on target and not being targeted */
277 if (choosy && economic)
278 for (j=0;j<kMaxMissiles;j++)
279 if (suitor[j] && st->missile[j].enemies == 0)
283 for (j=0;j<kMaxMissiles;j++) {
284 if (suitor[j] && cnt > 1)
285 if (st->missile[j].enemies > 0)
286 if (st->missile[j].enemies > 1 || untargeted == 0) {
290 /* who's closest? biggest threat */
291 if (suitor[j] && st->missile[j].y > deepest)
292 deepest = st->missile[j].y;
295 if (deepest > 0 && careful) {
296 /* only target deepest missile */
298 for (j=0;j<kMaxMissiles;j++)
299 if (suitor[j] && st->missile[j].y != deepest)
304 return 1; /* no targets available */
305 cnt = random() % cnt;
306 for (j=0;j<kMaxMissiles;j++)
309 mis = &st->missile[j];
315 return 1; /* shouldn't happen */
317 dcity = random() % livecity;
318 for (j=0;j<kNumCities;j++)
319 if (st->city[j].alive)
324 m->startx = st->city[dcity].x;
326 ex = mis->startx + ((float) (mis->endx - mis->startx)) * (mis->pos + kExpHelp + (1.0 - mis->pos) / kSpeedDiff);
327 ey = mis->starty + ((float) (mis->endy - mis->starty)) * (mis->pos + kExpHelp + (1.0 - mis->pos) / kSpeedDiff);
328 m->endx = ex + random() % 16 - 8 + (random() % st->aim) - st->aim / 2;
329 m->endy = ey + random() % 16 - 8 + (random() % st->aim) - st->aim / 2;
330 if (ey > ylim * kMaxToGround)
331 return 0; /* too far down */
342 dx = (m->endx - m->x);
343 dy = (m->endy - m->y);
344 m->velx = dx / 100.0;
345 m->vely = dy / 100.0;
347 /* m->lenMul = (kLaserLength * kLaserLength) / (m->velx * m->velx + m->vely * m->vely); */
348 m->lenMul = -(kLaserLength / m->vely);
351 m->color.blue = 0x0000;
352 m->color.green = 0xFFFF;
353 m->color.red = 0xFFFF;
354 m->color.flags = DoRed | DoGreen | DoBlue;
355 if (!XAllocColor (st->dpy, st->cmap, &m->color)) {
356 m->color.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
357 m->color.red = m->color.green = m->color.blue = 0xFFFF;
364 penetrate_init (Display *dpy, Window window)
366 struct state *st = (struct state *) calloc (1, sizeof(*st));
368 /*char *fontname = "-*-new century schoolbook-*-r-*-*-*-380-*-*-*-*-*-*"; */
369 char *fontname = "-*-courier-*-r-*-*-*-380-*-*-*-*-*-*";
371 XWindowAttributes xgwa;
376 XGetWindowAttributes (st->dpy, st->window, &xgwa);
377 st->cmap = xgwa.colormap;
380 st->nextBonus = kFirstBonus;
383 st->smart = get_boolean_resource(st->dpy, "smart","Boolean");
384 st->bgrowth = get_integer_resource (st->dpy, "bgrowth", "Integer");
385 st->lrate = get_integer_resource (st->dpy, "lrate", "Integer");
386 if (st->bgrowth < 0) st->bgrowth = 2;
387 if (st->lrate < 0) st->lrate = 2;
388 st->startlrate = st->lrate;
390 if (!fontname || !*fontname)
391 fprintf (stderr, "%s: no font specified.\n", progname);
392 st->font = XLoadQueryFont(st->dpy, fontname);
394 fprintf (stderr, "%s: could not load font %s.\n", progname, fontname);
396 if (!(st->scoreFont = XLoadQueryFont(st->dpy, "-*-times-*-r-*-*-*-180-*-*-*-*-*-*")))
397 fprintf(stderr, "%s: Can't load Times font.", progname);
399 for (i = 0; i < kMaxMissiles; i++)
400 st->missile[i].alive = 0;
402 for (i = 0; i < kMaxLasers; i++)
403 st->laser[i].alive = 0;
405 for (i = 0; i < kMaxBooms; i++)
406 st->boom[i].alive = 0;
408 for (i = 0; i < kNumCities; i++) {
409 City *m = &st->city[i];
411 m->color.red = m->color.green = m->color.blue = 0xFFFF;
412 m->color.blue = 0x1111; m->color.green = 0x8888;
413 m->color.flags = DoRed | DoGreen | DoBlue;
414 if (!XAllocColor (st->dpy, st->cmap, &m->color)) {
415 m->color.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
416 m->color.red = m->color.green = m->color.blue = 0xFFFF;
420 gcv.foreground = st->default_fg_pixel =
421 get_pixel_resource(st->dpy, st->cmap, "foreground", "Foreground");
422 gcv.font = st->scoreFont->fid;
423 st->draw_gc = XCreateGC(st->dpy, st->window, GCForeground | GCFont, &gcv);
424 gcv.font = st->font->fid;
425 st->level_gc = XCreateGC(st->dpy, st->window, GCForeground | GCFont, &gcv);
426 XSetForeground (st->dpy, st->level_gc, st->city[0].color.pixel);
427 gcv.foreground = get_pixel_resource(st->dpy, st->cmap, "background", "Background");
428 st->erase_gc = XCreateGC(st->dpy, st->window, GCForeground, &gcv);
431 jwxyz_XSetAntiAliasing (st->dpy, st->erase_gc, False);
432 jwxyz_XSetAntiAliasing (st->dpy, st->draw_gc, False);
436 /* make a gray color for score */
438 st->scoreColor.red = st->scoreColor.green = st->scoreColor.blue = 0xAAAA;
439 st->scoreColor.flags = DoRed | DoGreen | DoBlue;
440 if (!XAllocColor (st->dpy, st->cmap, &st->scoreColor)) {
441 st->scoreColor.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
442 st->scoreColor.red = st->scoreColor.green = st->scoreColor.blue = 0xFFFF;
446 XClearWindow(st->dpy, st->window);
450 static void DrawScore(struct state *st, int xlim, int ylim)
454 sprintf(buf, "%ld", st->score);
455 width = XTextWidth(st->scoreFont, buf, strlen(buf));
456 height = font_height(st->scoreFont);
457 XSetForeground (st->dpy, st->draw_gc, st->scoreColor.pixel);
458 XFillRectangle(st->dpy, st->window, st->erase_gc,
459 xlim - width - 6, ylim - height - 2, width + 6, height + 2);
460 XDrawString(st->dpy, st->window, st->draw_gc, xlim - width - 2, ylim - 2,
463 sprintf(buf, "%ld", st->highscore);
464 width = XTextWidth(st->scoreFont, buf, strlen(buf));
465 XFillRectangle(st->dpy, st->window, st->erase_gc,
466 4, ylim - height - 2, width + 4, height + 2);
467 XDrawString(st->dpy, st->window, st->draw_gc, 4, ylim - 2,
471 static void AddScore(struct state *st, int xlim, int ylim, long dif)
474 for (i=0;i<kNumCities;i++)
475 sumlive += st->city[i].alive;
477 return; /* no cities, not possible to score */
480 if (st->score > st->highscore)
481 st->highscore = st->score;
482 DrawScore(st, xlim, ylim);
485 static void DrawCity(struct state *st, int x, int y, XColor col)
487 XSetForeground (st->dpy, st->draw_gc, col.pixel);
488 XFillRectangle(st->dpy, st->window, st->draw_gc,
489 x - 30, y - 40, 60, 40);
490 XFillRectangle(st->dpy, st->window, st->draw_gc,
491 x - 20, y - 50, 10, 10);
492 XFillRectangle(st->dpy, st->window, st->draw_gc,
493 x + 10, y - 50, 10, 10);
496 static void DrawCities(struct state *st, int xlim, int ylim)
499 for (i = 0; i < kNumCities; i++) {
500 City *m = &st->city[i];
503 x = (i + 1) * (xlim / (kNumCities + 1));
506 DrawCity(st, x, ylim, m->color);
510 static void LoopMissiles(struct state *st, int xlim, int ylim)
513 for (i = 0; i < kMaxMissiles; i++) {
515 Missile *m = &st->missile[i];
520 m->pos += kMissileSpeed;
521 m->x = m->startx + ((float) (m->endx - m->startx)) * m->pos;
522 m->y = m->starty + ((float) (m->endy - m->starty)) * m->pos;
526 XSetLineAttributes(st->dpy, st->draw_gc, 4, 0,0,0);
527 XSetForeground (st->dpy, st->draw_gc, m->color.pixel);
528 XDrawLine(st->dpy, st->window, st->draw_gc,
529 old_x, old_y, m->x, m->y);
531 /* maybe split off a new missile? */
532 if (m->splits && (m->y > m->splits)) {
534 launch(st, xlim, ylim, i);
539 if (st->city[m->dcity].alive) {
540 st->city[m->dcity].alive = 0;
541 Explode(st, m->x, m->y, kBoomRad * 2, m->color, 0);
545 /* check hitting explosions */
546 for (j=0;j<kMaxBooms;j++) {
547 Boom *b = &st->boom[j];
551 int dx = abs(m->x - b->x);
552 int dy = abs(m->y - b->y);
554 if ((dx < r) && (dy < r))
555 if (dx * dx + dy * dy < r * r) {
557 max = b->max + st->bgrowth - kBoomRad;
558 AddScore(st, xlim, ylim, SCORE_MISSILE);
566 Explode(st, m->x, m->y, kBoomRad + max, m->color, 0);
567 XSetLineAttributes(st->dpy, st->erase_gc, 4, 0,0,0);
568 /* In a perfect world, we could simply erase a line from
569 (m->startx, m->starty) to (m->x, m->y). This is not a
573 my_pos = kMissileSpeed;
574 while (my_pos <= m->pos) {
575 m->x = m->startx + ((float) (m->endx - m->startx)) * my_pos;
576 m->y = m->starty + ((float) (m->endy - m->starty)) * my_pos;
577 XDrawLine(st->dpy, st->window, st->erase_gc, old_x, old_y, m->x, m->y);
580 my_pos += kMissileSpeed;
586 static void LoopLasers(struct state *st, int xlim, int ylim)
588 int i, j, miny = ylim * 0.8;
590 for (i = 0; i < kMaxLasers; i++) {
591 Laser *m = &st->laser[i];
596 XSetLineAttributes(st->dpy, st->erase_gc, 2, 0,0,0);
597 XDrawLine(st->dpy, st->window, st->erase_gc,
598 m->oldx2, m->oldy2, m->oldx, m->oldy);
606 x = m->fposx + (-m->velx * m->lenMul);
607 y = m->fposy + (-m->vely * m->lenMul);
612 XSetLineAttributes(st->dpy, st->draw_gc, 2, 0,0,0);
613 XSetForeground (st->dpy, st->draw_gc, m->color.pixel);
614 XDrawLine(st->dpy, st->window, st->draw_gc,
622 if (m->y < m->endy) {
626 /* check hitting explosions */
628 for (j=0;j<kMaxBooms;j++) {
629 Boom *b = &st->boom[j];
633 int dx = abs(m->x - b->x);
634 int dy = abs(m->y - b->y);
638 if ((dx < r) && (dy < r))
639 if (dx * dx + dy * dy < r * r) {
641 /* one less enemy on this missile -- it probably didn't make it */
642 if (st->missile[m->target].alive)
643 st->missile[m->target].enemies--;
650 XDrawLine(st->dpy, st->window, st->erase_gc,
652 Explode(st, m->x, m->y, kBoomRad, m->color, 1);
657 static void LoopBooms(struct state *st, int xlim, int ylim)
660 for (i = 0; i < kMaxBooms; i++) {
661 Boom *m = &st->boom[i];
668 if (m->rad >= m->max)
670 XSetLineAttributes(st->dpy, st->draw_gc, 1, 0,0,0);
671 XSetForeground (st->dpy, st->draw_gc, m->color.pixel);
672 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);
675 XSetLineAttributes(st->dpy, st->erase_gc, 1, 0,0,0);
676 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);
686 /* after they die, let's change a few things */
687 static void Improve(struct state *st)
692 return; /* no need, really */
694 if (st->level <= 2) st->aim -= 8;
695 if (st->level <= 5) st->aim -= 6;
698 st->carefulpersen += 6;
699 st->choosypersen += 4;
700 if (st->level <= 5) st->choosypersen += 3;
703 if (st->startlrate < kMinRate) {
704 if (st->lrate < st->startlrate)
705 st->lrate = st->startlrate;
708 if (st->lrate < kMinRate)
709 st->lrate = kMinRate;
711 if (st->level <= 5) st->econpersen += 3;
712 if (st->aim < 1) st->aim = 1;
713 if (st->choosypersen > 100) st->choosypersen = 100;
714 if (st->carefulpersen > 100) st->carefulpersen = 100;
715 if (st->econpersen > 100) st->econpersen = 100;
718 static void NewLevel(struct state *st, int xlim, int ylim)
721 int width, i, sumlive = 0;
725 if (st->level == 0) {
730 /* check for a free city */
731 if (st->score >= st->nextBonus) {
733 st->nextBonus += kFirstBonus * st->numBonus;
737 for (i=0;i<kNumCities;i++) {
739 st->city[i].alive = st->blive[i];
740 liv[i] = st->city[i].alive;
743 st->city[i].alive = 0;
746 /* print out screen */
747 XFillRectangle(st->dpy, st->window, st->erase_gc,
750 sprintf(buf, "Bonus Round Over");
752 if (sumlive || freecity)
753 sprintf(buf, "Level %d Cleared", st->level);
755 sprintf(buf, "GAME OVER");
758 width = XTextWidth(st->font, buf, strlen(buf));
759 XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - width / 2, ylim / 2 - font_height(st->font) / 2,
761 XSync(st->dpy, False);
766 if (sumlive || freecity) {
768 /* draw live cities */
769 XFillRectangle(st->dpy, st->window, st->erase_gc,
770 0, ylim - 100, xlim, 100);
772 sprintf(buf, "X %ld", st->level * 100L);
773 /* how much they get */
774 sumwidth = XTextWidth(st->font, buf, strlen(buf));
775 /* add width of city */
779 DrawCity(st, xlim / 2 - sumwidth / 2 + 30, ylim * 0.70, st->city[0].color);
780 XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - sumwidth / 2 + 40 + 60, ylim * 0.7, buf, strlen(buf));
781 for (i=0;i<kNumCities;i++) {
783 st->city[i].alive = 1;
784 AddScore(st, xlim, ylim, 100 * st->level);
785 DrawCities(st, xlim, ylim);
786 XSync(st->dpy, False);
798 for (i=0;i<kNumCities;i++)
799 st->city[i].alive = 1;
803 st->nextBonus = kFirstBonus;
805 DrawCities(st, xlim, ylim);
809 /* do free city part */
810 if (freecity && sumlive < 5) {
811 int ncnt = random() % (5 - sumlive) + 1;
812 for (i=0;i<kNumCities;i++)
813 if (!st->city[i].alive)
815 st->city[i].alive = 1;
816 strcpy(buf, "Bonus City");
817 width = XTextWidth(st->font, buf, strlen(buf));
818 XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - width / 2, ylim / 4, buf, strlen(buf));
819 DrawCities(st, xlim, ylim);
820 XSync(st->dpy, False);
824 XFillRectangle(st->dpy, st->window, st->erase_gc,
825 0, 0, xlim, ylim - 100);
829 if (st->level == 1) {
830 st->nextBonus = kFirstBonus;
833 if (st->level > 3 && (st->level % 5 == 1)) {
836 DrawCities(st, xlim, ylim);
841 st->levMissiles = 20 + st->level * 10;
843 for (i=0;i<kNumCities;i++)
844 st->blive[i] = st->city[i].alive;
845 sprintf(buf, "Bonus Round");
846 width = XTextWidth(st->font, buf, strlen(buf));
847 XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - width / 2, ylim / 2 - font_height(st->font) / 2, buf, strlen(buf));
848 XSync(st->dpy, False);
850 XFillRectangle(st->dpy, st->window, st->erase_gc,
851 0, 0, xlim, ylim - 100);
858 st->levMissiles = 5 + st->level * 3;
860 st->levMissiles += st->level * 5;
861 /* levMissiles = 2; */
862 st->levFreq = 120 - st->level * 5;
863 if (st->levFreq < 30)
873 penetrate_draw (Display *dpy, Window window, void *closure)
875 struct state *st = (struct state *) closure;
876 XWindowAttributes xgwa;
881 DrawCities(st, st->draw_xlim, st->draw_ylim);
884 XGetWindowAttributes(st->dpy, st->window, &xgwa);
885 st->draw_xlim = xgwa.width;
886 st->draw_ylim = xgwa.height;
888 /* see if just started */
891 st->choosypersen = st->econpersen = st->carefulpersen = 100;
892 st->lrate = kMinRate; st->aim = 1;
894 NewLevel(st, st->draw_xlim, st->draw_ylim);
895 DrawScore(st, st->draw_xlim, st->draw_ylim);
900 if (st->levMissiles == 0) {
901 /* see if anything's still on the screen, to know when to end level */
903 for (i=0;i<kMaxMissiles;i++)
904 if (st->missile[i].alive)
906 for (i=0;i<kMaxBooms;i++)
907 if (st->boom[i].alive)
909 for (i=0;i<kMaxLasers;i++)
910 if (st->laser[i].alive)
912 /* okay, nothing's alive, start end of level countdown */
913 usleep(kLevelPause*1000000);
914 NewLevel(st, st->draw_xlim, st->draw_ylim);
918 else if ((random() % st->levFreq) == 0) {
919 launch(st, st->draw_xlim, st->draw_ylim, -1);
923 if (st->loop - st->lastLaser >= st->lrate) {
924 if (fire(st, st->draw_xlim, st->draw_ylim))
925 st->lastLaser = st->loop;
928 if ((st->loop & 7) == 0)
931 LoopMissiles(st, st->draw_xlim, st->draw_ylim);
932 LoopLasers(st, st->draw_xlim, st->draw_ylim);
933 LoopBooms(st, st->draw_xlim, st->draw_ylim);
940 penetrate_reshape (Display *dpy, Window window, void *closure,
941 unsigned int w, unsigned int h)
946 penetrate_event (Display *dpy, Window window, void *closure, XEvent *event)
952 penetrate_free (Display *dpy, Window window, void *closure)
954 struct state *st = (struct state *) closure;
959 static const char *penetrate_defaults [] = {
960 ".background: black",
961 ".foreground: white",
965 "*geometry: 800x500",
969 static XrmOptionDescRec penetrate_options [] = {
970 { "-bgrowth", ".bgrowth", XrmoptionSepArg, 0 },
971 { "-lrate", ".lrate", XrmoptionSepArg, 0 },
972 {"-smart", ".smart", XrmoptionNoArg, "True" },
976 XSCREENSAVER_MODULE ("Penetrate", penetrate)