/*
 * aasnake.c: The game snake written in aalib.
 *
 *
 * Copyright (c) 2003, eazy <eazy@ondaquadra.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * *  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <aalib.h>

#define SNAKE_LEN 20

typedef struct {
	int             x, y;
}               pixel;

pixel           snake[SNAKE_LEN];
int food = 0;

void
sigalrm(int signo)
{
        food = 1;
	alarm(10);
}

int
getdirection(aa_context * context)
{
	int             key;

	key = aa_getkey(context, 0);
	if (key == AA_RIGHT || key == AA_LEFT || key == AA_UP || key == AA_DOWN) {
		aa_printf(context, 0, 0, AA_SPECIAL, "Key pressed: %i", key);
		aa_flush(context);
		return key;
	}
	return AA_NONE;
}

int
main(int argc, char **argv)
{

	int             key, pressed;
        aa_context     *context;
        pixel          *head, *tail, *ptr;
	struct sigaction act;

	if (!aa_parseoptions(NULL, NULL, &argc, argv) || argc != 1) {
		fprintf(stderr, "Usage: %s\n"
			"Options:\n"
			"%s", argv[0], aa_help);
		exit(-1);
	}
	if ((context = aa_autoinit(&aa_defparams)) == NULL) {
		fprintf(stderr, "Can not initialize aalib\n");
		exit(-1);
	}
	if (!aa_autoinitkbd(context, 0)) {
		printf("Can not intialize keyboard\n");
		aa_close(context);
		exit(-1);
	}
	/*
           Definisco la posizione iniziale da cui partirà il serpente,
           le due coodinate si riferiscono alla testa del serpente
         */
	head = snake;
	tail = &snake[SNAKE_LEN - 1];

	head->x = 1;
	head->y = aa_imgheight(context) / 2;
	key = AA_RIGHT;

	act.sa_handler = sigalrm;
	sigemptyset(&act.sa_mask);
	act.sa_flags |= SA_RESTART;

	if (sigaction(SIGALRM, &act, NULL) == -1) {
		aa_puts(context, 0, 0, AA_SPECIAL, "Sigaction error");
		aa_flush(context);
		aa_close(context);
		exit(-1);
	}
	alarm(5);

	/*
           Il serpente si muove nella direzione dell'ultimo tasto
           freccia premuto dall'utente
         */
	while (head->x > 0 && head->x < aa_imgwidth(context) - 1 &&
	       head->y > 0 && head->y < aa_imgheight(context) - 1) {

                usleep(30000);
		aa_putpixel(context, tail->x, tail->y, 0);

		for (ptr = tail; ptr > head; ptr--)
			*ptr = *(ptr - 1);

		switch (key) {
		case AA_RIGHT:
			head->x++;
			break;
		case AA_LEFT:
			head->x--;
			break;
		case AA_UP:
			head->y--;
			break;
		case AA_DOWN:
			head->y++;
			break;
		default:
			aa_puts(context, 0, 0, AA_SPECIAL, "Key error");
			aa_flush(context);
			aa_close(context);
			exit(-1);
		}

		aa_putpixel(context, head->x, head->y, 255);

                if(food){
                  aa_putpixel(context, rand() % aa_imgwidth(context), rand() % aa_imgheight(context), 200);
                  food = 0;
                  alarm(10);
                }

		aa_fastrender(context, 0, 0, aa_scrwidth(context), aa_scrheight(context));
		aa_flush(context);

		if ((pressed = getdirection(context)) != AA_NONE)
			key = pressed;
	}
	aa_puts(context, 0, 0, AA_SPECIAL, "HAI PERSO");
	aa_flush(context);
	aa_close(context);
	return 0;
}
