/*
 * Copyright (c) 1990 by Sun Microsystems, Inc.
 */

#ident	"@(#)lunar.c	1.5	91/09/14 SMI"



#include <math.h>
#include "lunar_cps.h"
#include "NeWS/psio.h"
#include "NeWS/psmacros.h"

#define GRAVITY		0.1
#define STARS		5000

#define TERRAINSIZE	100
int	terrain[TERRAINSIZE];

/* slot for a particular x value and x pos for a particluar slot */
#define slot(x)	(((x) - xmin) * TERRAINSIZE / (xmax - xmin))
#define xpos(s)	((s) * (xmax - xmin) / (TERRAINSIZE - 1) + xmin)

/* convert degrees to radians and back */
#define torad(deg)	((deg) * M_PI / 180.0)
#define todeg(rad)	((int) ((rad) * 180.0 / M_PI))

int	xmin, ymin, xmax, ymax;

main()
{
    int	ret, tag, done = 0;
    int	terrainslot, randseed;
    int	thrust = 0, angle = 0;
    float	shipx, shipy, shipdx, shipdy;
    float	ground, realslot;
    int crashed, finished = 0;

    if (ps_open_PostScript() == 0) {
	psio_fprintf(psio_stderr, "Could not connect to server.\n");
	exit(1);
    }
    ps_initialize(&xmin, &ymin, &xmax, &ymax, &randseed);
    srand(randseed);
    shipx = (xmin * 9 + xmax) / 10.0;
    shipy = (ymin + ymax * 9) / 10.0;
    shipdx = 0.1;
    shipdy = -0.1;
    create_landscape();
    ps_starttimer(60);
    while (!done && !psio_error(PostScriptInput)) {
	ret = ps_peek_PostScript_event(&tag);
	if (ret < 0) {
	    psio_fprintf(psio_stderr, "Error reading from server.\n");
	    exit(1);
	}
	if (ret) {
	    switch (tag) {
	    case DAMAGETAG:
		ps_get_tag(DAMAGETAG);
		repaint_landscape();
		break;
	    case TIMERTAG:
		ps_get_tag(TIMERTAG);
		if (!finished) {
		    shipx += shipdx;
		    if (shipx <= xmin) {
			shipx = xmin;
			shipdx = -shipdx;
		    } else if (shipx >= xmax) {
			shipx = xmax;
			shipdx = -shipdx;
		    }
		    shipy += shipdy;
		    shipdy += cos(torad(angle)) * thrust / 30 - GRAVITY;
		    shipdx -= sin(torad(angle)) * thrust / 30;
		    terrainslot = (int) (realslot = slot(shipx));
		    if (terrainslot > TERRAINSIZE - 2)
			terrainslot = TERRAINSIZE - 2;
		    ground = terrain[terrainslot]
			+ (terrain[terrainslot + 1] - terrain[terrainslot])
			    * (realslot - terrainslot);
		    ps_eraseoverlay();
		    ps_lander(thrust, angle, (int) shipx, (int) shipy);
		    if (shipy <= ground) {
			shipy = ground;
			finished = 30;
			crashed = (abs(angle) >= 20 ||
				   fabs(shipdx) >= 3.0 ||
				   fabs(shipdy) >= 3.0);
			if (crashed)
			    makeshards(shipdx, shipdy);
		    }
		} else {
		    ps_eraseoverlay();
		    if (crashed)
			ps_boom((int) shipx, (int) shipy,
				(30 - finished) / 3 + 1, finished);
		    else {
			ps_lander(0, 0, (int) shipx, (int) shipy);
			ps_astronaut(((int)shipx) - 50 + finished, (int)shipy);
		    }
		    if (--finished == 0)
			done = 1;
		}
		ps_starttimer(60);
		ps_flush_PostScript();
		break;
	    case MOUSETAG: {
		    int mx, my;

		    ps_get_mouse(&mx, &my);
		    thrust = (my - ymin) * 50 / (ymax - ymin) - 25;
		    angle = (mx - xmin) * 200 / (xmax - xmin) - 100;
		    break;
		}
	    }
	} else {
	    psio_fprintf(psio_stderr, "Skipping bad data from server.\n");
	    ps_skip_PostScript_event();
	}
    }
    ps_eraseoverlay();
    ps_close_PostScript();
    exit(0);
}

create_landscape()
{
    int	i, ty = (rand() >> 4) % 128;
    int slope = (rand() >> 4) % 64;
    int maxty = (ymax + ymin) / 2;

    for (i = 0; i < TERRAINSIZE; i++) {
	terrain[i] = ty;
	ty += (((rand() >> 4) % 64) - slope);
	if (ty <= 1) {
	    ty = 1;
	    slope = (rand() >> 4) % 16;
	} else if (ty >= maxty) {
	    ty = maxty;
	    slope = (rand() >> 4) % 16 + 48;
	} else if ((rand() >> 4) % 16 < 5) {
	    slope = (int) sqrt((float) ((rand() >> 4) % 1024));
	    slope = ((rand() & (1 << 5)) ? slope + 32 : 32 - slope);
	}
    }
}

repaint_landscape()
{
    int		dmg_xmin, dmg_ymin, dmg_xmax, dmg_ymax;
    int		i, slot_start, slot_end;
    int		x, y, numstars;

    ps_damagepath(&dmg_xmin, &dmg_ymin, &dmg_xmax, &dmg_ymax);
    ps_moveto(0, terrain[0]);
    slot_start = slot(dmg_xmin) - 1;
    if (slot_start < 0)
	slot_start = 0;
    slot_end = slot(dmg_xmax) + 1;
    if (slot_end > TERRAINSIZE)
	slot_end = TERRAINSIZE;
    for (i = slot_start; i < slot_end; i++) {
	ps_lineto(xpos(i), terrain[i]);
    }
    ps_stroke();
    numstars = 0;
    dmg_xmax -= dmg_xmin;
    dmg_ymax -= dmg_ymin;
    for (i = dmg_xmax * dmg_ymax; (i -= STARS) > 0;) {
	x = (rand() >> 4) % dmg_xmax + dmg_xmin;
	y = (rand() >> 4) % dmg_ymax + dmg_ymin;
	if (y >= terrain[slot(x)]) {
	    numstars++;
	    ps_moveto(x, y);
	    ps_lineto(x, y);
	}
    }
    if (numstars)
	ps_stroke();
    ps_initclip();
}

makeshards(shipdx, shipdy)
    double shipdx, shipdy;
{
    int		i, length, speed;
    float	theta, radius, angle;

    speed = (int) sqrt((shipdx * shipdx) + (shipdy * shipdy));
    if (speed < 20)
	speed = 20;
    angle = atan2(shipdy, shipdx) + M_PI;
    ps_startshards();
    for (i = 0; i < 16; i++) {
	theta = torad((rand() >> 4) % 180 - 90) + angle;
	radius = (float) ((rand() >> 4) % speed + (speed / 2));
	length = (rand() >> 4) % (speed / 2) + (speed / 4);
	ps_gsave();
	ps_translate((int) (radius * cos(theta)), (int) (radius * sin(theta)));
	ps_dorotate(todeg(theta) + 90, (rand() >> 4) % speed - speed/2);
	ps_moveto(length, 0);
	ps_lineto(-length, 0);
	ps_stroke();
	ps_grestore();
    }
    ps_defshards();
}
