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 = load_font_retry(st->dpy, levelfont);
392 if (!st->font) abort();
394 st->scoreFont = load_font_retry(st->dpy, scorefont);
395 if (!st->scoreFont) abort();
397 for (i = 0; i < kMaxMissiles; i++)
398 st->missile[i].alive = 0;
400 for (i = 0; i < kMaxLasers; i++)
401 st->laser[i].alive = 0;
403 for (i = 0; i < kMaxBooms; i++)
404 st->boom[i].alive = 0;
406 for (i = 0; i < kNumCities; i++) {
407 City *m = &st->city[i];
409 m->color.red = m->color.green = m->color.blue = 0xFFFF;
410 m->color.blue = 0x1111; m->color.green = 0x8888;
411 m->color.flags = DoRed | DoGreen | DoBlue;
412 if (!XAllocColor (st->dpy, st->cmap, &m->color)) {
413 m->color.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
414 m->color.red = m->color.green = m->color.blue = 0xFFFF;
418 gcv.foreground = st->default_fg_pixel =
419 get_pixel_resource(st->dpy, st->cmap, "foreground", "Foreground");
420 gcv.font = st->scoreFont->fid;
421 st->draw_gc = XCreateGC(st->dpy, st->window, GCForeground | GCFont, &gcv);
422 gcv.font = st->font->fid;
423 st->level_gc = XCreateGC(st->dpy, st->window, GCForeground | GCFont, &gcv);
424 XSetForeground (st->dpy, st->level_gc, st->city[0].color.pixel);
425 gcv.foreground = get_pixel_resource(st->dpy, st->cmap, "background", "Background");
426 st->erase_gc = XCreateGC(st->dpy, st->window, GCForeground, &gcv);
429 jwxyz_XSetAntiAliasing (st->dpy, st->erase_gc, False);
430 jwxyz_XSetAntiAliasing (st->dpy, st->draw_gc, False);
434 /* make a gray color for score */
436 st->scoreColor.red = st->scoreColor.green = st->scoreColor.blue = 0xAAAA;
437 st->scoreColor.flags = DoRed | DoGreen | DoBlue;
438 if (!XAllocColor (st->dpy, st->cmap, &st->scoreColor)) {
439 st->scoreColor.pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
440 st->scoreColor.red = st->scoreColor.green = st->scoreColor.blue = 0xFFFF;
444 XClearWindow(st->dpy, st->window);
448 static void DrawScore(struct state *st, int xlim, int ylim)
452 sprintf(buf, "%ld", st->score);
453 width = XTextWidth(st->scoreFont, buf, strlen(buf));
454 height = font_height(st->scoreFont);
455 XSetForeground (st->dpy, st->draw_gc, st->scoreColor.pixel);
456 XFillRectangle(st->dpy, st->window, st->erase_gc,
457 xlim - width - 6, ylim - height - 2, width + 6, height + 2);
458 XDrawString(st->dpy, st->window, st->draw_gc, xlim - width - 2, ylim - 2,
461 sprintf(buf, "%ld", st->highscore);
462 width = XTextWidth(st->scoreFont, buf, strlen(buf));
463 XFillRectangle(st->dpy, st->window, st->erase_gc,
464 4, ylim - height - 2, width + 4, height + 2);
465 XDrawString(st->dpy, st->window, st->draw_gc, 4, ylim - 2,
469 static void AddScore(struct state *st, int xlim, int ylim, long dif)
472 for (i=0;i<kNumCities;i++)
473 sumlive += st->city[i].alive;
475 return; /* no cities, not possible to score */
478 if (st->score > st->highscore)
479 st->highscore = st->score;
480 DrawScore(st, xlim, ylim);
483 static void DrawCity(struct state *st, int x, int y, XColor col)
485 XSetForeground (st->dpy, st->draw_gc, col.pixel);
486 XFillRectangle(st->dpy, st->window, st->draw_gc,
487 x - 30, y - 40, 60, 40);
488 XFillRectangle(st->dpy, st->window, st->draw_gc,
489 x - 20, y - 50, 10, 10);
490 XFillRectangle(st->dpy, st->window, st->draw_gc,
491 x + 10, y - 50, 10, 10);
494 static void DrawCities(struct state *st, int xlim, int ylim)
497 for (i = 0; i < kNumCities; i++) {
498 City *m = &st->city[i];
501 x = (i + 1) * (xlim / (kNumCities + 1));
504 DrawCity(st, x, ylim, m->color);
508 static void LoopMissiles(struct state *st, int xlim, int ylim)
511 for (i = 0; i < kMaxMissiles; i++) {
513 Missile *m = &st->missile[i];
518 m->pos += kMissileSpeed;
519 m->x = m->startx + ((float) (m->endx - m->startx)) * m->pos;
520 m->y = m->starty + ((float) (m->endy - m->starty)) * m->pos;
524 XSetLineAttributes(st->dpy, st->draw_gc, 4, 0,0,0);
525 XSetForeground (st->dpy, st->draw_gc, m->color.pixel);
526 XDrawLine(st->dpy, st->window, st->draw_gc,
527 old_x, old_y, m->x, m->y);
529 /* maybe split off a new missile? */
530 if (m->splits && (m->y > m->splits)) {
532 launch(st, xlim, ylim, i);
537 if (st->city[m->dcity].alive) {
538 st->city[m->dcity].alive = 0;
539 Explode(st, m->x, m->y, kBoomRad * 2, m->color, 0);
543 /* check hitting explosions */
544 for (j=0;j<kMaxBooms;j++) {
545 Boom *b = &st->boom[j];
549 int dx = abs(m->x - b->x);
550 int dy = abs(m->y - b->y);
552 if ((dx < r) && (dy < r))
553 if (dx * dx + dy * dy < r * r) {
555 max = b->max + st->bgrowth - kBoomRad;
556 AddScore(st, xlim, ylim, SCORE_MISSILE);
564 Explode(st, m->x, m->y, kBoomRad + max, m->color, 0);
565 XSetLineAttributes(st->dpy, st->erase_gc, 4, 0,0,0);
566 /* In a perfect world, we could simply erase a line from
567 (m->startx, m->starty) to (m->x, m->y). This is not a
571 my_pos = kMissileSpeed;
572 while (my_pos <= m->pos) {
573 m->x = m->startx + ((float) (m->endx - m->startx)) * my_pos;
574 m->y = m->starty + ((float) (m->endy - m->starty)) * my_pos;
575 XDrawLine(st->dpy, st->window, st->erase_gc, old_x, old_y, m->x, m->y);
578 my_pos += kMissileSpeed;
584 static void LoopLasers(struct state *st, int xlim, int ylim)
586 int i, j, miny = ylim * 0.8;
588 for (i = 0; i < kMaxLasers; i++) {
589 Laser *m = &st->laser[i];
594 XSetLineAttributes(st->dpy, st->erase_gc, 2, 0,0,0);
595 XDrawLine(st->dpy, st->window, st->erase_gc,
596 m->oldx2, m->oldy2, m->oldx, m->oldy);
604 x = m->fposx + (-m->velx * m->lenMul);
605 y = m->fposy + (-m->vely * m->lenMul);
610 XSetLineAttributes(st->dpy, st->draw_gc, 2, 0,0,0);
611 XSetForeground (st->dpy, st->draw_gc, m->color.pixel);
612 XDrawLine(st->dpy, st->window, st->draw_gc,
620 if (m->y < m->endy) {
624 /* check hitting explosions */
626 for (j=0;j<kMaxBooms;j++) {
627 Boom *b = &st->boom[j];
631 int dx = abs(m->x - b->x);
632 int dy = abs(m->y - b->y);
636 if ((dx < r) && (dy < r))
637 if (dx * dx + dy * dy < r * r) {
639 /* one less enemy on this missile -- it probably didn't make it */
640 if (st->missile[m->target].alive)
641 st->missile[m->target].enemies--;
648 XDrawLine(st->dpy, st->window, st->erase_gc,
650 Explode(st, m->x, m->y, kBoomRad, m->color, 1);
655 static void LoopBooms(struct state *st, int xlim, int ylim)
658 for (i = 0; i < kMaxBooms; i++) {
659 Boom *m = &st->boom[i];
666 if (m->rad >= m->max)
668 XSetLineAttributes(st->dpy, st->draw_gc, 1, 0,0,0);
669 XSetForeground (st->dpy, st->draw_gc, m->color.pixel);
670 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);
673 XSetLineAttributes(st->dpy, st->erase_gc, 1, 0,0,0);
674 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);
684 /* after they die, let's change a few things */
685 static void Improve(struct state *st)
690 return; /* no need, really */
692 if (st->level <= 2) st->aim -= 8;
693 if (st->level <= 5) st->aim -= 6;
696 st->carefulpersen += 6;
697 st->choosypersen += 4;
698 if (st->level <= 5) st->choosypersen += 3;
701 if (st->startlrate < kMinRate) {
702 if (st->lrate < st->startlrate)
703 st->lrate = st->startlrate;
706 if (st->lrate < kMinRate)
707 st->lrate = kMinRate;
709 if (st->level <= 5) st->econpersen += 3;
710 if (st->aim < 1) st->aim = 1;
711 if (st->choosypersen > 100) st->choosypersen = 100;
712 if (st->carefulpersen > 100) st->carefulpersen = 100;
713 if (st->econpersen > 100) st->econpersen = 100;
716 static void NewLevel(struct state *st, int xlim, int ylim)
719 int width, i, sumlive = 0;
723 if (st->level == 0) {
728 /* check for a free city */
729 if (st->score >= st->nextBonus) {
731 st->nextBonus += kFirstBonus * st->numBonus;
735 for (i=0;i<kNumCities;i++) {
737 st->city[i].alive = st->blive[i];
738 liv[i] = st->city[i].alive;
741 st->city[i].alive = 0;
744 /* print out screen */
745 XFillRectangle(st->dpy, st->window, st->erase_gc,
748 sprintf(buf, "Bonus Round Over");
750 if (sumlive || freecity)
751 sprintf(buf, "Level %d Cleared", st->level);
753 sprintf(buf, "GAME OVER");
756 width = XTextWidth(st->font, buf, strlen(buf));
757 XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - width / 2, ylim / 2 - font_height(st->font) / 2,
759 XSync(st->dpy, False);
764 if (sumlive || freecity) {
766 /* draw live cities */
767 XFillRectangle(st->dpy, st->window, st->erase_gc,
768 0, ylim - 100, xlim, 100);
770 sprintf(buf, "X %ld", st->level * 100L);
771 /* how much they get */
772 sumwidth = XTextWidth(st->font, buf, strlen(buf));
773 /* add width of city */
777 DrawCity(st, xlim / 2 - sumwidth / 2 + 30, ylim * 0.70, st->city[0].color);
778 XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - sumwidth / 2 + 40 + 60, ylim * 0.7, buf, strlen(buf));
779 for (i=0;i<kNumCities;i++) {
781 st->city[i].alive = 1;
782 AddScore(st, xlim, ylim, 100 * st->level);
783 DrawCities(st, xlim, ylim);
784 XSync(st->dpy, False);
796 for (i=0;i<kNumCities;i++)
797 st->city[i].alive = 1;
801 st->nextBonus = kFirstBonus;
803 DrawCities(st, xlim, ylim);
807 /* do free city part */
808 if (freecity && sumlive < 5) {
809 int ncnt = random() % (5 - sumlive) + 1;
810 for (i=0;i<kNumCities;i++)
811 if (!st->city[i].alive)
813 st->city[i].alive = 1;
814 strcpy(buf, "Bonus City");
815 width = XTextWidth(st->font, buf, strlen(buf));
816 XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - width / 2, ylim / 4, buf, strlen(buf));
817 DrawCities(st, xlim, ylim);
818 XSync(st->dpy, False);
822 XFillRectangle(st->dpy, st->window, st->erase_gc,
823 0, 0, xlim, ylim - 100);
827 if (st->level == 1) {
828 st->nextBonus = kFirstBonus;
831 if (st->level > 3 && (st->level % 5 == 1)) {
834 DrawCities(st, xlim, ylim);
839 st->levMissiles = 20 + st->level * 10;
841 for (i=0;i<kNumCities;i++)
842 st->blive[i] = st->city[i].alive;
843 sprintf(buf, "Bonus Round");
844 width = XTextWidth(st->font, buf, strlen(buf));
845 XDrawString(st->dpy, st->window, st->level_gc, xlim / 2 - width / 2, ylim / 2 - font_height(st->font) / 2, buf, strlen(buf));
846 XSync(st->dpy, False);
848 XFillRectangle(st->dpy, st->window, st->erase_gc,
849 0, 0, xlim, ylim - 100);
856 st->levMissiles = 5 + st->level * 3;
858 st->levMissiles += st->level * 5;
859 /* levMissiles = 2; */
860 st->levFreq = 120 - st->level * 5;
861 if (st->levFreq < 30)
871 penetrate_draw (Display *dpy, Window window, void *closure)
873 struct state *st = (struct state *) closure;
874 XWindowAttributes xgwa;
879 DrawCities(st, st->draw_xlim, st->draw_ylim);
882 XGetWindowAttributes(st->dpy, st->window, &xgwa);
883 st->draw_xlim = xgwa.width;
884 st->draw_ylim = xgwa.height;
886 /* see if just started */
889 st->choosypersen = st->econpersen = st->carefulpersen = 100;
890 st->lrate = kMinRate; st->aim = 1;
892 NewLevel(st, st->draw_xlim, st->draw_ylim);
893 DrawScore(st, st->draw_xlim, st->draw_ylim);
898 if (st->levMissiles == 0) {
899 /* see if anything's still on the screen, to know when to end level */
901 for (i=0;i<kMaxMissiles;i++)
902 if (st->missile[i].alive)
904 for (i=0;i<kMaxBooms;i++)
905 if (st->boom[i].alive)
907 for (i=0;i<kMaxLasers;i++)
908 if (st->laser[i].alive)
910 /* okay, nothing's alive, start end of level countdown */
911 usleep(kLevelPause*1000000);
912 NewLevel(st, st->draw_xlim, st->draw_ylim);
916 else if ((random() % st->levFreq) == 0) {
917 launch(st, st->draw_xlim, st->draw_ylim, -1);
921 if (st->loop - st->lastLaser >= st->lrate) {
922 if (fire(st, st->draw_xlim, st->draw_ylim))
923 st->lastLaser = st->loop;
926 if ((st->loop & 7) == 0)
929 LoopMissiles(st, st->draw_xlim, st->draw_ylim);
930 LoopLasers(st, st->draw_xlim, st->draw_ylim);
931 LoopBooms(st, st->draw_xlim, st->draw_ylim);
938 penetrate_reshape (Display *dpy, Window window, void *closure,
939 unsigned int w, unsigned int h)
941 XClearWindow (dpy, window);
945 penetrate_event (Display *dpy, Window window, void *closure, XEvent *event)
951 penetrate_free (Display *dpy, Window window, void *closure)
953 struct state *st = (struct state *) closure;
958 static const char *penetrate_defaults [] = {
960 ".background: black",
961 ".foreground: white",
970 static XrmOptionDescRec penetrate_options [] = {
971 { "-bgrowth", ".bgrowth", XrmoptionSepArg, 0 },
972 { "-lrate", ".lrate", XrmoptionSepArg, 0 },
973 {"-smart", ".smart", XrmoptionNoArg, "True" },
977 XSCREENSAVER_MODULE ("Penetrate", penetrate)