Пример #1
0
int main(int argc, char* argv[])
{
    if (argc >= 2 && strcmp (argv[1], "init") == 0) {
	printf ("Initializing ...\n");
	launchConsole(1);
    } else
	launchConsole(0);
}
Пример #2
0
Файл: main.cpp Проект: alobo/ev
int main() {

    sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Main");

    // Setup test creature
    Creature creature = Creature();
    creature.position[0] = 100;
    creature.position[1] = 100;
    // creature.velocity[0] = 20;
    // creature.velocity[1] = 20;
    creature.setRotation(50);
    env.addObject(&creature);

    // Setup creatures
    for (int i = 0; i < NUM_CREATURES; ++i) {
        creatures[i] = Creature();
        creatures[i].position[0] = 50 * (i + 1);
        creatures[i].position[1] = 50 * (i + 1);
        creatures[i].setRotation(0);
        env.addObject(&creatures[i]);
    }

    // Setup food
    for (int i = 0; i < NUM_FOOD; ++i) {
        Food f = Food();
        f.reset(WIDTH, HEIGHT);
        food.push_back(f);
    }

    sf::Clock frame_clock;
    sf::Clock gen_clock;
    int frame_count = 0;

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            switch (event.type) {

                case sf::Event::Closed:
                    window.close();
                    break;

                case sf::Event::KeyPressed:
                    if (event.key.code == sf::Keyboard::W) {
                        creature.moveForward();
                    } else if (event.key.code == sf::Keyboard::S) {
                        printf("%s\n", "S PRESSED");
                    } else if (event.key.code == sf::Keyboard::A) {
                        creature.setRotation(creature.getRotation() + 5);
                    } else if (event.key.code == sf::Keyboard::D) {
                        creature.setRotation(creature.getRotation() - 5);
                    } else if (event.key.code == sf::Keyboard::P) {
                        launchConsole();
                        frame_clock.restart();
                    }
                    break;

                case sf::Event::MouseButtonPressed:
                    if (event.mouseButton.button == sf::Mouse::Right) {
                        sf::Vector2f position = static_cast<sf::Vector2f>(sf::Mouse::getPosition(window));
                    }
                    break;

                default:
                    break;
            }
        }

        window.clear();

        for (int i = 0; i < NUM_FOOD; ++i) {
            if (!food[i].isConsumed()) window.draw(food[i]);
        }

        // Debug creature
        creature.draw(&window);
        for (int i = 0; i < NUM_FOOD; ++i) {
            if (creature.isPointInFOV(food[i].getPosition())) {
                // printf("%f\n", creature.distanceToPoint(food[i].getPosition()));
            }
        }

        for (int i = 0; i < NUM_CREATURES; ++i) {
            creatures[i].process(&food);
            creatures[i].draw(&window);
        }

        // Handle generations
        if (gen_clock.getElapsedTime().asSeconds() > GENERATION_LENGTH_SECONDS) {
            std::cout << "Generation: " << ++gen_count << std::endl;
            writeOutData();
            // Reset food
            for (int i = 0; i < NUM_FOOD; ++i) {
                food[i].reset(WIDTH, HEIGHT);
            }

            // Sort by energy level
            std::sort(creatures, creatures + NUM_CREATURES, compareCreatures);

            // Select and mutate the top 50% of creatures
            Mutator mutator = Mutator();
            for (int i = 0; i < NUM_CREATURES/2; ++i) {
                printf("Creature %d  %f\n", i, creatures[i].getEnergy());
                mutator.setTarget(&creatures[i]);
                creatures[i].resetEnergy();
                creatures[i].position[0] = WIDTH / 2;
                creatures[i].position[1] = HEIGHT / 2;

                mutator.mutate(&creatures[NUM_CREATURES - 1 - i]);
                creatures[NUM_CREATURES - 1 - i].resetEnergy();
                creatures[NUM_CREATURES - 1 - i].position[0] = WIDTH / 2;
                creatures[NUM_CREATURES - 1 - i].position[1] = HEIGHT / 2;
            }

            gen_clock.restart();
        }

        window.display();

        sf::Time elapsed = frame_clock.restart();
        env.step(elapsed.asSeconds());
    }

    return 0;
}