int expose(t_all *all) { if (all->system.phase == 1) put_ui_to_window(all); if (all->system.phase == 4) see_stats(all); if (all->system.phase == 0) { mlx_string_put(all->system.mlx_p, all->system.mlx_w, 210, 250, 0xFFFFFF, "PRESS SPACE"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 450, 490, 0xFF0000, "rev 1.0"); } if (all->system.phase == 2) end_phase(all, 1); if (all->system.phase == 42) { mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 165, 0xFF0000, " AAAAAA AAAAAA"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 180, 0xFF0000, " SSSSSSSSSSSSSSSSSSS"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 195, 0xFF0000, " TTTTTTTTTTTTTTTTTTTTTTT"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 210, 0xFF0000, " EEEEEEEEEEEEEEEEEEEEEEEEE"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 225, 0xFF0000, "KKKKKKKKKKKKKKKKKKKKKKKKKKK"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 240, 0xFF0000, "###########################"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 255, 0xFF0000, " EEEEEEEEEEEEEEEEEEEEEEEEE"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 270, 0xFF0000, " NNNNNNNNNNNNNNNNNNNNNNN"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 285, 0xFF0000, " CCCCCCCCCCCCCCCCCCC"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 300, 0xFF0000, " UUUUUUUUUUUUUUU"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 315, 0xFF0000, " LLLLLLLLL"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 165, 330, 0xFF0000, " EEE"); } }
static void start_phase (GsmSession *session) { g_debug ("starting phase %d\n", session->phase); g_slist_free (session->pending_apps); session->pending_apps = NULL; if (session->pending_apps) { if (session->phase < GSM_SESSION_PHASE_APPLICATION) { session->timeout = g_timeout_add_seconds (GSM_SESSION_PHASE_TIMEOUT, phase_timeout, session); } } else end_phase (session); }
static void app_registered (GsmApp *app, gpointer data) { GsmSession *session = data; session->pending_apps = g_slist_remove (session->pending_apps, app); g_signal_handlers_disconnect_by_func (app, app_registered, session); if (!session->pending_apps) { if (session->timeout > 0) { g_source_remove (session->timeout); session->timeout = 0; } end_phase (session); } }
static gboolean phase_timeout (gpointer data) { GsmSession *session = data; GSList *a; session->timeout = 0; for (a = session->pending_apps; a; a = a->next) { g_warning ("Application '%s' failed to register before timeout", gsm_app_get_basename (a->data)); g_signal_handlers_disconnect_by_func (a->data, app_registered, session); /* FIXME: what if the app was filling in a required slot? */ } end_phase (session); return FALSE; }
int put_ui_to_window(t_all *all) { void *mlx_i; double x = 0; int y = 0; int i; if (all->system.phase == 1) { if (all->p1.energy > all->p1.energy_max) all->p1.energy = all->p1.energy_max; if (all->p1.energy < 0) all->p1.energy = 0; if (all->p2.hp > all->p2.hp_max) all->p2.hp = all->p2.hp_max; if (all->p2.hp < 0) all->p1.energy = 0; if (all->p1.hp > all->p1.hp_max) all->p1.hp = all->p1.hp_max; if (all->p1.hp < 0) all->p1.hp = 0; if (all->p2.energy > all->p2.energy_max) all->p2.energy = all->p2.energy_max; if (all->p2.energy < 0) all->p2.energy = 0; mlx_i = mlx_new_image(all->system.mlx_p, 500, 140); while (x < 500) { mlx_pixel_put_to_image(mlx_i, x, y, 0xFFFFFF); mlx_pixel_put_to_image(mlx_i, x, y + 20, 0xFFFFFF); mlx_pixel_put_to_image(mlx_i, x, y + 40, 0xFFFFFF); mlx_pixel_put_to_image(mlx_i, x, y + 90, 0xFFFFFF); mlx_pixel_put_to_image(mlx_i, x++, y + 139, 0xFFFFFF); } x = 0; while (y < 140) { mlx_pixel_put_to_image(mlx_i, x, y, 0xFFFFFF); if (y > 40) mlx_pixel_put_to_image(mlx_i, x + 250, y, 0xFFFFFF); mlx_pixel_put_to_image(mlx_i, x + 499, y++, 0xFFFFFF); } y = 0; while (((500000 / all->p1.energy_max) * all->p1.energy) / 1000 > x && x < 501) { i = 21; while (i < 40) mlx_pixel_put_to_image(mlx_i, x, y + i++, 0xFFFF00); ++x; } x = 0; while ((all->p1.hp / (all->p1.hp_max / 500)) > x && x < 501) { i = 1; while (i < 20) mlx_pixel_put_to_image(mlx_i, x, y + i++, 0xFF0000); ++x; } put_enemy_ui(all); mlx_put_image_to_window(all->system.mlx_p, all->system.mlx_w, mlx_i, 0, 360); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 230, 52, 0xFF0000, "ENEMY"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 200, 355, 0x00FFFF, "PLAYER (aka you)"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 85, 427, 0xFFFFFF, "Attack [1]"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 320, 420, 0xFFFFFF, "Special punch [2]"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 65, 470, 0xFFFFFF, "Use a bandage [3]"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 83, 485, 0xFFFF00, "60 energy"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 335, 435, 0xFFFF00, "40 energy"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 335, 476, 0xFFFFFF, "Meditation [4]"); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 240, 395, 0x555555, int_to_char(all->p1.energy)); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 237, 375, 0x555555, int_to_char(all->p1.hp)); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 235, 15, 0x555555, int_to_char(all->p2.energy)); mlx_string_put(all->system.mlx_p, all->system.mlx_w, 232, 35, 0x555555, int_to_char(all->p2.hp)); if (all->p1.meditate_last == 3) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 350, 350, 0xFFFFFF, "Meditation, 3 turns left"); if (all->p1.meditate_last == 2) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 350, 350, 0xFFFFFF, "Meditation, 2 turns left"); if (all->p1.meditate_last == 1) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 350, 350, 0xFFFFFF, "Meditation, 1 turns left"); if (all->p2.meditate_last == 3) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 20, 70, 0xFFFFFF, "Meditation, 3 turns left"); if (all->p2.meditate_last == 2) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 20, 70, 0xFFFFFF, "Meditation, 2 turns left"); if (all->p2.meditate_last == 1) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 20, 70, 0xFFFFFF, "Meditation, 1 turns left"); if (all->system.error_msg == 1) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 50, 250, 0xFF0000, "Not enough energy"); all->system.error_msg = 0; if (all->system.enemy_choice != 0) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 157, 200, 0xFFFFFF, all->p2.name); if (all->system.enemy_choice == 1) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 187, 200, 0xFFFFFF, " attacks !"); if (all->system.enemy_choice == 2) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 187, 200, 0xFFFFFF, " uses a special attack !"); if (all->system.enemy_choice == 3) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 187, 200, 0xFFFFFF, " heals himself !"); if (all->system.enemy_choice == 4) mlx_string_put(all->system.mlx_p, all->system.mlx_w, 187, 200, 0xFFFFFF, " uses meditation !"); } if (all->p2.hp <= 0 || all->p1.hp <= 0) end_phase(all, 0); }
int run_alps(int *steps) { int i,j,k,p; double temp_fitness[FITNESS_COUNT]; double temp_gene[GENE_COUNT]; int pareto_front[POP]; int pareto_count; begin = time(NULL); init_population(); // Initialise population. t = 0; goal_indiv = -1; mprintf(1, "preamble -> {expName -> %s, phaseCount -> %d, lobotomise -> %s, task -> %d, \n", exp_name, phase_count, lobotomise ? "True" : "False", task_index + 1); mprintf(1, "\ttmax -> %.2lf, fitnessType -> %d, runType -> %d, randomSeed -> %ld }\n", TIME_MAX, fitness_type, run_type, random_seed); mprintf(1, "alpsParams -> { layerCount -> %d, popPerLayer -> %d, " "popCount -> %d, mutProbability -> %.3f, maxSeconds -> %d }\n", LAYER_COUNT, POP_PER_LAYER, POP, mut_prob, MAX_SECONDS); fflush(stdout); for (p = 0, phase = 1; p < phase_count && elapsed_seconds() < MAX_SECONDS; p++, phase = p + 1) { if (p != 0) end_phase(p); start_phase(phase); for (i = 0; i < POP; i++) { // Evaluate every gene. evaluate(genes[i], fitness_matrix + FITNESS_INDEX(i)); } int met_goal = 0; for (j = 0; j < POP; j++) { if (is_goal_fitness(fitness_matrix + FITNESS_INDEX(j))) { met_goal = 1; goal_indiv = j; } } if (met_goal) continue; for (; t < MAX_OPT_STEPS && elapsed_seconds() < MAX_SECONDS; t++) { // Evaluate the pareto front for each layer. for (k = 0; k < LAYER_COUNT; k++) pareto_front_rowmajor(pareto_front + k * POP_PER_LAYER, fitness_matrix + k * FITNESS_COUNT * POP_PER_LAYER, POP_PER_LAYER, FITNESS_COUNT); // Grab a non-dominated individual. int a; // O(n) single pass to count and grab a random individual // that's on the pareto front. pareto_count=0; for (i = 0; i < POP; i++) if (pareto_front[i] && (rand() < 1./(double)++pareto_count)) a = i; k = LAYER_OF_INDIV(a); if (t % DISPLAY_FREQ == 0) { int n = FITNESS_INDEX(a); if (! quiet) printf("t = %5d, pi = %3d, a = %2d, k = %2d, N(PF) = %2d, f(a) = {%f, %f}, " "efail = %3d, esucc = %5d, secs = %4ld\n", t, a, ages[a], k, pareto_count, fitness_matrix[n], fitness_matrix[n + 1], eval_fail_count, eval_succ_count, elapsed_seconds()); fflush(stdout); } if (t % reset_freq == 0) { // Reset the bottom layer. for (i = 0; i < POP_PER_LAYER; i++) { // Try to dislogde in the layer above if it's in the pareto front. if (pareto_front[i]) try_dislodge(genes[i], fitness_matrix + FITNESS_LINDEX(0, i), ages[i], 1); init_gene(genes[i]); ages[i] = 0; } for (i = 0; i < POP_PER_LAYER; i++) evaluate(genes[i], fitness_matrix + FITNESS_INDEX(i)); pareto_front_rowmajor(pareto_front, fitness_matrix, POP_PER_LAYER, FITNESS_COUNT); } copy(genes[a], temp_gene); mutate(temp_gene); int age = t/POP; evaluate(temp_gene, temp_fitness); int new_i = try_dislodge(temp_gene, temp_fitness, age, k); if (is_goal_fitness(temp_fitness)) { if (new_i < 0) { printf("warning: goal individual not able to dislodge anyone in layer %d and up.\n", k); } goal_indiv = new_i; break; /* Goto next phase */ } } } if (t == MAX_OPT_STEPS || elapsed_seconds() > MAX_SECONDS) alps_status = ALPS_FAIL; else alps_status = ALPS_SUCC; end_phase(phase - 1); if (steps) *steps = t; return alps_status; }