void FuzzySystem::agregarSaida(unsigned char numsaida) {
    Variavel saida = var[NUMINPUTS+numsaida];
    
    float x = saida.min;
    float pedaco = (saida.max - saida.min)/INTERVALO;
    unsigned char i,j;
    
    for (i=0; i<INTERVALO; i++) {
        var_saida[i] = 0;
        
		// Para cada conjunto nesse ponto, verificar qual tem maior forca
		// Roda para cada conjunto
		for (j=0; j<saida.numConjuntos; j++) {
			if (forca_conj_saida[numsaida][j] > 0) {
				var_saida[i] = fuzzy_max(var_saida[i], forca_conj_saida[numsaida][j]*saida.conj[j].membership(x));
			}
        }
        
        if (debug) std::cout << "Saida " << (int)numsaida << " (x = " << (int)i << ") = " << var_saida[i] << std::endl;
        
        x += pedaco;
    }
    
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_EVENT_QUEUE *evqueue = NULL;
	ALLEGRO_TIMER *timer = NULL;
	ALLEGRO_KEYBOARD_STATE keyboard_state;
    ALLEGRO_EVENT event;
    ALLEGRO_FONT *font;
    ALLEGRO_BITMAP *clock_hand, *clock_quadrant, *bow, *sword;
    float clock_ray = 0, clock_angle = 0;
    int clock_ray_alpha;
    float soul_interval = SOUL_TIME_INTERVAL;
    FuzzyPlayer *player, *cpu;
    FuzzyGame * game;

	bool running = true;
	bool redraw = true;

	int map_x = 13*16, map_y = 5*16;
	int screen_width = WINDOW_WIDTH;
	int screen_height = WINDOW_HEIGHT;
    double curtime;

	/* Initialization */
    fuzzy_iz_error(al_init(), "Failed to initialize allegro");
    fuzzy_load_addon("image", al_init_image_addon());
    fuzzy_load_addon("primitives", al_init_primitives_addon());
    fuzzy_load_addon("keyboard", al_install_keyboard());
    fuzzy_load_addon("mouse", al_install_mouse());
    al_init_font_addon();

	fuzzy_iz_error(timer = al_create_timer(1.0 / FPS), "Cannot create FPS timer");
    fuzzy_iz_error(evqueue = al_create_event_queue(), "Cannot create event queue");
	fuzzy_iz_error(display = al_create_display(screen_width, screen_height),
      "Cannot initialize display");
    al_set_window_title(display, WINDOW_TITLE);
    fuzzy_iz_error(font = al_load_font(fuzzy_res(FONT_FOLDER, "fixed_font.tga"), 0, 0), "Cannot load 'fixed_font.tga'");
    clock_hand = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "clock_hand.png"));
    fuzzy_iz_error(clock_hand, "Cannot load clock handle");
    clock_quadrant = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "clock_quadrant.png"));
    fuzzy_iz_error(clock_hand, "Cannot load clock quadrant");
    bow = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "bow.png"));
    fuzzy_iz_error(clock_hand, "Cannot load bow image");
    sword = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "sword.png"));
    fuzzy_iz_error(clock_hand, "Cannot load sword image");

	/* Queue setup */
	al_register_event_source(evqueue, al_get_display_event_source(display));
	al_register_event_source(evqueue, al_get_timer_event_source(timer));
	al_register_event_source(evqueue, al_get_keyboard_event_source());
    al_register_event_source(evqueue, al_get_mouse_event_source());

    /* Game setup */
    game = fuzzy_game_new("level000.tmx");
    player = fuzzy_player_new(game, FUZZY_PLAYER_LOCAL, "Dolly");
    cpu = fuzzy_player_new(game, FUZZY_PLAYER_CPU, "CPU_0");

    fuzzy_chess_add(game, player, FUZZY_FOO_LINK, 34, 30);
    fuzzy_chess_add(game, player, FUZZY_FOO_LINK, 33, 30);
    fuzzy_chess_add(game, cpu, FUZZY_FOO_LINK, 40, 30);
    bool showing_area = false;
    FuzzyChess *chess, *focus = NULL;

	al_clear_to_color(al_map_rgb(0, 0, 0));
    al_draw_bitmap(game->map->bitmap, -map_x, -map_y, 0);
	al_flip_display();

#if DEBUG
	ALLEGRO_BITMAP *icon;
    int fps, fps_accum;
    double fps_time;

    icon = al_load_bitmap(fuzzy_res(PICTURE_FOLDER, "icon.tga"));
    if (icon)
        al_set_display_icon(display, icon);
    fps_accum = fps_time = 0;
    fps = FPS;
#endif
    /* Server connection */
    int svsock;
    //~ FuzzyMessage * sendmsg = fuzzy_message_new();
    svsock = fuzzy_server_connect(FUZZY_DEFAULT_SERVER_ADDRESS, FUZZY_DEFAULT_SERVER_PORT);

    _aaa_menu(game, svsock);

	/* MAIN loop */
    player->soul_time = al_get_time();
    al_start_timer(timer);
	while (running) {
        /* wait until an event happens */
		al_wait_for_event(evqueue, &event);

        switch (event.type) {
        case ALLEGRO_EVENT_TIMER:
            /* check soul ticks */
            curtime = al_get_time();
            while (curtime - player->soul_time >= soul_interval) {
                //~ fuzzy_debug("Soul tick!");
                player->soul_time += soul_interval;
                player->soul_points += SOUL_POINTS_BOOST;

                clock_ray = 1;
            }
            clock_angle = (curtime - player->soul_time)/soul_interval * FUZZY_2PI;
            if (clock_ray) {
                clock_ray = (curtime - player->soul_time)/RAY_TIME_INTERVAL * 50 + 40;
                clock_ray_alpha = (curtime - player->soul_time)/RAY_TIME_INTERVAL*(55) + 200;
                if (clock_ray >= 90)
                    clock_ray = 0;
            }

            al_get_keyboard_state(&keyboard_state);
            if (al_key_down(&keyboard_state, ALLEGRO_KEY_RIGHT)) {
                map_x += 5;
                if (map_x > (game->map->tot_width - screen_width))
                    map_x = game->map->tot_width - screen_width;
            }
            else if (al_key_down(&keyboard_state, ALLEGRO_KEY_LEFT)) {
                map_x -= 5;
                if (map_x < 0)
                    map_x = 0;
            }
            else if (al_key_down(&keyboard_state, ALLEGRO_KEY_UP)) {
                map_y -= 5;
                if (map_y < 0)
                    map_y = 0;
            }
            else if (al_key_down(&keyboard_state, ALLEGRO_KEY_DOWN)) {
                map_y += 5;
                if (map_y > (game->map->tot_height - screen_height))
                    map_y = game->map->tot_height - screen_height;
            } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_O)) {
                soul_interval = fuzzy_max(0.1, soul_interval - 0.05);
            } else if (al_key_down(&keyboard_state, ALLEGRO_KEY_P)) {
                soul_interval += 0.05;
            }
            redraw = true;
            break;
        case ALLEGRO_EVENT_KEY_DOWN:
            if(! focus)
                break;

            _attack_area_off();

            switch(event.keyboard.keycode) {
                case ALLEGRO_KEY_W:
                    _chess_move(game, player, focus, focus->x, focus->y-1);
                    break;
                case ALLEGRO_KEY_A:
                    _chess_move(game, player, focus, focus->x-1, focus->y);
                    break;
                case ALLEGRO_KEY_S:
                    _chess_move(game, player, focus, focus->x, focus->y+1);
                    break;
                case ALLEGRO_KEY_D:
                    _chess_move(game, player, focus, focus->x+1, focus->y);
                    break;

                case ALLEGRO_KEY_K:
                    _attack_area_on();
                    break;
                case ALLEGRO_KEY_SPACE:
                    /* switch attack type */
                    if (! focus)
                        break;
                    if (focus->atkarea == &FuzzyMeleeMan)
                        focus->atkarea = &FuzzyRangedMan;
                    else
                        focus->atkarea = &FuzzyMeleeMan;
                    break;
            }
            break;
        case ALLEGRO_EVENT_DISPLAY_CLOSE:
            running = false;
            break;
        case ALLEGRO_EVENT_KEY_UP:
            break;
        case ALLEGRO_EVENT_KEY_CHAR:
            break;
        case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
            if(event.mouse.button == RIGHT_BUTTON) {
                _attack_area_on();
            } else if(event.mouse.button == LEFT_BUTTON) {
                /* world to tile coords */
                int tx = (event.mouse.x+map_x) / game->map->tile_width;
                int ty = (event.mouse.y+map_y) / game->map->tile_height;
#ifdef DEBUG
                printf("SELECT %d %d\n", tx, ty);
#endif
                if(showing_area && fuzzy_chess_inside_target_area(game, focus, tx, ty)) {
                    /* select attack target */
                    if (fuzzy_map_spy(game->map, FUZZY_LAYER_SPRITES, tx, ty) == FUZZY_CELL_SPRITE) {
                        if (fuzzy_chess_local_attack(game, player, focus, tx, ty))
                            _attack_area_off();
                    }
                } else {
                    /* select chess */
                    chess = fuzzy_chess_at(game, player, tx, ty);
                    if (chess && focus != chess) {
                        _attack_area_off();

                        if (focus != NULL) {
                            // already has a focus effect, just move it
                            fuzzy_sprite_move(game->map, FUZZY_LAYER_BELOW, focus->x, focus->y, tx, ty);
                        } else {
                            fuzzy_sprite_create(game->map, FUZZY_LAYER_BELOW, GID_TARGET, tx, ty);
                        }
                        focus = chess;
                    } else if (! chess) {
                        if (showing_area) {
                            // just hide the attack area
                            _attack_area_off();
                        } else if(focus) {
                            // remove the focus
                            fuzzy_sprite_destroy(game->map, FUZZY_LAYER_BELOW, focus->x, focus->y);
                            focus = NULL;
                        }
                    }
                }
            }
            break;
        default:
#ifdef DEBUG
            //~ fprintf(stderr, "Unknown event received: %d\n", event.type);
#endif
            break;
        }

        if (redraw && al_is_event_queue_empty(evqueue)) {
            curtime = al_get_time();
            fuzzy_map_update(game->map, curtime);

            // Clear the screen
            al_clear_to_color(al_map_rgb(0, 0, 0));
            al_draw_bitmap(game->map->bitmap, -map_x, -map_y, 0);

#ifdef GRID_ON
            /* Draw the grid */
            int tw = game->map->tile_width;
            int ty = game->map->tile_height;
            int x, y;
            for (x=(tw-map_x)%tw; x<screen_width; x+=tw)
                al_draw_line(x, 0, x, screen_height, al_map_rgba(7,7,7,100), 1);
            for (y=(ty-map_y)%ty; y<screen_height; y+=ty)
                al_draw_line(0, y, screen_width, y, al_map_rgba(7,7,7,100), 1);
#endif
#if DEBUG
            al_draw_filled_rounded_rectangle(screen_width-100, 4, screen_width, 30,
                8, 8, al_map_rgba(0, 0, 0, 200));
            al_draw_textf(font, al_map_rgb(255, 255, 255),
                screen_width-50, 8, ALLEGRO_ALIGN_CENTRE, "FPS: %d", fps);
#endif
            /* draw SP count */
            al_draw_filled_rounded_rectangle(4, screen_height-170, 175, screen_height-4,
                8, 8, al_map_rgba(0, 0, 0, 200));
            al_draw_textf(font, al_map_rgb(255, 255, 255),
                15, screen_height-163, ALLEGRO_ALIGN_LEFT, "SP: %d", player->soul_points);

            /* draw Soul Clock */
            al_draw_scaled_bitmap(clock_quadrant, 0, 0, 301, 301, 20, screen_height-80-139/2, 139, 139, 0);
            al_draw_scaled_rotated_bitmap(clock_hand, 160, 607, 90, screen_height-80, 0.11, 0.11, clock_angle, 0);
            al_draw_circle(90, screen_height-80, clock_ray, al_map_rgb(80, clock_ray_alpha, 80), 2.0);

            /* draw weapon */
            if (focus) {
                ALLEGRO_BITMAP * weapon;

                if (focus->atkarea == &FuzzyMeleeMan)
                    weapon = sword;
                else
                    weapon = bow;
                al_draw_scaled_bitmap(weapon, 0, 0, 90, 90, 20, 20, 60, 60, 0);
            }

            al_flip_display();
#if DEBUG
            fps_accum++;
            if (curtime - fps_time >= 1) {
                fps = fps_accum;
                fps_accum = 0;
                fps_time = curtime;
            }
#endif
            redraw = false;
        }
    }

	/* Cleanup */
    //~ void * retval;
    //~ char srvkey[FUZZY_SERVERKEY_LEN];
    //~ fuzzy_protocol_server_shutdown(svsock, sendmsg, srvkey);
    //~ fuzzy_message_del(sendmsg);
    fuzzy_game_free(game);

    al_destroy_event_queue(evqueue);
	al_destroy_display(display);
    al_destroy_timer(timer);
	return 0;
}
float* FuzzySystem::rodarSistema(float *x) {
    unsigned char regra, j, conj;
    float forca_regra;
    //
    //    if ((sizeof(x)/sizeof(float)) != NUMINPUTS) {
    //        if (debug) cout << "Erro: numero de entradas incompativel com sistema." << endl;
    //        return NULL;
    //    }
    
    if (debug) {
        std::cout << "Rodando sistema com entradas " << std::endl;
        for (j=0; j<NUMINPUTS; j++) {
            std::cout << "x[" << (int)j << "] = " << x[j] << std::endl;
        }
    }
    
    if (tipo == SUGENO) {
        for (j=0; j<NUMOUTPUTS; j++) {
            num[j] = 0;
            den[j] = 0;
        }
    }
    
    for (regra=0; regra<NUMRULES; regra++) {
        if (debug) std::cout << "Avaliando regra " << (int)regra << std::endl;
        forca_regra = 1;
        
        // Avaliar entradas para determinar forca que tera a regra
        // j = numero da entrada sendo avaliada em j
        for (j=0; j<NUMINPUTS; j++) {
            conj = (regras[regra].conjEntrada[j] - 1 < 0) ? 255 : regras[regra].conjEntrada[j] - 1; // conjunto que da variavel j que sera avaliado
            
            // Antes de calcular o mi de x, confirma se a regra depende daquela entrada, senao ja passo
            if (conj != 255) {
                if (debug) std::cout << "u("<< x[j] <<") no conjunto " << (int)conj+1 << " da entrada "<<(int)j<<": " << var[j].conj[conj].membership(x[j]) << std::endl;
                
                if (and_method == ANDMETHOD_MIN) {
                    forca_regra = fuzzy_min(var[j].conj[conj].membership(x[j]), forca_regra);
                } else if (and_method == ANDMETHOD_PROD) {
                    forca_regra *= var[j].conj[conj].membership(x[j]);
                }
            }
        }
        
        // Avaliar quanto sera cada saida ativada pela regra a partir dos dados atuais
        // Se a regra tiver forca 0, isso eh desnecessario, pois nao vai ativar nenhuma saida.
        if (forca_regra > 0) {
			if (debug) std::cout << "Regra ativada com forca " << forca_regra << std::endl;
            for (j=0; j<NUMOUTPUTS; j++) {
                conj = regras[regra].conjSaida[j]-1;
                
                if (tipo == SUGENO) {
                    num[j] += forca_regra * var[NUMINPUTS+j].linear_conj[conj].evaluate(x);
                    den[j] += forca_regra;
                } else if (tipo == MAMDANI) {
                    
                    // Se esta for a primeira vez avaliando essa saida, crio seu vetor
                    // que guardara a forca de cada um dos seus conjuntos
                    if (forca_conj_saida[j] == NULL) {
                        forca_conj_saida[j] = (float*) malloc(var[NUMINPUTS+j].numConjuntos*sizeof(float));
                        if (forca_conj_saida[j] == NULL) {
                            if (1) std::cout << "Erro de alocacao: forca_conj_saida" << std::endl;
                            return NULL;
                        }
                    }
                    
                    /*cout << "DEBUG fuzzy_max(forca_conj_saida[" << (int)j << "][" << (int)conj << "]=" << endl;
                     cout << forca_conj_saida[j][conj] << ", " << forca_regra << endl;*/
                    forca_conj_saida[j][conj] = fuzzy_max(forca_conj_saida[j][conj], forca_regra);
                    
                }
                
            }
        }
    }
    
    // Avaliar quanto ficou o final de cada saida
    for (j=0; j<NUMOUTPUTS; j++) {
        if (tipo == SUGENO) {
            saidas[j] = num[j]/den[j];
        } else if (tipo == MAMDANI) {
            agregarSaida(j);
            centroid(j);
        }
    }
    
    //limparVariaveis();
    return saidas;
}