ftp://updates.redhat.com/enterprise/2.1AS/en/os/SRPMS/xscreensaver-3.33-4.rhel21...
[xscreensaver] / hacks / speedmine.c
1 /* -*- Mode: C; c-basic-offset: 4; tab-width: 4 -*-
2  * speedmine, Copyright (C) 2001 Conrad Parker <conrad@deephackmode.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 /*
14  * Written mostly over the Easter holiday, 2001. Psychedelic option due to
15  * a night at Home nightclub, Sydney. Three all-nighters of solid partying
16  * were involved in the week this hack was written.
17  *
18  * Happy Birthday to WierdArms (17 April) and Pat (18 April)
19  */
20
21 /*
22  * Hacking notes
23  *
24  * This program generates a rectangular terrain grid and maps this onto
25  * a semi-circular tunnel. The terrain has length TERRAIN_LENGTH, which
26  * corresponds to length along the tunnel, and breadth TERRAIN_BREADTH,
27  * which corresponds to circumference around the tunnel. For each frame,
28  * the tunnel is perspective mapped onto a set of X and Y screen values.
29  *
30  * Throughout this code the following temporary variable names are used:
31  *
32  *                      i       iterates along the tunnel in the direction of travel
33  *                      j       iterates around the tunnel clockwise
34  *                      t       iterates along the length of the perspective mapped values
35  *                              from the furthest to the nearest
36  *
37  * Thus, the buffers are used with these iterators:
38  *
39  *                      terrain[i][j]                                   terrain map
40  *                      worldx[i][j], worldy[i][j]              world coordinates (after wrapping)
41  *                      {x,y,z}curvature[i]                             tunnel curvature
42  *                      wideness[i]                                             tunnel wideness
43  *                      bonuses[i]                                              bonus values
44  *
45  *                      xvals[t][j], yvals[t][j]                screen coordinates
46  *                      {min,max}{x,y}[t]                               bounding boxes of screen coords
47  */
48
49 /* Define or undefine NDEBUG to turn assert and abort debugging off or on */
50 #define NDEBUG
51 #include <assert.h>
52
53 #include <math.h>
54
55 #include "screenhack.h"
56 #include "erase.h"
57
58 #define MIN(a,b) ((a)<(b)?(a):(b))
59 #define MAX(a,b) ((a)>(b)?(a):(b))
60
61 #define RAND(r) (int)(((r)>0)?(random() % (long)(r)): -(random() % (long)(-r)))
62
63 #define SIGN3(a) ((a)>0?1:((a)<0?-1:0))
64
65 #define MODULO(a,b) while ((a)<0) (a)+=(b); (a) %= (b);
66
67 static Display * display;
68 static Pixmap dbuf, stars_mask;
69 static Colormap cmap;
70 static unsigned int default_fg_pixel;
71 static GC draw_gc, erase_gc, tunnelend_gc, stars_gc, stars_erase_gc;
72
73 /* No. of shades of each color (ground, walls, bonuses) */
74 #define MAX_COLORS 32
75 static int ncolors, nr_ground_colors, nr_wall_colors, nr_bonus_colors;
76 static XColor ground_colors[MAX_COLORS], wall_colors[MAX_COLORS];
77 static XColor bonus_colors[MAX_COLORS];
78 static GC ground_gcs[MAX_COLORS], wall_gcs[MAX_COLORS], bonus_gcs[MAX_COLORS];
79
80 static int be_wormy;
81
82 static int width, height;
83 static int delay;
84
85 static int smoothness;
86 static int verbose_flag;
87 static int wire_flag;
88 static int terrain_flag;
89 static int widening_flag;
90 static int bumps_flag;
91 static int bonuses_flag;
92 static int crosshair_flag;
93 static int psychedelic_flag;
94
95 #ifdef NDEBUG
96 #define DEBUG_FLAG 0
97 #else
98 #define DEBUG_FLAG 1
99 #endif
100
101 static double maxspeed;
102
103 static double thrust, gravity;
104
105 static double vertigo;
106 static double curviness;
107 static double twistiness;
108
109 static double pos=0.0;
110 static double speed=-1.1;
111 static double accel=0.00000001;
112 static double step=0.0;
113
114
115 #define FORWARDS 1
116 #define BACKWARDS -1
117 static int direction = FORWARDS;
118
119 static int pindex=0, nearest=0;
120 static int flipped_at=0;
121 static int xoffset=0, yoffset=0;
122
123 static int bonus_bright = 0;
124 static int wire_bonus=0;
125 #define wireframe (wire_flag||wire_bonus>8||wire_bonus%2==1)
126
127 static double speed_bonus=0.0;
128 #define effective_speed (direction*(speed+speed_bonus))
129
130 static int spin_bonus = 0;
131 static int backwards_bonus = 0;
132
133 /* No. of levels of interpolation, for perspective */
134 #define INTERP 32
135
136 /* These must be powers of 2 */
137 #define TERRAIN_LENGTH 256
138 #define TERRAIN_BREADTH 32
139
140 /* total "perspective distance" of terrain */
141 #define TERRAIN_PDIST (INTERP*TERRAIN_LENGTH)
142
143 #define ROTS 1024
144 #define TB_MUL (ROTS/TERRAIN_BREADTH)
145 static double sintab[ROTS], costab[ROTS];
146
147 static int orientation = (17*ROTS)/22;
148
149 static int terrain[TERRAIN_LENGTH][TERRAIN_BREADTH];
150 static double xcurvature[TERRAIN_LENGTH];
151 static double ycurvature[TERRAIN_LENGTH];
152 static double zcurvature[TERRAIN_LENGTH];
153 static int wideness[TERRAIN_LENGTH];
154 static int bonuses[TERRAIN_LENGTH];
155 static int xvals[TERRAIN_LENGTH][TERRAIN_BREADTH];
156 static int yvals[TERRAIN_LENGTH][TERRAIN_BREADTH];
157 static double worldx[TERRAIN_LENGTH][TERRAIN_BREADTH];
158 static double worldy[TERRAIN_LENGTH][TERRAIN_BREADTH];
159 static int minx[TERRAIN_LENGTH], maxx[TERRAIN_LENGTH];
160 static int miny[TERRAIN_LENGTH], maxy[TERRAIN_LENGTH];
161
162 #define random_elevation() (terrain_flag?(random() % 200):0)
163 #define random_curvature() (curviness>0.0?((double)(random() % 40)-20)*curviness:0.0)
164 #define random_twist() (twistiness>0.0?((double)(random() % 40)-20)*twistiness:0.0)
165 #define random_wideness() (widening_flag?(int)(random() % 1200):0)
166
167 #define STEEL_ELEVATION 300
168
169 /* a forward declaration ... */
170 static void change_colors(void);
171
172 #if HAVE_GETTIMEOFDAY
173 static int total_nframes = 0;
174 static int nframes = 0;
175 static double fps = 0.0;
176 static double fps_start, fps_end;
177 static struct timeval start_time;
178
179 /*
180  * get_time ()
181  *
182  * returns the total time elapsed since the beginning of the demo
183  */
184 static double get_time(void) {
185   struct timeval t;
186   float f;
187 #if GETTIMEOFDAY_TWO_ARGS
188   gettimeofday(&t, NULL);
189 #else
190   gettimeofday(&t);
191 #endif
192   t.tv_sec -= start_time.tv_sec;
193   f = ((double)t.tv_sec) + t.tv_usec*1e-6;
194   return f;
195 }
196
197 /*
198  * init_time ()
199  *
200  * initialises the timing structures
201  */
202 static void init_time(void) {
203 #if GETTIMEOFDAY_TWO_ARGS
204   gettimeofday(&start_time, NULL);
205 #else
206   gettimeofday(&start_time);
207 #endif
208   fps_start = get_time();
209 }
210 #endif
211
212 /*
213  * perspective()
214  *
215  * perspective map the world coordinates worldx[i][j], worldy[i][j] onto
216  * screen coordinates xvals[t][j], yvals[t][j]
217  */
218 static void
219 perspective (void)
220 {
221         static int rotation_offset=0;
222
223         int i, j, jj, t=0, depth, view_pos;
224         int rotation_bias, r;
225         double xc=0.0, yc=0.0, zc=0.0;
226         double xcc=0.0, ycc=0.0, zcc=0.0;
227         double xx, yy;
228         double zfactor, zf;
229
230         zf = 8.0*28.0 / (double)(width*TERRAIN_LENGTH);
231         if (be_wormy) zf *= 3.0;
232
233         depth = TERRAIN_PDIST - INTERP + pindex;
234
235         view_pos = (nearest+3*TERRAIN_LENGTH/4)%TERRAIN_LENGTH;
236         
237         xoffset += - xcurvature[view_pos]*curviness/8;
238         xoffset /= 2;
239
240         yoffset += - ycurvature[view_pos]*curviness/4;
241         yoffset /= 2;
242
243         rotation_offset += (int)((zcurvature[view_pos]-zcurvature[nearest])*ROTS/8);
244         rotation_offset /= 2;
245         rotation_bias = orientation + spin_bonus - rotation_offset;
246
247         if (bumps_flag) {
248                 if (be_wormy) {
249                         yoffset -= ((terrain[view_pos][TERRAIN_BREADTH/4] * width /(8*1600)));
250                         rotation_bias += (terrain[view_pos][TERRAIN_BREADTH/4+2] -
251                                                          terrain[view_pos][TERRAIN_BREADTH/4-2])/8;
252                 } else {
253                         yoffset -= ((terrain[view_pos][TERRAIN_BREADTH/4] * width /(2*1600)));
254                         rotation_bias += (terrain[view_pos][TERRAIN_BREADTH/4+2] -
255                                                          terrain[view_pos][TERRAIN_BREADTH/4-2])/16;
256                 }
257         }
258
259         MODULO(rotation_bias, ROTS);
260
261         for (t=0; t < TERRAIN_LENGTH; t++) {
262                 i = nearest + t; MODULO(i, TERRAIN_LENGTH);
263                 xc += xcurvature[i]; yc += ycurvature[i]; zc += zcurvature[i];
264                 xcc += xc; ycc += yc; zcc += zc;
265                 maxx[i] = maxy[i] = 0;
266                 minx[i] = width; miny[i] = height;
267         }
268
269         for (t=0; t < TERRAIN_LENGTH; t++) {
270                 i = nearest - 1 - t; MODULO(i, TERRAIN_LENGTH);
271
272                 zfactor = (double)depth* (12.0 - TERRAIN_LENGTH/8.0) * zf;
273                 for (j=0; j < TERRAIN_BREADTH; j++) {
274                         jj = direction * j; MODULO(jj, TERRAIN_BREADTH);
275                         xx = (worldx[i][jj]-(vertigo*xcc))/zfactor; 
276                         yy = (worldy[i][j]-(vertigo*ycc))/zfactor;
277
278                         r = rotation_bias + (int)(vertigo*zcc); MODULO(r, ROTS);
279
280                         xvals[t][j] = xoffset + width/2 +
281                                           (int)(xx * costab[r] - yy * sintab[r]);
282                         maxx[t] = MAX(maxx[t], xvals[t][j]);
283                         minx[t] = MIN(minx[t], xvals[t][j]);
284
285                         yvals[t][j] = yoffset + height/2 +
286                                                   (int)(xx * sintab[r] + yy * costab[r]);
287                         maxy[t] = MAX(maxy[t], yvals[t][j]);
288                         miny[t] = MIN(miny[t], yvals[t][j]);
289                 }
290                 xcc -= xc; ycc -= yc; zcc -= zc;
291                 xc -= xcurvature[i]; yc -= ycurvature[i]; zc -= zcurvature[i];
292                 depth -= INTERP;
293         }
294 }
295
296 /*
297  * wrap_tunnel (start, end)
298  *
299  * wrap the terrain terrain[i][j] around the semi-circular tunnel function
300  * 
301  *                      x' = x/2 * cos(theta) - (y-k) * x * sin(theta)
302  *                      y' = x/4 * sin(theta) + y * cos(theta)
303  *
304  * between i=start and i=end inclusive, producing world coordinates
305  * worldx[i][j], worldy[i][j]
306  */
307 static void
308 wrap_tunnel (int start, int end)
309 {
310         int i, j, v;
311         double x, y;
312
313         assert (start < end);
314
315         for (i=start; i <= end; i++) {
316                 for (j=0; j < TERRAIN_BREADTH; j++) {
317                         x = j * (1.0/TERRAIN_BREADTH);
318                         v = terrain[i][j];
319                         y = (double)(v==STEEL_ELEVATION?200:v) - wideness[i] - 1200;
320
321                         /* lower road */
322                         if (j > TERRAIN_BREADTH/8 && j < 3*TERRAIN_BREADTH/8) y -= 300;
323
324                     worldx[i][j] = x/2 * costab[j*TB_MUL] -
325                                                         (y-height/4.0)*x*sintab[j*TB_MUL];
326                         worldy[i][j] = x/4 * sintab[j*TB_MUL] +
327                                                         y * costab[j*TB_MUL];
328                 }
329         }
330 }
331
332 /*
333  * flip_direction()
334  *
335  * perform the state transitions and terrain transformation for the
336  * "look backwards/look forwards" bonus
337  */
338 static void
339 flip_direction (void)
340 {
341         int i, ip, in, j, t;
342
343         direction = -direction;
344
345         bonus_bright = 20;
346
347         for (i=0; i < TERRAIN_LENGTH; i++) {
348                 in = nearest + i; MODULO(in, TERRAIN_BREADTH);
349                 ip = nearest - i; MODULO(ip, TERRAIN_BREADTH);
350                 for (j=0; j < TERRAIN_BREADTH; j++) {
351                         t = terrain[ip][j];
352                         terrain[ip][j] = terrain[in][j];
353                         terrain[in][j] = t;
354                 }
355         }
356 }
357
358 /*
359  * generate_smooth (start, end)
360  *
361  * generate smooth terrain between i=start and i=end inclusive
362  */
363 static void
364 generate_smooth (int start, int end)
365 {
366         int i,j, ii;
367
368         assert (start < end);
369
370         for (i=start; i <= end; i++) {
371                 ii = i; MODULO(ii, TERRAIN_LENGTH);
372                 for (j=0; j < TERRAIN_BREADTH; j++) {
373                         terrain[i][j] = STEEL_ELEVATION;
374                 }
375         }
376 }
377
378 /*
379  * generate_straight (start, end)
380  *
381  * zero the curvature and wideness between i=start and i=end inclusive
382  */
383 static void
384 generate_straight (int start, int end)
385 {
386         int i,j, ii;
387
388         assert (start < end);
389
390         for (i=start; i <= end; i++) {
391                 ii = i; MODULO(ii, TERRAIN_LENGTH);
392                 for (j=0; j < TERRAIN_BREADTH; j++) {
393                         xcurvature[ii] = 0;
394                         ycurvature[ii] = 0;
395                         zcurvature[ii] = 0;
396                         wideness[ii] = 0;
397                 }
398         }
399 }
400
401 /*
402  * int generate_terrain_value (v1, v2, v3, v4, w)
403  *
404  * generate terrain value near the average of v1, v2, v3, v4, with
405  * perturbation proportional to w
406  */
407 static int
408 generate_terrain_value (int v1, int v2, int v3, int v4, int w)
409 {
410         int sum, ret;
411         int rval;
412
413         if (!terrain_flag) return 0;
414
415         sum = v1 + v2 + v3 + v4;
416
417         rval = w*sum/smoothness;
418         if (rval == 0) rval = 2;
419
420         ret = (sum/4 -(rval/2) + RAND(rval));
421
422         if (ret < -400 || ret > 400) {
423                 ret = sum/4;
424         }
425
426         return ret;
427 }
428
429 /*
430  * generate_terrain (start, end, final)
431  *
432  * generate terrain[i][j] between i=start and i=end inclusive
433  *
434  * This is performed by successive subdivision of the terrain into
435  * rectangles of decreasing size. Subdivision continues until the
436  * the minimum width or height of these rectangles is 'final'; ie.
437  * final=1 indicates to subdivide as far as possible, final=2 indicates
438  * to stop one subdivision before that (leaving a checkerboard pattern
439  * uncalculated) etc.
440  */
441 static void
442 generate_terrain (int start, int end, int final)
443 {
444         int i,j,w,l;
445         int ip, jp, in, jn; /* prev, next values */
446         int diff;
447
448         assert (start < end);
449         assert (start >= 0 && start < TERRAIN_LENGTH);
450         assert (end >= 0 && end < TERRAIN_LENGTH);
451
452         diff = end - start + 1;
453
454         terrain[end][0] = random_elevation();
455         terrain[end][TERRAIN_BREADTH/2] = random_elevation();
456
457         for (w= diff/2, l=TERRAIN_BREADTH/4;
458              w >= final || l >= final; w /= 2, l /= 2) {
459
460                 if (w<1) w=1; if (l<1) l=1;
461
462                 for (i=start+w-1; i < end; i += (w*2)) {
463                         ip = i-w; MODULO(ip, TERRAIN_LENGTH);
464                         in = i+w; MODULO(in, TERRAIN_LENGTH);
465                         for (j=l-1; j < TERRAIN_BREADTH; j += (l*2)) {
466                                 jp = j-1; MODULO(jp, TERRAIN_BREADTH);
467                                 jn = j+1; MODULO(jn, TERRAIN_BREADTH);
468                                 terrain[i][j] =
469                                         generate_terrain_value (terrain[ip][jp], terrain[in][jp], 
470                                             terrain[ip][jn], terrain[in][jn], w);
471                         }
472                 }
473
474                 for (i=start+(w*2)-1; i < end; i += (w*2)) {
475                         ip = i-w; MODULO(ip, TERRAIN_LENGTH);
476                         in = i+w; MODULO(in, TERRAIN_LENGTH);
477                         for (j=l-1; j < TERRAIN_BREADTH; j += (l*2)) {
478                                 jp = j-1; MODULO(jp, TERRAIN_BREADTH);
479                                 jn = j+1; MODULO(jn, TERRAIN_BREADTH);
480                                 terrain[i][j] =
481                                         generate_terrain_value (terrain[ip][j], terrain[in][j],
482                                             terrain[i][jp], terrain[i][jn], w);
483                         }
484                 }
485
486                 for (i=start+w-1; i < end; i += (w*2)) {
487                         ip = i-w; MODULO(ip, TERRAIN_LENGTH);
488                         in = i+w; MODULO(in, TERRAIN_LENGTH);
489                         for (j=2*l-1; j < TERRAIN_BREADTH; j += (l*2)) {
490                                 jp = j-1; MODULO(jp, TERRAIN_BREADTH);
491                                 jn = j+1; MODULO(jn, TERRAIN_BREADTH);
492                                 terrain[i][j] =
493                                         generate_terrain_value (terrain[ip][j], terrain[in][j],
494                                             terrain[i][jp], terrain[i][jn], w);
495                         }
496                 }
497         }
498 }
499
500 /*
501  * double generate_curvature_value (v1, v2, w)
502  *
503  * generate curvature value near the average of v1 and v2, with perturbation
504  * proportional to w
505  */
506 static double
507 generate_curvature_value (double v1, double v2, int w)
508 {
509         double sum, avg, diff, ret;
510         int rval;
511
512         assert (!isnan(v1) && !isnan(v2));
513
514         sum = v1+v2;
515         avg = sum/2.0;
516
517         diff = MIN(v1 - avg, v2 - avg);
518
519         rval = (int)diff * w;
520         if (rval == 0.0) return avg;
521
522         ret = (avg -((double)rval)/500.0 + ((double)RAND(rval))/1000.0);
523
524         assert (!isnan(ret));
525
526         return ret;
527 }
528
529 /*
530  * generate_curves (start, end)
531  *
532  * generate xcurvature[i], ycurvature[i], zcurvature[i] and wideness[i]
533  * between start and end inclusive
534  */
535 static void
536 generate_curves (int start, int end)
537 {
538         int i, diff, ii, in, ip, w;
539
540         assert (start < end);
541
542         diff = end - start + 1; MODULO (diff, TERRAIN_LENGTH);
543
544         if (random() % 100 == 0)
545           xcurvature[end] = 30 * random_curvature();
546         else if (random() % 10 == 0)
547           xcurvature[end] = 20 * random_curvature();
548         else
549           xcurvature[end] = 10 * random_curvature();
550
551         if (random() % 50 == 0)
552           ycurvature[end] = 20 * random_curvature();
553         else if (random() % 25 == 0)
554           ycurvature[end] = 30 * random_curvature();
555         else
556           ycurvature[end] = 10 * random_curvature();
557
558         if (random() % 3 == 0)
559           zcurvature[end] = random_twist();
560         else
561           zcurvature[end] =
562                           generate_curvature_value (zcurvature[end], random_twist(), 1);
563
564         if (be_wormy)
565                         wideness[end] = random_wideness();
566         else
567                 wideness[end] =
568                         generate_curvature_value (wideness[end], random_wideness(), 1);
569
570     for (w=diff/2; w >= 1; w /= 2) {
571       for (i=start+w-1; i < end; i+=(w*2)) {
572         ii = i; MODULO (ii, TERRAIN_LENGTH);
573                 ip = i-w; MODULO (ip, TERRAIN_LENGTH);
574             in = i+w; MODULO (in, TERRAIN_LENGTH);
575             xcurvature[ii] =
576                                 generate_curvature_value (xcurvature[ip], xcurvature[in], w);
577             ycurvature[ii] =
578                                 generate_curvature_value (ycurvature[ip], ycurvature[in], w);
579             zcurvature[ii] =
580                                 generate_curvature_value (zcurvature[ip], zcurvature[in], w);
581             wideness[ii] =
582                                 generate_curvature_value (wideness[ip], wideness[in], w);
583       }
584     }
585 }
586
587 /*
588  * do_bonus ()
589  *
590  * choose a random bonus and perform its state transition
591  */
592 static void
593 do_bonus (void)
594 {
595         static int jamming=0;
596
597         bonus_bright = 20;
598
599         if (jamming > 0) {
600                 jamming--;
601                 nearest -= 2; MODULO(nearest, TERRAIN_LENGTH);
602                 return;
603         }
604
605         if (psychedelic_flag) change_colors();
606
607         switch (random() % 7) {
608         case 0: /* switch to or from wireframe */
609                 wire_bonus = (wire_bonus?0:300);
610                 break;
611         case 1: /* speedup */
612                 speed_bonus = 40.0;
613                 break;
614         case 2:
615                 spin_bonus += ROTS;
616                 break;
617         case 3:
618                 spin_bonus -= ROTS;
619                 break;
620         case 4: /* look backwards / look forwards */
621                 flipped_at = nearest;
622                 flip_direction ();
623                 backwards_bonus = (backwards_bonus?0:10);
624                 break;
625         case 5:
626                 change_colors();
627                 break;
628         case 6: /* jam against the bonus a few times; deja vu! */
629                 nearest -= 2; MODULO(nearest, TERRAIN_LENGTH);
630                 jamming = 3;
631                 break;
632         default:
633                 assert(0);
634                 break;
635         }
636 }
637
638 /*
639  * check_bonus ()
640  *
641  * check if a bonus has been passed in the last frame, and handle it
642  */
643 static void
644 check_bonuses (void)
645 {
646         int i, ii, start, end;
647
648         if (!bonuses_flag) return;
649
650         if (step >= 0.0) {
651                 start = nearest; end = nearest + (int)floor(step);
652         } else {
653                 end = nearest; start = nearest + (int)floor(step);
654         }
655
656         if (be_wormy) {
657                 start += TERRAIN_LENGTH/4;
658                 end += TERRAIN_LENGTH/4;
659         }
660
661         for (i=start; i < end; i++) {
662                 ii = i; MODULO(ii, TERRAIN_LENGTH);
663                 if (bonuses[ii] == 1) do_bonus ();
664         }
665 }
666
667 /*
668  * decrement_bonuses (double time_per_frame)
669  *
670  * decrement timers associated with bonuses
671  */
672 static void
673 decrement_bonuses (double time_per_frame)
674 {
675         if (!bonuses_flag) return;
676
677         if (bonus_bright > 0) bonus_bright-=4;
678         if (wire_bonus > 0) wire_bonus--;
679         if (speed_bonus > 0) speed_bonus -= 2.0;
680
681         if (spin_bonus > 10) spin_bonus -= (int)(step*13.7);
682         else if (spin_bonus < -10) spin_bonus += (int)(step*11.3);
683
684         if (backwards_bonus > 1) backwards_bonus--;
685         else if (backwards_bonus == 1) {
686                 nearest += 2*(MAX(flipped_at, nearest) - MIN(flipped_at,nearest));
687                 MODULO(nearest, TERRAIN_LENGTH);
688                 flip_direction ();
689                 backwards_bonus = 0;
690         }
691 }
692
693 /*
694  * set_bonuses (start, end)
695  *
696  * choose if to and where to set a bonus between i=start and i=end inclusive
697  */
698 static void
699 set_bonuses (int start, int end)
700 {
701         int i, diff, ii;
702
703         if (!bonuses_flag) return;
704
705         assert (start < end);
706
707         diff = end - start;
708
709         for (i=start; i <= end; i++) {
710                 ii = i; if (ii>=TERRAIN_LENGTH) ii -= TERRAIN_LENGTH;
711                 bonuses[ii] = 0;
712         }
713         if (random() % 4 == 0) {
714                 i = start + RAND(diff-3);
715                 ii = i; if (ii>=TERRAIN_LENGTH) ii -= TERRAIN_LENGTH;
716                 bonuses[ii] = 2; /* marker */
717                 ii = i+3; if (ii>=TERRAIN_LENGTH) ii -= TERRAIN_LENGTH;
718                 bonuses[ii] = 1; /* real thing */
719         }
720 }
721
722 /*
723  * regenerate_terrain ()
724  *
725  * regenerate a portion of the terrain map of length TERRAIN_LENGTH/4 iff
726  * we just passed between two quarters of the terrain.
727  *
728  * choose the kind of terrain to produce, produce it and wrap the tunnel
729  */
730 static void
731 regenerate_terrain (void)
732 {
733         int start, end;
734         int passed;
735
736         passed = nearest % (TERRAIN_LENGTH/4);
737
738         if (speed == 0.0 ||
739                 (speed > 0.0 && passed > (int)step) ||
740                 (speed < 0.0 && (TERRAIN_LENGTH/4)-passed > (int)fabs(step))) {
741
742                 return;
743         }
744
745         end = nearest - passed - 1; MODULO(end, TERRAIN_LENGTH);
746         start = end - TERRAIN_LENGTH/4 + 1; MODULO(start, TERRAIN_LENGTH);
747
748         if (DEBUG_FLAG) printf ("Regenerating [%d - %d]\n", start, end);
749
750         set_bonuses (start, end);
751
752         switch (random() % 64) {
753         case 0:
754         case 1:
755                 generate_terrain (start, end, 1);
756                 generate_smooth (start,
757                         start + TERRAIN_LENGTH/8 + (random() % TERRAIN_LENGTH/8));
758                 break;
759         case 2:
760                 generate_smooth (start, end);
761                 generate_terrain (start, end, 4); break;
762         case 3:
763                 generate_smooth (start, end);
764                 generate_terrain (start, end, 2); break;
765         default:
766                 generate_terrain (start, end, 1);
767         }
768
769         if (random() % 16 == 0) {
770                 generate_straight (start, end);
771         } else {
772                 generate_curves (start, end);
773         }
774
775         wrap_tunnel (start, end);
776 }
777
778 /*
779  * init_terrain ()
780  *
781  * initialise the terrain map for the beginning of the demo
782  */
783 static void
784 init_terrain (void)
785 {
786         int i, j;
787
788         for (i=0; i < TERRAIN_LENGTH; i++) {
789                 for (j=0; j < TERRAIN_BREADTH; j++) {
790                         terrain[i][j] = 0;
791                 }
792         }
793
794         terrain[TERRAIN_LENGTH-1][0] =  - (random() % 300);
795         terrain[TERRAIN_LENGTH-1][TERRAIN_BREADTH/2] =  - (random() % 300);
796
797         generate_smooth (0, TERRAIN_LENGTH-1);
798         generate_terrain (0, TERRAIN_LENGTH/4 -1, 4);
799         generate_terrain (TERRAIN_LENGTH/4, TERRAIN_LENGTH/2 -1, 2);
800         generate_terrain (TERRAIN_LENGTH/2, 3*TERRAIN_LENGTH/4 -1, 1);
801         generate_smooth (3*TERRAIN_LENGTH/4, TERRAIN_LENGTH-1);
802 }
803
804 /*
805  * init_curves ()
806  *
807  * initialise the curvatures and wideness for the beginning of the demo.
808  */
809 static void
810 init_curves (void)
811 {
812         int i;
813
814         for (i=0; i < TERRAIN_LENGTH-1; i++) {
815         xcurvature[i] = 0.0;
816             ycurvature[i] = 0.0;
817                 zcurvature[i] = 0.0;
818         }
819
820     xcurvature[TERRAIN_LENGTH-1] = random_curvature();
821     ycurvature[TERRAIN_LENGTH-1] = random_curvature();
822     zcurvature[TERRAIN_LENGTH-1] = random_twist();
823
824         generate_straight (0, TERRAIN_LENGTH/4-1);
825         generate_curves (TERRAIN_LENGTH/4, TERRAIN_LENGTH/2-1);
826         generate_curves (TERRAIN_LENGTH/2, 3*TERRAIN_LENGTH/4-1);
827         generate_straight (3*TERRAIN_LENGTH/4, TERRAIN_LENGTH-1);
828
829 }
830
831 /*
832  * render_quads (dpy, d, t, dt, i)
833  *
834  * renders the quadrilaterals from perspective depth t to t+dt.
835  * i is passed as a hint, where i corresponds to t as asserted.
836  */
837 static void
838 render_quads (Display * dpy, Drawable d, int t, int dt, int i)
839 {
840         int j, t2, j2, in;
841         int index;
842         XPoint points[4];
843         GC gc;
844
845         assert (i == (nearest - (t + dt) + TERRAIN_LENGTH) % TERRAIN_LENGTH);
846
847         in = i + 1; MODULO(in, TERRAIN_LENGTH);
848
849         for (j=0; j < TERRAIN_BREADTH; j+=dt) {
850                 t2 = t+dt; if (t2 >= TERRAIN_LENGTH) t2 -= TERRAIN_LENGTH;
851                 j2 = j+dt; if (j2 >= TERRAIN_BREADTH) j2 -= TERRAIN_BREADTH;
852                 points[0].x = xvals[t][j]; points[0].y = yvals[t][j];
853                 points[1].x = xvals[t2][j]; points[1].y = yvals[t2][j];
854                 points[2].x = xvals[t2][j2]; points[2].y = yvals[t2][j2];
855                 points[3].x = xvals[t][j2]; points[3].y = yvals[t][j2];
856
857             index = bonus_bright + ncolors/3 +
858                                 t*(t*INTERP + pindex) * ncolors /
859                             (3*TERRAIN_LENGTH*TERRAIN_PDIST);
860                 if (!wireframe) {
861                         index += (int)((points[0].y - points[3].y) / 8);
862                         index += (int)((worldx[i][j] - worldx[in][j]) / 40);
863                         index += (int)((terrain[in][j] - terrain[i][j]) / 100);
864                 }
865                 if (be_wormy && psychedelic_flag) index += ncolors/4;
866
867                 index = MIN (index, ncolors-1);
868                 index = MAX (index, 0);
869
870                 if (bonuses[i]) {
871                         XSetClipMask (dpy, bonus_gcs[index], None);
872                 }
873
874                 if (wireframe) {
875                         if (bonuses[i]) gc = bonus_gcs[index];
876                         else gc = ground_gcs[index];
877                         XDrawLines (dpy, d, gc, points, 4, CoordModeOrigin);
878                 } else {
879                         if (bonuses[i])
880                                 gc = bonus_gcs[index];
881                         else if ((direction>0 && j < TERRAIN_BREADTH/8) ||
882                                 (j > TERRAIN_BREADTH/8 && j < 3*TERRAIN_BREADTH/8-1) ||
883                                 (direction < 0 && j > 3*TERRAIN_BREADTH/8-1 &&
884                                         j < TERRAIN_BREADTH/2) ||
885                                 terrain[i][j] == STEEL_ELEVATION ||
886                                 wideness[in] - wideness[i] > 200) 
887                                 gc = ground_gcs[index];
888                         else
889                                 gc = wall_gcs[index];
890
891                         XFillPolygon (dpy, d, gc, points, 4, Nonconvex, CoordModeOrigin);
892                 }
893         }
894 }
895
896 /*
897  * render_pentagons (dpy, d, t, dt, i)
898  *
899  * renders the pentagons from perspective depth t to t+dt.
900  * i is passed as a hint, where i corresponds to t as asserted.
901  */
902 static void
903 render_pentagons (Display *dpy, Drawable d, int t, int dt, int i)
904 {
905         int j, t2, j2, j3, in;
906         int index;
907         XPoint points[5];
908         GC gc;
909
910         assert (i == (nearest -t + TERRAIN_LENGTH) % TERRAIN_LENGTH);
911
912         in = i + 1; MODULO(in, TERRAIN_LENGTH);
913
914         for (j=0; j < TERRAIN_BREADTH; j+=dt*2) {
915                 t2 = t+(dt*2); if (t2 >= TERRAIN_LENGTH) t2 -= TERRAIN_LENGTH;
916                 j2 = j+dt; if (j2 >= TERRAIN_BREADTH) j2 -= TERRAIN_BREADTH;
917                 j3 = j+dt+dt; if (j3 >= TERRAIN_BREADTH) j3 -= TERRAIN_BREADTH;
918                 points[0].x = xvals[t][j]; points[0].y = yvals[t][j];
919                 points[1].x = xvals[t2][j]; points[1].y = yvals[t2][j];
920                 points[2].x = xvals[t2][j2]; points[2].y = yvals[t2][j2];
921                 points[3].x = xvals[t2][j3]; points[3].y = yvals[t2][j3];
922                 points[4].x = xvals[t][j3]; points[4].y = yvals[t][j3];
923
924             index = bonus_bright + ncolors/3 +
925                                 t*(t*INTERP + pindex) * ncolors /
926                             (3*TERRAIN_LENGTH*TERRAIN_PDIST);
927                 if (!wireframe) {
928                         index += (int)((points[0].y - points[3].y) / 8);
929                         index += (int)((worldx[i][j] - worldx[in][j]) / 40);
930                         index += (int)((terrain[in][j] - terrain[i][j]) / 100);
931                 }
932                 if (be_wormy && psychedelic_flag) index += ncolors/4;
933
934                 index = MIN (index, ncolors-1);
935                 index = MAX (index, 0);
936
937                 if (bonuses[i]) {
938                         XSetClipMask (dpy, bonus_gcs[index], None);
939                 }
940
941                 if (wireframe) {
942                         if (bonuses[i]) gc = bonus_gcs[index];
943                         else gc = ground_gcs[index];
944                         XDrawLines (dpy, d, gc, points, 5, CoordModeOrigin);
945                 } else {
946                         if (bonuses[i])
947                                 gc = bonus_gcs[index];
948                         else if (j < TERRAIN_BREADTH/8 ||
949                                 (j > TERRAIN_BREADTH/8 && j < 3*TERRAIN_BREADTH/8-1) ||
950                                 terrain[i][j] == STEEL_ELEVATION ||
951                                 wideness[in] - wideness[i] > 200) 
952                                 gc = ground_gcs[index];
953                         else
954                                 gc = wall_gcs[index];
955
956                         XFillPolygon (dpy, d, gc, points, 5, Complex, CoordModeOrigin);
957                 }
958         }
959 }
960
961 /*
962  * render_block (dpy, d, gc, t)
963  *
964  * render a filled polygon at perspective depth t using the given GC
965  */
966 static void
967 render_block (Display * dpy, Drawable d, GC gc, int t)
968 {
969         int i;
970
971         XPoint erase_points[TERRAIN_BREADTH/2];
972
973         for (i=0; i < TERRAIN_BREADTH/2; i++) {
974                 erase_points[i].x = xvals[t][i*2];
975                 erase_points[i].y = yvals[t][i*2];
976         }
977
978         XFillPolygon (dpy, d, gc, erase_points,
979                                   TERRAIN_BREADTH/2, Complex, CoordModeOrigin);
980 }
981
982 /*
983  * regenerate_stars_mask (dpy, t)
984  *
985  * regenerate the clip mask 'stars_mask' for drawing the bonus stars at
986  * random positions within the bounding box at depth t
987  */
988 static void
989 regenerate_stars_mask (Display * dpy, int t)
990 {
991         int i, w, h, a, b, l1, l2;
992         const int lim = width*TERRAIN_LENGTH/(300*(TERRAIN_LENGTH-t));
993
994         w = maxx[t] - minx[t];
995         h = maxy[t] - miny[t];
996
997         if (w<6||h<6) return;
998
999         XFillRectangle (dpy, stars_mask, stars_erase_gc,
1000                                         0, 0, width, height);
1001
1002         l1 = (t>3*TERRAIN_LENGTH/4?2:1);
1003         l2 = (t>7*TERRAIN_LENGTH/8?2:1);
1004
1005         for (i=0; i < lim; i++) {
1006                 a = RAND(w); b = RAND(h);
1007                 XDrawLine (dpy, stars_mask, stars_gc,
1008                                         minx[t]+a-l1, miny[t]+b, minx[t]+a+l1, miny[t]+b);
1009                 XDrawLine (dpy, stars_mask, stars_gc,
1010                                         minx[t]+a, miny[t]+b-l1, minx[t]+a, miny[t]+b+l1);
1011         }
1012         for (i=0; i < lim; i++) {
1013                 a = RAND(w); b = RAND(h);
1014                 XDrawLine (dpy, stars_mask, stars_gc,
1015                                         minx[t]+a-l2, miny[t]+b, minx[t]+a+l2, miny[t]+b);
1016                 XDrawLine (dpy, stars_mask, stars_gc,
1017                                         minx[t]+a, miny[t]+b-l2, minx[t]+a, miny[t]+b+l2);
1018         }
1019 }
1020
1021 /*
1022  * render_bonus_block (dpy, d, t, i)
1023  *
1024  * draw the bonus stars at depth t.
1025  * i is passed as a hint, where i corresponds to t as asserted.
1026  */
1027 static void
1028 render_bonus_block (Display * dpy, Drawable d, int t, int i)
1029 {
1030         int bt;
1031
1032         assert (i == (nearest -t + TERRAIN_LENGTH) % TERRAIN_LENGTH);
1033
1034         if (!bonuses[i] || wireframe) return;
1035
1036         regenerate_stars_mask (dpy, t);
1037
1038         bt = t * nr_bonus_colors / (2*TERRAIN_LENGTH);
1039
1040         XSetClipMask (dpy, bonus_gcs[bt], stars_mask);
1041
1042         render_block (dpy, d, bonus_gcs[bt], t);
1043 }
1044
1045 static int
1046 begin_at (void)
1047 {
1048         int t;
1049         int max_minx=0, min_maxx=width, max_miny=0, min_maxy=height;
1050
1051         for (t=TERRAIN_LENGTH-1; t > 0; t--) {
1052                 max_minx = MAX (max_minx, minx[t]);
1053                 min_maxx = MIN (min_maxx, maxx[t]);
1054                 max_miny = MAX (max_miny, miny[t]);
1055                 min_maxy = MIN (min_maxy, maxy[t]);
1056
1057                 if (max_miny >= min_maxy || max_minx >= min_maxx) break;
1058         }
1059
1060         return t;
1061 }
1062
1063 /*
1064  * render_speedmine (dpy, d)
1065  *
1066  * render the current frame.
1067  */
1068 static void
1069 render_speedmine (Display * dpy, Drawable d)
1070 {
1071         int t, i=nearest, dt=1;
1072         GC gc;
1073
1074         assert (nearest >= 0 && nearest < TERRAIN_LENGTH);
1075
1076         if (be_wormy || wireframe) {
1077                 XFillRectangle (dpy, d, erase_gc, 0, 0, width, height);
1078
1079                 dt=4;
1080                 for (t=0; t < TERRAIN_LENGTH/4; t+=dt) {
1081                         render_bonus_block (dpy, d, t, i);
1082                         i -= dt; MODULO(i, TERRAIN_LENGTH);
1083                         render_quads (dpy, d, t, dt, i);
1084                 }
1085
1086                 assert (t == TERRAIN_LENGTH/4);
1087         } else {
1088                 t = MAX(begin_at(), TERRAIN_LENGTH/4);
1089                 /*t = TERRAIN_LENGTH/4; dt = 2; */
1090                 dt = (t >= 3*TERRAIN_LENGTH/4 ? 1 : 2);
1091                 i = (nearest -t + TERRAIN_LENGTH) % TERRAIN_LENGTH;
1092                 render_block (dpy, d, tunnelend_gc, t);
1093         }
1094
1095         dt=2;
1096
1097         if (t == TERRAIN_LENGTH/4)
1098                 render_pentagons (dpy, d, t, dt, i);
1099
1100         for (; t < 3*TERRAIN_LENGTH/4; t+=dt) {
1101                 render_bonus_block (dpy, d, t, i);
1102                 i -= dt; MODULO(i, TERRAIN_LENGTH);
1103                 render_quads (dpy, d, t, dt, i);
1104         }
1105
1106         dt=1;
1107         if (be_wormy) {
1108                 for (; t < TERRAIN_LENGTH-(1+(pindex<INTERP/2)); t+=dt) {
1109                         render_bonus_block (dpy, d, t, i);
1110                         i -= dt; MODULO(i, TERRAIN_LENGTH);
1111                 }
1112         } else {
1113                 if (wireframe) {assert (t == 3*TERRAIN_LENGTH/4);}
1114
1115                 if (t == 3*TERRAIN_LENGTH/4)
1116                         render_pentagons (dpy, d, t, dt, i);
1117
1118                 for (; t < TERRAIN_LENGTH-(1+(pindex<INTERP/2)); t+=dt) {
1119                         render_bonus_block (dpy, d, t, i);
1120                         i -= dt; MODULO(i, TERRAIN_LENGTH);
1121                         render_quads (dpy, d, t, dt, i);
1122                 }
1123         }
1124
1125         /* Draw crosshair */
1126         if (crosshair_flag) {
1127                 gc = (wireframe ? bonus_gcs[nr_bonus_colors/2] : erase_gc);
1128                 XFillRectangle (dpy, d, gc,
1129                                                 width/2+(xoffset)-8, height/2+(yoffset*2)-1, 16, 3);
1130                 XFillRectangle (dpy, d, gc,
1131                                                 width/2+(xoffset)-1, height/2+(yoffset*2)-8, 3, 16);
1132         }
1133
1134 }
1135
1136 /*
1137  * move (step)
1138  *
1139  * move to the position for the next frame, and modify the state variables
1140  * nearest, pindex, pos, speed
1141  */
1142 static void
1143 move (double step)
1144 {
1145         double dpos;
1146
1147         pos += step;
1148         dpos = SIGN3(pos) * floor(fabs(pos));
1149
1150         pindex += SIGN3(effective_speed) + INTERP;
1151         while (pindex >= INTERP) {
1152                 nearest --;
1153                 pindex -= INTERP;
1154         }
1155         while (pindex < 0) {
1156                 nearest ++;
1157                 pindex += INTERP;
1158         }
1159
1160     nearest += dpos; MODULO(nearest, TERRAIN_LENGTH);
1161
1162         pos -= dpos;
1163
1164         accel = thrust + ycurvature[nearest] * gravity;
1165         speed += accel;
1166         if (speed > maxspeed) speed = maxspeed;
1167         if (speed < -maxspeed) speed = -maxspeed;
1168 }
1169
1170 /*
1171  * speedmine (dpy, window)
1172  *
1173  * do everything required for one frame of the demo
1174  */
1175 static void
1176 speedmine (Display *dpy, Window window)
1177 {
1178         double elapsed, time_per_frame = 0.04;
1179
1180         regenerate_terrain ();
1181
1182         perspective ();
1183
1184         render_speedmine (dpy, dbuf);
1185         XCopyArea (dpy, dbuf, window, draw_gc, 0, 0, width, height, 0, 0);
1186
1187 #if HAVE_GETTIMEOFDAY
1188         fps_end = get_time();
1189         nframes++;
1190         total_nframes++;
1191
1192         if (fps_end > fps_start + 0.5) {
1193                 elapsed = fps_end - fps_start;
1194                 fps_start = get_time();
1195
1196                 time_per_frame = elapsed / nframes - delay*1e-6;
1197                 fps = nframes / elapsed;
1198                 if (DEBUG_FLAG) {
1199                         printf ("%f s elapsed\t%3f s/frame\t%.1f FPS\n", elapsed,
1200                                         time_per_frame, fps);
1201                 }
1202                 step = effective_speed * elapsed;
1203
1204                 nframes = 0;
1205         }
1206 #else
1207         time_per_frame = 0.04;
1208         step = effective_speed;
1209 #endif
1210
1211         move (step);
1212
1213         decrement_bonuses (time_per_frame);
1214
1215         check_bonuses ();
1216 }
1217
1218 /*
1219  * speedmine_color_ramp (dpy, cmap, gcs, colors, ncolors, s1, s2, v1, v2)
1220  *
1221  * generate a color ramp of up to *ncolors between randomly chosen hues,
1222  * varying from saturation s1 to s2 and value v1 to v2, placing the colors
1223  * in 'colors' and creating corresponding GCs in 'gcs'.
1224  *
1225  * The number of colors actually allocated is returned in ncolors.
1226  */
1227 static void
1228 speedmine_color_ramp (Display * dpy, Colormap cmap, GC *gcs, XColor * colors,
1229                                          int *ncolors, double s1, double s2, double v1, double v2)
1230 {
1231         XGCValues gcv;
1232         int h1, h2;
1233         unsigned long flags;
1234         int i;
1235
1236         assert (*ncolors >= 0);
1237         assert (s1 >= 0.0 && s1 <= 1.0 && v1 >= 0.0 && v2 <= 1.0);
1238
1239         if (psychedelic_flag) {
1240                 h1 = RAND(360); h2 = (h1 + 180) % 360;
1241         } else {
1242                 h1 = h2 = RAND(360);
1243         }
1244
1245         make_color_ramp (dpy, cmap, h1, s1, v1, h2, s2, v2,
1246                                      colors, ncolors, False, True, False);
1247
1248         flags = GCForeground;
1249         for (i=0; i < *ncolors; i++) {
1250                 gcv.foreground = colors[i].pixel;
1251                 gcs[i] = XCreateGC (dpy, dbuf, flags, &gcv);
1252         }
1253
1254 }
1255
1256 /*
1257  * change_colors ()
1258  *
1259  * perform the color changing bonus. New colors are allocated for the
1260  * walls and bonuses, and if the 'psychedelic' option is set then new
1261  * colors are also chosen for the ground.
1262  */
1263 static void
1264 change_colors (void)
1265 {
1266         double s1, s2;
1267
1268         if (psychedelic_flag) {
1269                 free_colors (display, cmap, bonus_colors, nr_bonus_colors);
1270                 free_colors (display, cmap, wall_colors, nr_wall_colors);
1271                 free_colors (display, cmap, ground_colors, nr_ground_colors);
1272                 ncolors = MAX_COLORS;
1273
1274                 s1 = 0.4; s2 = 0.9;
1275
1276                 speedmine_color_ramp (display, cmap, ground_gcs, ground_colors,
1277                                                           &ncolors, 0.0, 0.8, 0.0, 0.9);
1278                 nr_ground_colors = ncolors;
1279         } else {
1280                 free_colors (display, cmap, bonus_colors, nr_bonus_colors);
1281                 free_colors (display, cmap, wall_colors, nr_wall_colors);
1282                 ncolors = nr_ground_colors;
1283
1284                 s1 = 0.0; s2 = 0.6;
1285         }
1286
1287     speedmine_color_ramp (display, cmap, wall_gcs, wall_colors, &ncolors,
1288                                                   s1, s2, 0.0, 0.9);
1289     nr_wall_colors = ncolors;
1290
1291     speedmine_color_ramp (display, cmap, bonus_gcs, bonus_colors, &ncolors,
1292                                                   0.6, 0.9, 0.4, 1.0);
1293         nr_bonus_colors = ncolors;
1294 }
1295
1296 /*
1297  * init_psychedelic_colors (dpy, window, cmap)
1298  *
1299  * initialise a psychedelic colormap
1300  */
1301 static void
1302 init_psychedelic_colors (Display * dpy, Window window, Colormap cmap)
1303 {
1304   XGCValues gcv;
1305
1306   gcv.foreground = get_pixel_resource ("tunnelend", "TunnelEnd", dpy, cmap);
1307   tunnelend_gc = XCreateGC (dpy, window, GCForeground, &gcv);
1308
1309   ncolors = MAX_COLORS;
1310
1311   speedmine_color_ramp (dpy, cmap, ground_gcs, ground_colors, &ncolors,
1312                                                 0.0, 0.8, 0.0, 0.9);
1313   nr_ground_colors = ncolors;
1314
1315   speedmine_color_ramp (dpy, cmap, wall_gcs, wall_colors, &ncolors,
1316                                                 0.0, 0.6, 0.0, 0.9);
1317   nr_wall_colors = ncolors;
1318
1319   speedmine_color_ramp (dpy, cmap, bonus_gcs, bonus_colors, &ncolors,
1320                                                 0.6, 0.9, 0.4, 1.0);
1321   nr_bonus_colors = ncolors;
1322 }
1323
1324 /*
1325  * init_colors (dpy, window, cmap)
1326  *
1327  * initialise a normal colormap
1328  */
1329 static void
1330 init_colors (Display * dpy, Window window, Colormap cmap)
1331 {
1332   XGCValues gcv;
1333   XColor dark, light;
1334   int h1, h2;
1335   double s1, s2, v1, v2;
1336   unsigned long flags;
1337   int i;
1338
1339   gcv.foreground = get_pixel_resource ("tunnelend", "TunnelEnd", dpy, cmap);
1340   tunnelend_gc = XCreateGC (dpy, window, GCForeground, &gcv);
1341
1342   ncolors = MAX_COLORS;
1343
1344   dark.pixel = get_pixel_resource ("darkground", "DarkGround", dpy, cmap);
1345   XQueryColor (dpy, cmap, &dark);
1346
1347   light.pixel = get_pixel_resource ("lightground", "LightGround", dpy, cmap);
1348   XQueryColor (dpy, cmap, &light);
1349
1350   rgb_to_hsv (dark.red, dark.green, dark.blue, &h1, &s1, &v1);
1351   rgb_to_hsv (light.red, light.green, light.blue, &h2, &s2, &v2);
1352   make_color_ramp (dpy, cmap, h1, s1, v1, h2, s2, v2,
1353                                   ground_colors, &ncolors, False, True, False);
1354   nr_ground_colors = ncolors;
1355
1356   flags = GCForeground;
1357   for (i=0; i < ncolors; i++) {
1358         gcv.foreground = ground_colors[i].pixel;
1359         ground_gcs[i] = XCreateGC (dpy, dbuf, flags, &gcv);
1360   }
1361
1362   speedmine_color_ramp (dpy, cmap, wall_gcs, wall_colors, &ncolors,
1363                                                 0.0, 0.6, 0.0, 0.9);
1364   nr_wall_colors = ncolors;
1365
1366   speedmine_color_ramp (dpy, cmap, bonus_gcs, bonus_colors, &ncolors,
1367                                                 0.6, 0.9, 0.4, 1.0);
1368   nr_bonus_colors = ncolors;
1369 }
1370
1371 /*
1372  * print_stats ()
1373  *
1374  * print out average FPS stats for the demo
1375  */
1376 static void
1377 print_stats (void)
1378 {
1379         if (total_nframes >= 1)
1380                 printf ("Rendered %d frames averaging %f FPS\n", total_nframes,
1381                                 total_nframes / get_time());
1382 }
1383
1384 /*
1385  * init_speedmine (dpy, window)
1386  *
1387  * initialise the demo
1388  */
1389 static void
1390 init_speedmine (Display *dpy, Window window)
1391 {
1392   XGCValues gcv;
1393   XWindowAttributes xgwa;
1394   int i;
1395   double th;
1396   int wide;
1397
1398   display = dpy;
1399
1400   XGetWindowAttributes (dpy, window, &xgwa);
1401   cmap = xgwa.colormap;
1402   width = xgwa.width;
1403   height = xgwa.height;
1404
1405   verbose_flag = get_boolean_resource ("verbose", "Boolean");
1406
1407   dbuf = XCreatePixmap (dpy, window, width, height, xgwa.depth);
1408   stars_mask = XCreatePixmap (dpy, window, width, height, 1);
1409
1410   gcv.foreground = default_fg_pixel =
1411     get_pixel_resource ("foreground", "Foreground", dpy, cmap);
1412   draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
1413   stars_gc = XCreateGC (dpy, stars_mask, GCForeground, &gcv);
1414
1415   gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
1416   erase_gc = XCreateGC (dpy, dbuf, GCForeground, &gcv);
1417   stars_erase_gc = XCreateGC (dpy, stars_mask, GCForeground, &gcv);
1418
1419   wire_flag = get_boolean_resource ("wire", "Boolean");
1420
1421   psychedelic_flag = get_boolean_resource ("psychedelic", "Boolean");
1422
1423   delay = get_integer_resource("delay", "Integer");
1424
1425   smoothness = get_integer_resource("smoothness", "Integer");
1426   if (smoothness < 1) smoothness = 1;
1427
1428   maxspeed = get_float_resource("maxspeed", "Float");
1429   maxspeed *= 0.01;
1430   maxspeed = fabs(maxspeed);
1431
1432   thrust = get_float_resource("thrust", "Float");
1433   thrust *= 0.2;
1434
1435   gravity = get_float_resource("gravity", "Float");
1436   gravity *= 0.002/9.8;
1437
1438   vertigo = get_float_resource("vertigo", "Float");
1439   vertigo *= 0.2;
1440
1441   curviness = get_float_resource("curviness", "Float");
1442   curviness *= 0.25;
1443
1444   twistiness = get_float_resource("twistiness", "Float");
1445   twistiness *= 0.125;
1446
1447   terrain_flag = get_boolean_resource ("terrain", "Boolean");
1448   widening_flag = get_boolean_resource ("widening", "Boolean");
1449   bumps_flag = get_boolean_resource ("bumps", "Boolean");
1450   bonuses_flag = get_boolean_resource ("bonuses", "Boolean");
1451   crosshair_flag = get_boolean_resource ("crosshair", "Boolean");
1452
1453   be_wormy = get_boolean_resource ("worm", "Boolean");
1454   if (be_wormy) {
1455       maxspeed   *= 1.43;
1456       thrust     *= 10;
1457       gravity    *= 3;
1458       vertigo    *= 0.5;
1459       smoothness *= 2;
1460       curviness  *= 2;
1461       twistiness *= 2;
1462       psychedelic_flag = True;
1463       crosshair_flag = False;
1464   }
1465
1466   if (psychedelic_flag) init_psychedelic_colors (dpy, window, cmap);
1467   else init_colors (dpy, window, cmap);
1468
1469   for (i=0; i<ROTS; i++) {
1470         th = M_PI * 2.0 * i / ROTS;
1471         costab[i] = cos(th);
1472         sintab[i] = sin(th);
1473   }
1474
1475   wide = random_wideness();
1476
1477   for (i=0; i < TERRAIN_LENGTH; i++) {
1478         wideness[i] = wide;
1479         bonuses[i] = 0;
1480   }
1481
1482   init_terrain ();
1483   init_curves ();
1484   wrap_tunnel (0, TERRAIN_LENGTH-1);
1485
1486   if (DEBUG_FLAG || verbose_flag) atexit(print_stats);
1487
1488   step = effective_speed;
1489
1490 #ifdef HAVE_GETTIMEOFDAY
1491   init_time ();
1492 #endif
1493
1494 }
1495
1496 \f
1497 /*
1498  * Down the speedmine, you'll find speed
1499  * to satisfy your moving needs;
1500  * So if you're looking for a blast
1501  * then hit the speedmine, really fast.
1502  */
1503
1504 /*
1505  * Speedworm likes to choke and spit
1506  * and chase his tail, and dance a bit
1507  * he really is a funky friend;
1508  * he's made of speed from end to end.
1509  */
1510
1511 char *progclass = "Speedmine";
1512
1513 char *defaults [] = {
1514   ".verbose: False",
1515   "*worm: False",
1516   "*wire: False",
1517   ".background: black",
1518   ".foreground: white",
1519   "*darkground: #101010",
1520   "*lightground: #a0a0a0",
1521   "*tunnelend: #000000",
1522   "*delay:      30000",
1523   "*maxspeed: 700",
1524   "*thrust: 1.0",
1525   "*gravity: 9.8",
1526   "*vertigo: 1.0",
1527   "*terrain: True",
1528   "*smoothness: 6",
1529   "*curviness: 1.0",
1530   "*twistiness: 1.0",
1531   "*widening: True",
1532   "*bumps: True",
1533   "*bonuses: True",
1534   "*crosshair: True",
1535   "*psychedelic: False",
1536   0
1537 };
1538
1539 XrmOptionDescRec options [] = {
1540   { "-verbose",                 ".verbose",                             XrmoptionNoArg, "True"},
1541   { "-worm",                    ".worm",                                XrmoptionNoArg, "True"},
1542   { "-wire",                    ".wire",                                XrmoptionNoArg, "True"},
1543   { "-nowire",                  ".wire",                                XrmoptionNoArg, "False"},
1544   { "-darkground",              ".darkground",                  XrmoptionSepArg, 0 },
1545   { "-lightground",             ".lightground",                 XrmoptionSepArg, 0 },
1546   { "-tunnelend",               ".tunnelend",                   XrmoptionSepArg, 0 },
1547   { "-delay",           ".delay",               XrmoptionSepArg, 0 },
1548   { "-maxspeed",                ".maxspeed",                    XrmoptionSepArg, 0 },
1549   { "-thrust",                  ".thrust",                              XrmoptionSepArg, 0 },
1550   { "-gravity",                 ".gravity",                             XrmoptionSepArg, 0 },
1551   { "-vertigo",                 ".vertigo",                             XrmoptionSepArg, 0 },
1552   { "-terrain",                 ".terrain",                             XrmoptionNoArg, "True"},
1553   { "-noterrain",               ".terrain",                             XrmoptionNoArg, "False"},
1554   { "-smoothness",      ".smoothness",                  XrmoptionSepArg, 0 },
1555   { "-curviness",               ".curviness",                   XrmoptionSepArg, 0 },
1556   { "-twistiness",              ".twistiness",                  XrmoptionSepArg, 0 },
1557   { "-widening",                ".widening",                    XrmoptionNoArg, "True"},
1558   { "-nowidening",              ".widening",                    XrmoptionNoArg, "False"},
1559   { "-bumps",                   ".bumps",                               XrmoptionNoArg, "True"},
1560   { "-nobumps",                 ".bumps",                               XrmoptionNoArg, "False"},
1561   { "-bonuses",                 ".bonuses",                             XrmoptionNoArg, "True"},
1562   { "-nobonuses",               ".bonuses",                             XrmoptionNoArg, "False"},
1563   { "-crosshair",               ".crosshair",                   XrmoptionNoArg, "True"},
1564   { "-nocrosshair",             ".crosshair",                   XrmoptionNoArg, "False"},
1565   { "-psychedelic",             ".psychedelic",                 XrmoptionNoArg, "True"},
1566   { "-nopsychedelic",   ".psychedelic",                 XrmoptionNoArg, "False"},
1567   { 0, 0, 0, 0 }
1568 };
1569
1570
1571 void
1572 screenhack (Display *dpy, Window window)
1573 {
1574 #ifndef NDEBUG
1575         atexit (abort);
1576 #endif
1577
1578         init_speedmine (dpy, window);
1579
1580         while (1) {
1581                 speedmine (dpy, window);
1582                 XSync (dpy, False);
1583                 screenhack_handle_events (dpy);
1584                 if (delay) usleep(delay);
1585         }
1586 }
1587
1588 /* vim: ts=4
1589  */