Esempio n. 1
0
void move_organism(world_t *world, organism_t *organism)
{
    move_gene_t move_gene;
    personality_gene_t personality_gene;
    unsigned int move_gene_start_address;
    unsigned int personality_gene_start_address;
    unsigned int current_x;
    unsigned int current_y;
    unsigned int target_x;
    unsigned int target_y;
    char color_of_possible_friend;

    move_gene_start_address = gene_start_address(organism, GENE_INDEX_MOVE);
    parse_move_gene(organism, move_gene_start_address, &move_gene);

    personality_gene_start_address = gene_start_address(organism,
                                     GENE_INDEX_PERSONALITY);
    parse_personality_gene(organism, personality_gene_start_address,
                           &personality_gene);

    current_x = organism->position.x;
    current_y = organism->position.y;
    target_x = wrapped_index(current_x + move_gene.offset_x, WORLD_WIDTH);
    target_y = wrapped_index(current_y + move_gene.offset_y, WORLD_HEIGHT);

    if (NULL == world->cells[target_x][target_y]) {
        world->cells[target_x][target_y] = organism;
        world->cells[current_x][current_y] = NULL;
        organism->position.x = target_x;
        organism->position.y = target_y;
    }
    else {
        if (personality_gene.extrovert) {
            color_of_possible_friend
                = display_color(world->cells[target_x][target_y]);
            if ((!personality_gene.racist)
                    || (display_color(organism) == color_of_possible_friend)) {
                meet_organism(world, organism,
                              world->cells[target_x][target_y]);
            }
        }
        else {
            target_x = wrapped_index(current_x + (-1 * move_gene.offset_x),
                                     WORLD_WIDTH);
            target_y = wrapped_index(current_y + (-1 * move_gene.offset_y),
                                     WORLD_HEIGHT);
            if (NULL == world->cells[target_x][target_y]) {
                world->cells[target_x][target_y] = organism;
                world->cells[current_x][current_y] = NULL;
                organism->position.x = target_x;
                organism->position.y = target_y;
            }
        }
    }
}
Esempio n. 2
0
/*
 * Enters the start-menu state
 * Attempts to draw the start menu, and sets up the UI
 */
void start_menu()
{
	game_state = 4;

	clear_screen();	
	draw_titlescreen(&FatFs);
	
	display_color(CRIMSON, BLACK);
	ml_printf_at("%s", 135, 85, VERSION);
	
	display_color(WHITE, BLACK);
    	ml_printf_at("CENTRE to start", 5, 105);
    	ml_printf_at("< %u Players >", 5, 115, players);
}
Esempio n. 3
0
void check_win(){

        if (flags == 0){
            for(i = 0; i < rows; i++){
                for (j = 0; j < columns; j++){
                    //all flags have bombs
                    if (button_arr[i][j].flag == 1 && button_arr[i][j].value != -1) return;
                    //all bombs have flags
                    if (button_arr[i][j].flag != 1 && button_arr[i][j].value == -1) return;
                }
            }
        } else {
            for(i = 0; i < rows; i++){
            for (j = 0; j < columns; j++){
                //all the hidden ones are bombs
                if (button_arr[i][j].state == 0 && button_arr[i][j].value != -1) return;
            }
        }
        }
      
        cli();
        _delay_ms(500);
        display_color(BLACK, GREEN);
        clear_screen();
        display_string_xy("CONGRATULATIONS! YOU WON! :D\n", 40, 100);
        display_string_xy("Press center to restart", 60, 130);
        display_string_xy("Hold < to return to menu", 56, 150);
        finish_game();
        sei();
}
Esempio n. 4
0
void select_button(uint16_t x, uint16_t y) {
    button_col = BLUE;
    button_posx = x;
    button_posy = y;
    draw_button(x, y);
    display_color(BLACK,button_col);
    draw_flag(x,y);
}
Esempio n. 5
0
void deselect_button(int16_t x, int16_t y) {
   
    if (x >= 0 && y >= 0 && button_arr[x][y].state == 0){
        button_col = def_button_col;
        button_posx = x;
        button_posy = y;
        draw_button(x, y);
        display_color(BLACK, def_button_col);
        draw_flag(x,y);
    }
}
Esempio n. 6
0
void switch_button_flag(){
    button b;
     int e = get_selected_button(&b);
     if (e != 0){
        uint16_t x = b.posx;
        uint16_t y = b.posy;
        
        if (!button_arr[x][y].state) {
            int before = button_arr[x][y].flag;
            button_arr[x][y].flag = (button_arr[x][y].flag + 1) % 3;
            button_col = BLUE;
            draw_button_background(x,y);
            display_color(BLACK,button_col);  
            draw_flag(x,y);
            
            if (button_arr[x][y].flag == 1){
                
                    --flags;
                    char disp[20];
                    display_color(BLACK, def_button_col);
                    sprintf(disp, "== %d ==", flags);         
                    display_string_xy(disp, LCDHEIGHT/2 + 25, 13);
                
            } else {
                if (flags < maxflags && before == 1) {
                    ++flags;
                    char disp[20];
                    display_color(BLACK, def_button_col);
                    sprintf(disp, "== %d ==", flags);         
                    display_string_xy(disp, LCDHEIGHT/2 + 25, 13);
                }
                
            }
            check_win();

        }
     }
}
Esempio n. 7
0
/*
 * Draw the HP of all players at the top of the screen
 */
void draw_HP_UI()
{
	rectangle UI_rec = {0, WIDTH, 0, 15};
	fill_rectangle(UI_rec, BLACK);

	display_color(BLUE, BLACK);
	ml_printf_at("BLUE: %u", 5, 5, players_HP[0]);
	
	display_color(RED, BLACK);
	ml_printf_at("RED: %u", 85, 5, players_HP[1]);
	
	if(players >= 3)
	{
		display_color(YELLOW, BLACK);
	        ml_printf_at("YELLOW: %u", 165, 5, players_HP[2]);
		
		if(players == 4)
		{
			display_color(GREEN, BLACK);
		        ml_printf_at("GREEN: %u", 245, 5, players_HP[3]);
		}
	}
}
Esempio n. 8
0
int main(int argc, char *argv[])
{
	world_t *world;
	unsigned int x;
	unsigned int y;
	char c;
	char color;
	unsigned int each_iteration;
	unsigned int displayed_iteration = 0;
	unsigned int display_gene_start_address;
	unsigned int red_uint;
	unsigned int green_uint;
	unsigned int blue_uint;
	organism_t *organism;

	Display *display;
	GC gc;
	int screen_number;
	int window_x;
	int window_y;
	unsigned int each_x;
	unsigned int each_y;
	unsigned int window_border_width;
	unsigned int window_height;
	unsigned int window_width;
	unsigned long gc_value_mask;
	unsigned long window_background_color;
	unsigned long window_border_color;
	Window root_window;
	Window window;
	XGCValues gc_values;
	Visual* default_visual;
	Colormap colormap;
	XColor system_color_red;
	XColor system_color_green;
	XColor system_color_blue;
	XColor system_color_white;
	XColor system_color_black;
	XColor system_color;
	XColor exact_color;

	srandom(RANDOM_SEED);

#if CURSES_VISUALIZATION
	initscr();
	start_color();

#if CURSES_SOLID_COLORS
	init_pair(1, COLOR_BLACK, COLOR_RED);
	init_pair(2, COLOR_BLACK, COLOR_GREEN);
	init_pair(3, COLOR_BLACK, COLOR_BLUE);
	init_pair(4, COLOR_BLACK, COLOR_WHITE);
	init_pair(5, COLOR_BLACK, COLOR_BLACK);
#else
	init_pair(1, COLOR_RED, COLOR_BLACK);
	init_pair(2, COLOR_GREEN, COLOR_BLACK);
	init_pair(3, COLOR_BLUE, COLOR_BLACK);
	init_pair(4, COLOR_WHITE, COLOR_BLACK);
	init_pair(5, COLOR_BLACK, COLOR_BLACK);
#endif

#endif

#if X_VISUALIZATION
	display = XOpenDisplay(NULL);
	screen_number = DefaultScreen(display);
	root_window = RootWindow(display, screen_number);
	window_x = 0;
	window_y = 0;
	window_width = (WORLD_WIDTH * 4) + 16 + 3;
	window_height = (WORLD_HEIGHT * 9) + 16 + 8;
	window_border_width = 0;
	window_border_color = BlackPixel(display, screen_number);
	window_background_color = WhitePixel(display, screen_number);
	window = XCreateSimpleWindow(display, root_window, window_x, window_y,
			window_width, window_height, window_border_width,
			window_border_color, window_background_color);
	XMapWindow(display, window);
	XFlush(display);

	gc_value_mask = 0;
	gc = XCreateGC(display, window, gc_value_mask, &gc_values);
	XSync(display, False);

	default_visual = DefaultVisual(display, DefaultScreen(display));
	colormap = XCreateColormap(display, window, default_visual, AllocNone);
	XAllocNamedColor(display, colormap, "red", &system_color_red,
			&exact_color);
	XAllocNamedColor(display, colormap, "green", &system_color_green,
			&exact_color);
	XAllocNamedColor(display, colormap, "blue", &system_color_blue,
			&exact_color);
	XAllocNamedColor(display, colormap, "white", &system_color_white,
			&exact_color);
	XAllocNamedColor(display, colormap, "black", &system_color_black,
			&exact_color);
#endif

	world = new_world();
	for (each_iteration = 0; each_iteration < ITERATIONS; each_iteration++) {

		x = random_unsigned_int(WORLD_WIDTH);
		y = random_unsigned_int(WORLD_HEIGHT);
		each_x = x;
		each_y = y;

#if CURSES_VISUALIZATION
		for (x = 0; x < WORLD_WIDTH; x++) {
			for (y = 0; y < WORLD_HEIGHT; y++) {
				if (NULL == world->organisms[x][y]) {
					color = 'x';
					c = ' ';
				}
				else {
					color = display_color(world->organisms[x][y]);
					c = world->organisms[x][y]->face;
				}
				switch (color) {
					case 'r':
						mvaddch(y, x, c | COLOR_PAIR(1));
						break;
					case 'g':
						mvaddch(y, x, c | COLOR_PAIR(2));
						break;
					case 'b':
						mvaddch(y, x, c | COLOR_PAIR(3));
						break;
					case 'w':
						mvaddch(y, x, c | COLOR_PAIR(4));
						break;
					default:
						mvaddch(y, x, c | COLOR_PAIR(5));
						break;
				}
			}
		}
		refresh();
		usleep(SLEEP_US);
#endif

#if X_VISUALIZATION
		if (0 == (each_iteration % X_FRAME_SAMPLE)) {
			for (each_x = 0; each_x < WORLD_WIDTH; each_x++) {
				for (each_y = 0; each_y < WORLD_HEIGHT; each_y++) {
					organism = world->organisms[each_x][each_y];

					display_gene_start_address = gene_start_address(organism,
							GENE_INDEX_DISPLAY);
					red_uint = unsigned_int_from_genome(organism,
							display_gene_start_address + 0, 32);
					green_uint = unsigned_int_from_genome(organism,
							display_gene_start_address + 32, 32);
					blue_uint = unsigned_int_from_genome(organism,
							display_gene_start_address + 64, 32);

					system_color.red = red_uint % 8192;
					system_color.green = green_uint;
					system_color.blue = blue_uint;

					XAllocColor(display, colormap, &system_color);

					XSetForeground(display, gc, system_color.pixel);

					/* XDrawPoint(display, window, gc, each_x, each_y); */
					XDrawPoint(display, window, gc,
							((displayed_iteration % 4) * 128)
							+ (displayed_iteration % 4) + each_x,
							((displayed_iteration / 4) * 64) +
							 (displayed_iteration / 4) + each_y);
				}
			}
			displayed_iteration++;
			XFlush(display);
			usleep(SLEEP_US);
		}
#endif

		iterate_world(world);
	}

#if CURSES_VISUALIZATION
	endwin();
#endif

#if X_VISUALIZATION
	while (1) {
		usleep(SLEEP_US);
	}

	XUnmapWindow(display, window);
	XDestroyWindow(display, window);
	XCloseDisplay(display);
#endif

	destroy_world(world);

	return 0;
}
Esempio n. 9
0
int main(int argc, char *argv[])
{
    world_t world;
    unsigned int each_iteration;
    unsigned int each_organism;
    unsigned int x;
    unsigned int y;
    char c;
    char color;
    organism_t organisms[ORGANISM_COUNT];

    srandom(RANDOM_SEED);

#if CURSES_VISUALIZATION
    initscr();
    start_color();

#if CURSES_SOLID_COLORS
    init_pair(1, COLOR_BLACK, COLOR_RED);
    init_pair(2, COLOR_BLACK, COLOR_GREEN);
    init_pair(3, COLOR_BLACK, COLOR_BLUE);
    init_pair(4, COLOR_BLACK, COLOR_WHITE);
    init_pair(5, COLOR_BLACK, COLOR_BLACK);
#else
    init_pair(1, COLOR_RED, COLOR_BLACK);
    init_pair(2, COLOR_GREEN, COLOR_BLACK);
    init_pair(3, COLOR_BLUE, COLOR_BLACK);
    init_pair(4, COLOR_WHITE, COLOR_BLACK);
    init_pair(5, COLOR_BLACK, COLOR_BLACK);
#endif

#endif

    create_world(&world);
    for (each_organism = 0; each_organism < ORGANISM_COUNT; each_organism++) {
        create_organism(&world, &organisms[each_organism]);
    }
    for (each_iteration = 0; each_iteration < ITERATIONS; each_iteration++) {
        for (each_organism = 0; each_organism < ORGANISM_COUNT;
                each_organism++) {
            move_organism(&world, &organisms[each_organism]);
        }
#if CURSES_VISUALIZATION
        for (x = 0; x < WORLD_WIDTH; x++) {
            for (y = 0; y < WORLD_HEIGHT; y++) {
                if (NULL == world.cells[x][y]) {
                    color = 'x';
                    c = ' ';
                }
                else {
                    color = display_color(world.cells[x][y]);
                    c = world.cells[x][y]->face;
                }
                switch (color) {
                case 'r':
                    mvaddch(y, x, c | COLOR_PAIR(1));
                    break;
                case 'g':
                    mvaddch(y, x, c | COLOR_PAIR(2));
                    break;
                case 'b':
                    mvaddch(y, x, c | COLOR_PAIR(3));
                    break;
                case 'w':
                    mvaddch(y, x, c | COLOR_PAIR(4));
                    break;
                default:
                    mvaddch(y, x, c | COLOR_PAIR(5));
                    break;
                }
            }
        }
        refresh();
        usleep(SLEEP_US);
#endif
    }

    for (each_organism = 0; each_organism < ORGANISM_COUNT; each_organism++) {
        destroy_organism(&world, &organisms[each_organism]);
    }

#if CURSES_VISUALIZATION
    endwin();
#endif

    return 0;
}
Esempio n. 10
0
int main(int argc, char *argv[])
{
	world_t world;
	unsigned int each_iteration;
	unsigned int each_organism;
	unsigned int x;
	unsigned int y;
	char c;
	char color;
	organism_t organisms[ORGANISM_COUNT];
	char iteration_string[16];

	srandom(RANDOM_SEED);

	initscr();
	start_color();
	init_pair(1, COLOR_RED, COLOR_BLACK);
	init_pair(2, COLOR_GREEN, COLOR_BLACK);
	init_pair(3, COLOR_BLUE, COLOR_BLACK);
	init_pair(4, COLOR_WHITE, COLOR_BLUE);
	init_pair(5, COLOR_WHITE, COLOR_BLACK);
	attron(COLOR_PAIR(4));
	mvaddstr(39, 0, "                                   "
			"                                             ");
	mvaddstr(0, 0, "teoc - the evolution of culture - "
			"created 2007 by inhaesio zha                  ");

	create_world(&world);
	for (each_organism = 0; each_organism < ORGANISM_COUNT; each_organism++) {
		create_organism(&world, &organisms[each_organism]);
		//print_organism(&organisms[each_organism]);
	}
	for (each_iteration = 0; each_iteration < ITERATIONS; each_iteration++) {
		for (each_organism = 0; each_organism < ORGANISM_COUNT;
			 each_organism++) {
			move_organism(&world, &organisms[each_organism]);
		}
		for (x = 0; x < WORLD_WIDTH; x++) {
			for (y = 0; y < WORLD_HEIGHT; y++) {
				c = ' ';
				if (NULL != world.cells[x][y]) {
					color = display_color(world.cells[x][y]);
					c = world.cells[x][y]->face;
				}
				switch (color) {
					case 'r':
						mvaddch(y + 1, x, c | COLOR_PAIR(1));
						break;
					case 'g':
						mvaddch(y + 1, x, c | COLOR_PAIR(2));
						break;
					case 'b':
						mvaddch(y + 1, x, c | COLOR_PAIR(3));
						break;
					default:
						mvaddch(y + 1, x, c | COLOR_PAIR(5));
						break;
				}
			}
		}
		attron(COLOR_PAIR(4));
		//sprintf(iteration_string, "%s", each_iteration);
		//mvaddstr(39, 0, iteration_string);
		refresh();
		usleep(SLEEP_US);
	}

	endwin();

	return 0;
}
Esempio n. 11
0
void reveal_button(){

     button b;
     int e = get_selected_button(&b);
     if (e != 0){
        char disp[20];

        uint16_t x = b.posx;
        uint16_t y = b.posy;
        button_arr[x][y].state = 1;
        if (button_arr[x][y].flag == 1){
            if (flags < maxflags) {
                button_arr[x][y].flag = 0;
                ++flags;
                char disp[20];
                display_color(BLACK, def_button_col);
                sprintf(disp, "== %d ==", flags);         
                display_string_xy(disp, LCDHEIGHT/2 + 25, 13);
            }
        }

        if (b.value == -1){
            cli();
            button_col = RED;
            draw_button_background(x, y);
            display_color(BLACK,button_col);
            draw_button_content(x, y);
            draw_grid_lines(x,y);

            _delay_ms(1000);
            clear_screen();
            display_string_xy("GAME OVER :(\n", 110, 100);
            display_string_xy("Press center to restart", 60, 130);
            display_string_xy("Hold < to return to menu", 56, 150);
            finish_game();
            sei();
            return;
        } else {
            button_col = DIM_GRAY;
            draw_button_background(x, y);
            char n = b.value;
            if (n > 0){
                switch(n) {
                    case 1 :
                        display_color(BLUE,button_col);
                    break;
                    case 2 :
                        display_color(DARK_GREEN,button_col);
                    break;
                    case 3 :
                        display_color(RED, button_col);
                    break;
                    case 4:
                        display_color(PURPLE, button_col);
                    break;
                    default :
                        display_color(PURPLE,button_col);  
                }
                draw_button_content(x, y);
            }
            draw_grid_lines(x,y);
            check_neighbours(x,y);
            check_win();
        }
        
     }
     
}
Esempio n. 12
0
/*
 * Checks to see if there are enough alive players to continue the game
 * If so, control switches to the next player
 */
void start_turn()
{
	/* Update the HP UI */
	draw_HP_UI();

	/* Reset the power bar */
	launch_speed = 0;
	fill_rectangle(power_empty, BLACK);

	/* If only one player remains, declare them winner. */
	uint8_t i;
	uint8_t player_left; //Record last player left (meaningless if more than 1 survivor)
	uint8_t players_alive = 0;
	for(i = 0; i < players; i++)
	{
		if(players_HP[i] != 0)
		{
			players_alive++;
			player_left = i;
		}
	}
	if(players_alive == 1)
	{
		switch(player_left)
		{
			case 0:
				display_color(BLUE, BLACK);
				ml_printf_at("To the victor, the spoils! BLUE WINS!", 5, 20);
				break;
			case 1:
				display_color(RED, BLACK);
				ml_printf_at("And the win goes to RED! Congratulations!", 5, 20);
				break;
			case 2:
				display_color(YELLOW, BLACK);
				ml_printf_at("Who's the best? YELLOW's the best!", 5, 20);
				break;
			default:
				display_color(GREEN, BLACK);
				ml_printf_at("Looks like the others are GREEN with envy! (GREEN WINS)", 5, 20);
				break;
		}

		end_game();
		return;
	}
	else if(players_alive == 0)
	{
		display_color(WHITE, BLACK);
		ml_printf_at("Oh dear, it looks like a DRAW!", 5, 20);

		end_game();
		return;
	}

	/* Rotate through the players */
	current_player = (current_player + 1) % players;
	while(players_HP[current_player] == 0)
	{
		current_player = (current_player + 1) % players;
	}

	/* Re-colour the reticule for the current player */
	free_sprite(reticule_SPR);
	reticule_SPR = reticule(current_player);

	/* Change to movement phase */
	game_state = 1;

	/* Generate a random(ish) wind for the turn */
	wind_velocity = rand() % 21;

	rectangle *wind_bar = malloc(sizeof(rectangle));
	uint16_t wind_colour;
	fill_rectangle(wind_empty, BLACK);
	
	if(wind_velocity < 10)
	{
		wind_bar->left = WIND_BAR_START + wind_velocity * 10;
		wind_bar->right = WIND_BAR_END;
		wind_bar->top = 231;
		wind_bar->bottom = 236;
		wind_colour = RED;
	}
	else if (wind_velocity > 10)
	{
		wind_bar->left = WIND_BAR_START;
		wind_bar->right = WIND_BAR_START + (wind_velocity - 10) * 10;
		wind_bar->top = 231;
		wind_bar->bottom = 236;
		wind_colour = BLUE;
	}
	else
	{
		free(wind_bar);
		return;
	}
	fill_rectangle(*wind_bar, wind_colour);

	free(wind_bar);
}