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)
182 /* special if we're from another missile */
184 int dc = random() % (kNumCities - 1);
185 msrc = &st->missile[src];
186 if (dc == msrc->dcity)
191 if (m->starty > ylim * 0.4 || m->splits <= m->starty)
192 m->splits = 0; /* too far down already */
193 m->jenis = msrc->jenis;
196 m->dcity = random() % kNumCities;
197 m->endx = st->city[m->dcity].x + (random() % 20) - 10;
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;
214 #define kSpeedDiff 3.5
215 #define kMaxToGround 0.75
216 static int fire(struct state *st, int xlim, int ylim)
224 int choosy = 0, economic = 0, careful = 0;
225 int suitor[kMaxMissiles];
227 int ytargetmin = ylim * 0.75;
231 choosy = (random() % 100) < st->choosypersen;
232 economic = (random() % 100) < st->econpersen;
233 careful = (random() % 100) < st->carefulpersen;
235 /* count our cities */
236 for (i=0;i<kNumCities;i++)
237 livecity += st->city[i].alive;
239 return 1; /* no guns */
241 for (i=0;i<kMaxLasers;i++)
242 if (!st->laser[i].alive) {
249 /* if no missiles on target, no need to be choosy */
252 for (j=0;j<kMaxMissiles;j++) {
253 mis = &st->missile[j];
254 if (!mis->alive || (mis->y > ytargetmin))
256 if (st->city[mis->dcity].alive)
263 for (j=0;j<kMaxMissiles;j++) {
264 mis = &st->missile[j];
266 if (!mis->alive || (mis->y > ytargetmin))
268 if (choosy && (st->city[mis->dcity].alive == 0))
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 */
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)
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) {
291 /* who's closest? biggest threat */
292 if (suitor[j] && st->missile[j].y > deepest)
293 deepest = st->missile[j].y;
296 if (deepest > 0 && careful) {
297 /* only target deepest missile */
299 for (j=0;j<kMaxMissiles;j++)
300 if (suitor[j] && st->missile[j].y != deepest)
305 return 1; /* no targets available */
306 cnt = random() % cnt;
307 for (j=0;j<kMaxMissiles;j++)
310 mis = &st->missile[j];
316 return 1; /* shouldn't happen */
318 dcity = random() % livecity;
319 for (j=0;j<kNumCities;j++)
320 if (st->city[j].alive)
325 m->startx = st->city[dcity].x;
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 */
343 dx = (m->endx - m->x);
344 dy = (m->endy - m->y);
345 m->velx = dx / 100.0;
346 m->vely = dy / 100.0;
348 /* m->lenMul = (kLaserLength * kLaserLength) / (m->velx * m->velx + m->vely * m->vely); */
349 m->lenMul = -(kLaserLength / m->vely);
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;
365 penetrate_init (Display *dpy, Window window)
367 struct state *st = (struct state *) calloc (1, sizeof(*st));
369 const char *levelfont = "-*-courier-*-r-*-*-*-380-*-*-*-*-*-*";
370 const char *scorefont = "-*-helvetica-*-r-*-*-*-180-*-*-*-*-*-*";
372 XWindowAttributes xgwa;
377 XGetWindowAttributes (st->dpy, st->window, &xgwa);
378 st->cmap = xgwa.colormap;
381 st->nextBonus = kFirstBonus;
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;
391 st->font = XLoadQueryFont(st->dpy, levelfont);
393 fprintf (stderr, "%s: could not load font %s.\n", progname, levelfont);
394 st->font = XLoadQueryFont(st->dpy, scorefont);
396 st->font = XLoadQueryFont(st->dpy, "fixed");
397 if (! st->font) abort();
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);
405 st->scoreFont = XLoadQueryFont(st->dpy, "fixed");
406 if (! st->scoreFont) abort();
409 for (i = 0; i < kMaxMissiles; i++)
410 st->missile[i].alive = 0;
412 for (i = 0; i < kMaxLasers; i++)
413 st->laser[i].alive = 0;
415 for (i = 0; i < kMaxBooms; i++)
416 st->boom[i].alive = 0;
418 for (i = 0; i < kNumCities; i++) {
419 City *m = &st->city[i];
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;
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);
441 jwxyz_XSetAntiAliasing (st->dpy, st->erase_gc, False);
442 jwxyz_XSetAntiAliasing (st->dpy, st->draw_gc, False);
446 /* make a gray color for score */
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;
456 XClearWindow(st->dpy, st->window);
460 static void DrawScore(struct state *st, int xlim, int ylim)
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,
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,
481 static void AddScore(struct state *st, int xlim, int ylim, long dif)
484 for (i=0;i<kNumCities;i++)
485 sumlive += st->city[i].alive;
487 return; /* no cities, not possible to score */
490 if (st->score > st->highscore)
491 st->highscore = st->score;
492 DrawScore(st, xlim, ylim);
495 static void DrawCity(struct state *st, int x, int y, XColor col)
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);
506 static void DrawCities(struct state *st, int xlim, int ylim)
509 for (i = 0; i < kNumCities; i++) {
510 City *m = &st->city[i];
513 x = (i + 1) * (xlim / (kNumCities + 1));
516 DrawCity(st, x, ylim, m->color);
520 static void LoopMissiles(struct state *st, int xlim, int ylim)
523 for (i = 0; i < kMaxMissiles; i++) {
525 Missile *m = &st->missile[i];
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;
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);
541 /* maybe split off a new missile? */
542 if (m->splits && (m->y > m->splits)) {
544 launch(st, xlim, ylim, i);
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);
555 /* check hitting explosions */
556 for (j=0;j<kMaxBooms;j++) {
557 Boom *b = &st->boom[j];
561 int dx = abs(m->x - b->x);
562 int dy = abs(m->y - b->y);
564 if ((dx < r) && (dy < r))
565 if (dx * dx + dy * dy < r * r) {
567 max = b->max + st->bgrowth - kBoomRad;
568 AddScore(st, xlim, ylim, SCORE_MISSILE);
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
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);
590 my_pos += kMissileSpeed;
596 static void LoopLasers(struct state *st, int xlim, int ylim)
598 int i, j, miny = ylim * 0.8;
600 for (i = 0; i < kMaxLasers; i++) {
601 Laser *m = &st->laser[i];
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);
616 x = m->fposx + (-m->velx * m->lenMul);
617 y = m->fposy + (-m->vely * m->lenMul);
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,
632 if (m->y < m->endy) {
636 /* check hitting explosions */
638 for (j=0;j<kMaxBooms;j++) {
639 Boom *b = &st->boom[j];
643 int dx = abs(m->x - b->x);
644 int dy = abs(m->y - b->y);
648 if ((dx < r) && (dy < r))
649 if (dx * dx + dy * dy < r * r) {
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--;
660 XDrawLine(st->dpy, st->window, st->erase_gc,
662 Explode(st, m->x, m->y, kBoomRad, m->color, 1);
667 static void LoopBooms(struct state *st, int xlim, int ylim)
670 for (i = 0; i < kMaxBooms; i++) {
671 Boom *m = &st->boom[i];
678 if (m->rad >= m->max)
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);
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);
696 /* after they die, let's change a few things */
697 static void Improve(struct state *st)
702 return; /* no need, really */
704 if (st->level <= 2) st->aim -= 8;
705 if (st->level <= 5) st->aim -= 6;
708 st->carefulpersen += 6;
709 st->choosypersen += 4;
710 if (st->level <= 5) st->choosypersen += 3;
713 if (st->startlrate < kMinRate) {
714 if (st->lrate < st->startlrate)
715 st->lrate = st->startlrate;
718 if (st->lrate < kMinRate)
719 st->lrate = kMinRate;
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;
728 static void NewLevel(struct state *st, int xlim, int ylim)
731 int width, i, sumlive = 0;
735 if (st->level == 0) {
740 /* check for a free city */
741 if (st->score >= st->nextBonus) {
743 st->nextBonus += kFirstBonus * st->numBonus;
747 for (i=0;i<kNumCities;i++) {
749 st->city[i].alive = st->blive[i];
750 liv[i] = st->city[i].alive;
753 st->city[i].alive = 0;
756 /* print out screen */
757 XFillRectangle(st->dpy, st->window, st->erase_gc,
760 sprintf(buf, "Bonus Round Over");
762 if (sumlive || freecity)
763 sprintf(buf, "Level %d Cleared", st->level);
765 sprintf(buf, "GAME OVER");
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,
771 XSync(st->dpy, False);
776 if (sumlive || freecity) {
778 /* draw live cities */
779 XFillRectangle(st->dpy, st->window, st->erase_gc,
780 0, ylim - 100, xlim, 100);
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 */
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++) {
793 st->city[i].alive = 1;
794 AddScore(st, xlim, ylim, 100 * st->level);
795 DrawCities(st, xlim, ylim);
796 XSync(st->dpy, False);
808 for (i=0;i<kNumCities;i++)
809 st->city[i].alive = 1;
813 st->nextBonus = kFirstBonus;
815 DrawCities(st, xlim, ylim);
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)
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);
834 XFillRectangle(st->dpy, st->window, st->erase_gc,
835 0, 0, xlim, ylim - 100);
839 if (st->level == 1) {
840 st->nextBonus = kFirstBonus;
843 if (st->level > 3 && (st->level % 5 == 1)) {
846 DrawCities(st, xlim, ylim);
851 st->levMissiles = 20 + st->level * 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);
860 XFillRectangle(st->dpy, st->window, st->erase_gc,
861 0, 0, xlim, ylim - 100);
868 st->levMissiles = 5 + st->level * 3;
870 st->levMissiles += st->level * 5;
871 /* levMissiles = 2; */
872 st->levFreq = 120 - st->level * 5;
873 if (st->levFreq < 30)
883 penetrate_draw (Display *dpy, Window window, void *closure)
885 struct state *st = (struct state *) closure;
886 XWindowAttributes xgwa;
891 DrawCities(st, st->draw_xlim, st->draw_ylim);
894 XGetWindowAttributes(st->dpy, st->window, &xgwa);
895 st->draw_xlim = xgwa.width;
896 st->draw_ylim = xgwa.height;
898 /* see if just started */
901 st->choosypersen = st->econpersen = st->carefulpersen = 100;
902 st->lrate = kMinRate; st->aim = 1;
904 NewLevel(st, st->draw_xlim, st->draw_ylim);
905 DrawScore(st, st->draw_xlim, st->draw_ylim);
910 if (st->levMissiles == 0) {
911 /* see if anything's still on the screen, to know when to end level */
913 for (i=0;i<kMaxMissiles;i++)
914 if (st->missile[i].alive)
916 for (i=0;i<kMaxBooms;i++)
917 if (st->boom[i].alive)
919 for (i=0;i<kMaxLasers;i++)
920 if (st->laser[i].alive)
922 /* okay, nothing's alive, start end of level countdown */
923 usleep(kLevelPause*1000000);
924 NewLevel(st, st->draw_xlim, st->draw_ylim);
928 else if ((random() % st->levFreq) == 0) {
929 launch(st, st->draw_xlim, st->draw_ylim, -1);
933 if (st->loop - st->lastLaser >= st->lrate) {
934 if (fire(st, st->draw_xlim, st->draw_ylim))
935 st->lastLaser = st->loop;
938 if ((st->loop & 7) == 0)
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);
950 penetrate_reshape (Display *dpy, Window window, void *closure,
951 unsigned int w, unsigned int h)
953 XClearWindow (dpy, window);
957 penetrate_event (Display *dpy, Window window, void *closure, XEvent *event)
963 penetrate_free (Display *dpy, Window window, void *closure)
965 struct state *st = (struct state *) closure;
970 static const char *penetrate_defaults [] = {
972 ".background: black",
973 ".foreground: white",
982 static XrmOptionDescRec penetrate_options [] = {
983 { "-bgrowth", ".bgrowth", XrmoptionSepArg, 0 },
984 { "-lrate", ".lrate", XrmoptionSepArg, 0 },
985 {"-smart", ".smart", XrmoptionNoArg, "True" },
989 XSCREENSAVER_MODULE ("Penetrate", penetrate)