int set_form_page(FORM *f, int page) { if (!f || !ValidPage(f, page)) return (E_BAD_ARGUMENT); if (!Status(f, POSTED)) { P(f) = page; C(f) = _first_active(f); return (E_OK); } if (Status(f, DRIVER)) return (E_BAD_STATE); if (page != P(f)) { if (_validate(f)) { int v; term_field(f); term_form(f); v = _set_form_page(f, page, (FIELD *) 0); init_form(f); init_field(f); (void) _update_current(f); return (v); } else return (E_INVALID_FIELD); } return (E_OK); }
void CSpiel::start_new_game(GAMEMODE gamemode){ init_field(); set_seeds(gamemode); for (int n = 0; n < PLAYER_MAX; n++){ CSpiel::m_player[n].init(this, n); } }
int post_form(FORM *f) { int x, y, v; if (!f) return (E_BAD_ARGUMENT); if (Status(f, POSTED)) return (E_POSTED); if (!f->field) return (E_NOT_CONNECTED); getmaxyx(Sub(f), y, x); if (f->rows > y || f->cols > x) return (E_NO_ROOM); v = _set_form_page(f, P(f), C(f)); if (v != E_OK) return (v); Set(f, POSTED); init_form(f); init_field(f); (void) _update_current(f); return (E_OK); }
gameobj::gameobj(int x, int y, int typenum) { this->size_x = x; this->size_y = y; this->typenum = typenum; init_objnum(); init_field(); init_objs(); point = objs; }
int main(int argc, const char** argv) { int ret; arg_t args; read_args(argc, argv, &args); printf("args width:%i height:%i rounds:%i hotspots:%s selection:%s\n", args.width_field, args.height_field, args.n_rounds, args.hotspot_filename, argc >= 6 ? args.selection_filename : "empty"); hotspot_vector_t hotspots; hotspot_vector_t coordinates; if (ret = read_hotspots(args.hotspot_filename, &hotspots)) { printf("could not read hotspots from: %s (%d)", args.hotspot_filename, ret); return -1; } field_t field; init_field(args, &field); int current_round = 0; set_hotspots(&field, current_round, &hotspots); while (current_round++ < args.n_rounds) { /*print_field(&field);*/ simulate_round(&field); // double buffering volatile field_value_t * temp = field.old_values; field.old_values = field.new_values; field.new_values = temp; set_hotspots(&field, current_round, &hotspots); } if (argc >= 6) { if (ret = read_coordinates(args.selection_filename, &coordinates)) { printf("could not read coordinates from: %s (%d)", args.selection_filename, ret); return -1; } print_coordinate_values(&field, &coordinates);} else { print_field(&field); } return 0; }
static void len_field(t_flags *f, char *comp) { int i; i = 0; while (comp[i] != '\0') { i = init_field(f, comp, i); if (i == -1) return ; i++; } }
/* * Initialisieren der Datenstruktur zum Abbilden des programminternen * Berechnungsstatus */ void init_values(struct state* state) { memset(state, 0, sizeof(struct state)); /* Initialisieren der Farbskala */ init_colorclasses(state); /* Anzahl der Trajektorinedateien auslesen */ state->list_max = count_files(); /* Speicherplatz reservieren */ state->list = (char**)calloc(sizeof(char*), state->list_max); /* Minimale und maximale Koordinatenwerte der in den fuer die * Berechnung zu verwendenden Trajektorienaufpunkte bestimmen */ get_lo_la_min_max(state); /* Bestimmen der benoetigten Netzelementeanzahl (abhaengig von * Netzelementgroesse (Aufloesung) und der Groesse des * Berechnungsgebiets (lo_min, la_min, lo_max, la_max)) */ count_squares(state); /* Errechnen der Gesamtanzahl der Netzelemente */ state->field_max = state->x_field * state->y_field; /* Reservieren des benoetigten Speicherplatzes fuer die * Trajektoriendichte-Speicherstruktur */ state->field_grid = (double*)calloc(sizeof(double), state->field_max); /* Berechnungsnetz Initialisieren */ init_field(state->field_grid, state->field_max); /* Kann gewuenschte Ausgabedatei angelegt werden? */ if (!(state->fh = fopen(get_string(FILENAME), "w"))) { printf("Couldn't write file %s!\n", get_string(FILENAME)); exit(1); } }
int main() { CONTOUR[CONTOUR_FIRE] = -20; CONTOUR[CONTOUR_LIFE] = 20; CONTOUR[CONTOUR_PLASMA] = 40; init(); init_field(); FrameRateLockTimer timer(1/20.0); while (true) { timer.lock(); events(); step(); glClear(GL_COLOR_BUFFER_BIT); draw(); SDL_GL_SwapBuffers(); } }
static void * cloudlife_init (Display *dpy, Window window) { struct state *st = (struct state *) calloc (1, sizeof(*st)); Bool tmp = True; st->dpy = dpy; st->window = window; st->field = init_field(st); #ifdef TIME_ME st->start_time = time(NULL); #endif st->cycle_delay = get_integer_resource(st->dpy, "cycleDelay", "Integer"); st->cycle_colors = get_integer_resource(st->dpy, "cycleColors", "Integer"); st->ncolors = get_integer_resource(st->dpy, "ncolors", "Integer"); st->density = (get_integer_resource(st->dpy, "initialDensity", "Integer") % 100 * 256)/100; XGetWindowAttributes(st->dpy, st->window, &st->xgwa); if (st->cycle_colors) { st->colors = (XColor *) xrealloc(st->colors, sizeof(XColor) * (st->ncolors+1)); make_smooth_colormap (st->xgwa.screen, st->xgwa.visual, st->xgwa.colormap, st->colors, &st->ncolors, True, &tmp, True); } st->gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap, "foreground", "Foreground"); st->fgc = XCreateGC(st->dpy, st->window, GCForeground, &st->gcv); st->gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap, "background", "Background"); st->bgc = XCreateGC(st->dpy, st->window, GCForeground, &st->gcv); return st; }
int set_current_field(FORM *f, FIELD *c) { if (!f || !c || c->form != f) return (E_BAD_ARGUMENT); if (!Opt(c, O_ACTIVE) || !Opt(c, O_VISIBLE)) return (E_REQUEST_DENIED); if (!Status(f, POSTED)) { C(f) = c; P(f) = c->page; return (E_OK); } if (Status(f, DRIVER)) return (E_BAD_STATE); if (c != C(f)) { if (_validate(f)) { int v; term_field(f); if (c -> page != P(f)) { /* page change */ term_form(f); v = _set_form_page(f, c->page, c); init_form(f); } else v = _set_current_field(f, c); init_field(f); (void) _update_current(f); return (v); } else return (E_INVALID_FIELD); } return (E_OK); }
int main(int ac, char *av[]) { bool show_only; extern char *Scorefile; int score_wfd; /* high score writable file descriptor */ int score_err = 0; /* hold errno from score file open */ int ch; extern int optind; gid_t gid; #ifdef FANCY char *sp; #endif if ((score_wfd = open(Scorefile, O_RDWR)) < 0) score_err = errno; /* revoke privs */ gid = getgid(); setresgid(gid, gid, gid); show_only = FALSE; while ((ch = getopt(ac, av, "srajt")) != -1) switch (ch) { case 's': show_only = TRUE; break; case 'r': Real_time = TRUE; /* Could be a command-line option */ tv.tv_sec = 3; tv.tv_usec = 0; FD_ZERO(&rset); break; case 'a': Start_level = 4; break; case 'j': Jump = TRUE; break; case 't': Teleport = TRUE; break; case '?': default: usage(); } ac -= optind; av += optind; if (ac > 1) usage(); if (ac == 1) { Scorefile = av[0]; if (score_wfd >= 0) close(score_wfd); /* This file requires no special privileges. */ if ((score_wfd = open(Scorefile, O_RDWR)) < 0) score_err = errno; #ifdef FANCY sp = strrchr(Scorefile, '/'); if (sp == NULL) sp = Scorefile; if (strcmp(sp, "pattern_roll") == 0) Pattern_roll = TRUE; else if (strcmp(sp, "stand_still") == 0) Stand_still = TRUE; if (Pattern_roll || Stand_still) Teleport = TRUE; #endif } if (show_only) { show_score(); exit(0); } if (score_wfd < 0) { warnx("%s: %s; no scores will be saved", Scorefile, strerror(score_err)); sleep(1); } initscr(); signal(SIGINT, quit); cbreak(); noecho(); nonl(); if (LINES != Y_SIZE || COLS != X_SIZE) { if (LINES < Y_SIZE || COLS < X_SIZE) { endwin(); errx(1, "Need at least a %dx%d screen", Y_SIZE, X_SIZE); } delwin(stdscr); stdscr = newwin(Y_SIZE, X_SIZE, 0, 0); } srandomdev(); do { init_field(); for (Level = Start_level; !Dead; Level++) { make_level(); play_level(); } move(My_pos.y, My_pos.x); printw("AARRrrgghhhh...."); refresh(); score(score_wfd); } while (another()); quit(0); /* NOT REACHED */ }
void play_single_player() { time_stepms = 4; time_step = 0.004f; spbg = get_image("spbg.png"); for(;;) { Uint32 last_tick = SDL_GetTicks(); first_tick = last_tick; last_avalanche = last_tick; init_field(); SDL_BlitSurface(spbg, 0, screen, 0); while(state == GS_NEUTRAL) { SDL_Event event; now = SDL_GetTicks(); while(now >= last_tick + time_stepms) { /* Skip delays larger than 5s */ if(now - last_tick > 5000) last_tick = now - 5000; last_tick += time_stepms; game_tick(now - last_tick < time_stepms); } while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: exit(EXIT_SUCCESS); break; case SDL_JOYBUTTONDOWN: if (event.jbutton.button == 15 || event.jbutton.button == 0)/* Code same as SDLK_SPACE */ { if(state == GS_NEUTRAL) { if(level < sizeof(levels) / sizeof(levels[0])) { bonus += levels[level].shoot_bonus; shoot(&p, random_bubble(&p), (levels[level].mode == GM_INV_GRAVITY) ? 400 : -1); } else shoot(&p, random_bubble(&p), -1); } } if (event.jbutton.button == 13 || event.jbutton.button == 2)/* Code same as SDLK_ESCAPE */ { if(level == 255) level = saved_level; #if LINUX || DARWIN if(getenv("HOME")) { char confpath[4096]; strcpy(confpath, getenv("HOME")); strcat(confpath, "/.pengupoprc"); int fd = open(confpath, O_WRONLY | O_CREAT, 0600); if(fd != -1) { lseek(fd, 32, SEEK_SET); write(fd, &level, 1); close(fd); } } #elif defined(WIN32) { HKEY k_config; if(ERROR_SUCCESS == RegCreateKey(HKEY_CURRENT_USER, "Software\\Junoplay.com\\Pengupop\\Config", &k_config)) { char str[64]; snprintf(str, sizeof(str), "%d", (level ^ 0x7236143)); str[63] = 0; RegSetValueEx(k_config, "bananas", 0, REG_SZ, str, strlen(str)); } } #endif return; } break; case SDL_JOYAXISMOTION: if ((event.jaxis.value > -3200) || (event.jaxis.value < 3200)) { switch (event.jaxis.axis) { case 0: if (p.right == -1)/* Code same as SDL_KEYUP:SDLK_LEFT */ p.right = 0; if (p.right == 1)/* Code same as SDL_KEYUP:SDLK_RIGHT */ p.right = 0; break; } } if ((event.jaxis.value < -3200) || (event.jaxis.value > 3200)) { switch (event.jaxis.axis) { case 0: if (event.jaxis.value < -22000)/* Code same as SDL_KEYDOWN:SDLK_LEFT */ p.right = -1; if (event.jaxis.value > 22000)/* Code same as SDL_KEYDOWN:SDLK_RIGHT */ p.right = 1; break; } } break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_q: case SDLK_ESCAPE: if(level == 255) level = saved_level; #if LINUX || DARWIN if(getenv("HOME")) { char confpath[4096]; strcpy(confpath, getenv("HOME")); strcat(confpath, "/.pengupoprc"); int fd = open(confpath, O_WRONLY | O_CREAT, 0600); if(fd != -1) { lseek(fd, 32, SEEK_SET); write(fd, &level, 1); close(fd); } } #elif defined(WIN32) { HKEY k_config; if(ERROR_SUCCESS == RegCreateKey(HKEY_CURRENT_USER, "Software\\Junoplay.com\\Pengupop\\Config", &k_config)) { char str[64]; snprintf(str, sizeof(str), "%d", (level ^ 0x7236143)); str[63] = 0; RegSetValueEx(k_config, "bananas", 0, REG_SZ, str, strlen(str)); } } #endif return; case SDLK_x: case SDLK_RETURN: case SDLK_SPACE: case SDLK_UP: if(state == GS_NEUTRAL) { if(level < sizeof(levels) / sizeof(levels[0])) { bonus += levels[level].shoot_bonus; shoot(&p, random_bubble(&p), (levels[level].mode == GM_INV_GRAVITY) ? 400 : -1); } else shoot(&p, random_bubble(&p), -1); } break; case SDLK_LEFT: p.right = -1; break; case SDLK_RIGHT: p.right = 1; break; case 's': sound_enable = !sound_enable; break; case 'f': #ifndef WIN32 SDL_WM_ToggleFullScreen(screen); #else if(fullscreen) screen = SDL_SetVideoMode(width, height, 0, SDL_SWSURFACE); else screen = SDL_SetVideoMode(width, height, 0, SDL_SWSURFACE | SDL_FULLSCREEN); SDL_BlitSurface(spbg, 0, screen, 0); p.dirty_minx = 0; p.dirty_miny = 0; p.dirty_maxx = max_field_width * 32; p.dirty_maxy = 440; #endif fullscreen = !fullscreen; break; default:; } break; case SDL_KEYUP: switch(event.key.keysym.sym) { case SDLK_LEFT: if(p.right == -1) p.right = 0; break; case SDLK_RIGHT: if(p.right == 1) p.right = 0; break; default:; } break; } } if(p.evil_bubble_count && !next_evil) { int i = 0; while(p.evil_bubble_count && i < sizeof(p.mbubbles) / sizeof(p.mbubbles[0])) { if(p.mbubbles[i].color != 0 || p.mbubbles[i].lastpaintx != INT_MIN) { ++i; continue; } float rand = sin(pow(p.evil_bubble_seed++, 4.5)) * 0.5 + 0.5; float angle = rand * 60 - 30; p.mbubbles[i].falling = 0; p.mbubbles[i].velx = sin(angle / 180 * M_PI) * bubble_speed; p.mbubbles[i].vely = -cos(angle / 180 * M_PI) * bubble_speed; p.mbubbles[i].x = 112.0f; p.mbubbles[i].y = 400.0f; p.mbubbles[i].color = p.evil_bubbles[0]; --p.evil_bubble_count; memmove(p.evil_bubbles, &p.evil_bubbles[1], p.evil_bubble_count); next_evil = 10; break; } } if(next_evil) --next_evil; SDL_UpdateRect(screen, 0, 0, 0, 0); } Uint32 message_until = now + 2000; p.right = 0; for(;;) { now = SDL_GetTicks(); SDL_Event event; while(SDL_PollEvent(&event)) { if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_SPACE || event.key.keysym.sym == SDLK_RETURN) message_until = now; } } while(now >= last_tick + time_stepms) { /* Skip delays larger than 5s */ if(now - last_tick > 5000) last_tick = now - 5000; last_tick += time_stepms; game_tick(now - last_tick < time_stepms); } if(now > message_until) break; SDL_UpdateRect(screen, 0, 0, 0, 0); } if(state == GS_SHINE_GET) ++level; if(level == sizeof(levels) / sizeof(levels[0])) { level = 0; for(;;) { SDL_Event event; while(SDL_PollEvent(&event)) { if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_SPACE || event.key.keysym.sym == SDLK_RETURN) return; } } SDL_BlitSurface(logo, 0, screen, 0); print_string(0, 320, 250, L"Congratulations!", 1); print_string(0, 320, 290, L"That was all", 1); SDL_UpdateRect(screen, 0, 0, 0, 0); } return; } } }
static void * intermomentary_init (Display *dpy, Window window) { struct state *st = (struct state *) calloc (1, sizeof(*st)); #ifdef TIME_ME time_t start_time = time(NULL); #endif int tempx; XGCValues gcv; st->dpy = dpy; st->window = window; XGetWindowAttributes(dpy, window, &st->xgwa); st->ncolors = get_integer_resource (st->dpy, "colors", "Colors"); st->ncolors++; st->colors = (XColor *) malloc(sizeof(*st->colors) * (st->ncolors+1)); gcv.foreground = get_pixel_resource(dpy, st->xgwa.colormap, "foreground", "Foreground"); gcv.background = get_pixel_resource(dpy, st->xgwa.colormap, "background", "Background"); { XColor fgc, bgc; int fgh, bgh; double fgs, fgv, bgs, bgv; fgc.pixel = gcv.foreground; bgc.pixel = gcv.background; XQueryColor (st->dpy, st->xgwa.colormap, &fgc); XQueryColor (st->dpy, st->xgwa.colormap, &bgc); rgb_to_hsv (fgc.red, fgc.green, fgc.blue, &fgh, &fgs, &fgv); rgb_to_hsv (bgc.red, bgc.green, bgc.blue, &bgh, &bgs, &bgv); #if 0 bgh = fgh; bgs = fgs; bgv = fgv / 10.0; #endif make_color_ramp (st->dpy, st->xgwa.colormap, bgh, bgs, bgv, fgh, fgs, fgv, st->colors, &st->ncolors, False, /* closed */ True, False); } st->f = init_field(); st->f->height = st->xgwa.height; st->f->width = st->xgwa.width; st->f->visdepth = st->xgwa.depth; st->draw_delay = (get_integer_resource(dpy, "drawDelay", "Integer")); st->f->maxrider = (get_integer_resource(dpy, "maxRiders", "Integer")); st->f->maxradius = (get_integer_resource(dpy, "maxRadius", "Integer")); st->f->initial_discs = (get_integer_resource(dpy, "numDiscs", "Integer")); if (st->f->initial_discs <= 10) { fprintf(stderr, "%s: Initial discs must be greater than 10\n", progname); exit (1); } if (st->f->maxradius <= 30) { fprintf(stderr, "%s: Max radius must be greater than 30\n", progname); exit (1); } if (st->f->maxrider <= 10) { fprintf(stderr, "%s: Max riders must be greater than 10\n", progname); exit (1); } st->fgc = XCreateGC(dpy, window, GCForeground, &gcv); st->copygc = XCreateGC(dpy, window, GCForeground, &gcv); st->f->fgcolor = gcv.foreground; st->f->bgcolor = gcv.background; /* Initialize stuff */ build_img(dpy, window, st->f); for (tempx = 0; tempx < st->f->initial_discs; tempx++) { float fx, fy, x, y, r; int bt; /* Arrange in anti-collapsing circle */ fx = 0.4 * st->f->width * cos((2 * M_PI) * tempx / st->f->initial_discs); fy = 0.4 * st->f->height * sin((2 * M_PI) * tempx / st->f->initial_discs); x = frand(st->f->width / 2) + fx; y = frand(st->f->height / 2) + fy; r = 5 + frand(st->f->maxradius); bt = 1; if ((random() % 100) < 50) bt = -1; make_disc(st->f, x, y, bt * fx / 1000.0, bt * fy / 1000.0, r); } return st; }
int main(int argc, char **argv) { const char *word; bool show_only; int score_wfd; /* high score writable file descriptor */ int score_err = 0; /* hold errno from score file open */ int maximum = 0; int ch, i; score_wfd = open(Scorefile, O_RDWR); if (score_wfd < 0) score_err = errno; else if (score_wfd < 3) exit(1); /* Revoke setgid privileges */ setgid(getgid()); show_only = false; Num_games = 1; while ((ch = getopt(argc, argv, "Aajnrst")) != -1) { switch (ch) { case 'A': Auto_bot = true; break; case 'a': Start_level = 4; break; case 'j': Jump = true; break; case 'n': Num_games++; break; case 'r': Real_time = true; break; case 's': show_only = true; break; case 't': Teleport = true; break; default: errx(1, "Usage: robots [-Aajnrst] [maximum] [scorefile]"); break; } } for (i = optind; i < argc; i++) { word = argv[i]; if (isdigit((unsigned char)word[0])) { maximum = atoi(word); } else { Scorefile = word; Max_per_uid = maximum; if (score_wfd >= 0) close(score_wfd); score_wfd = open(Scorefile, O_RDWR); if (score_wfd < 0) score_err = errno; #ifdef FANCY word = strrchr(Scorefile, '/'); if (word == NULL) word = Scorefile; if (strcmp(word, "pattern_roll") == 0) Pattern_roll = true; else if (strcmp(word, "stand_still") == 0) Stand_still = true; if (Pattern_roll || Stand_still) Teleport = true; #endif } } if (show_only) { show_score(); exit(0); /* NOTREACHED */ } if (score_wfd < 0) { errno = score_err; warn("%s", Scorefile); warnx("High scores will not be recorded!"); sleep(2); } if (!initscr()) errx(0, "couldn't initialize screen"); signal(SIGINT, quit); cbreak(); noecho(); nonl(); if (LINES != Y_SIZE || COLS != X_SIZE) { if (LINES < Y_SIZE || COLS < X_SIZE) { endwin(); printf("Need at least a %dx%d screen\n", Y_SIZE, X_SIZE); exit(1); } delwin(stdscr); stdscr = newwin(Y_SIZE, X_SIZE, 0, 0); } srandom(time(NULL)); if (Real_time) signal(SIGALRM, move_robots); do { while (Num_games--) { init_field(); for (Level = Start_level; !Dead; Level++) { make_level(); play_level(); if (Auto_bot) sleep(1); } move(My_pos.y, My_pos.x); printw("AARRrrgghhhh...."); refresh(); if (Auto_bot) sleep(1); score(score_wfd); if (Auto_bot) sleep(1); refresh(); } Num_games = 1; } while (!Auto_bot && another()); quit(0); /* NOTREACHED */ return(0); }
int aform_do(OBJECT *db,int start, int *cured, int movob) { int edob,nob,which,cont; int idx,mx,my,mb,ks,kr,br; nob=init_field(db,start); edob=0; cont=TRUE; wind_update(BEG_UPDATE); wind_update(BEG_MCTRL); while (cont) { if ( nob ne 0 and edob ne nob ) { edob=nob; nob=0; objc_edit(db,edob,0,&idx,ED_INIT); } which=evnt_multi( MU_KEYBD|MU_BUTTON, 2,1,1, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, &mx,&my,&mb, &ks,&kr,&br); if (which & MU_KEYBD) { cont=form_keybd(db,edob,nob,kr,&nob,&kr); if (kr) objc_edit(db,edob,kr,&idx,ED_CHAR); } if (which & MU_BUTTON) { nob=objc_find(db,0,MAX_DEPTH,mx,my); if (nob eq -1) { ping; nob=0; othw cont = (movob and nob eq movob) ? FALSE : form_button(db,nob,br,&nob); } } if (!cont or (nob ne 0 and nob ne edob) ) objc_edit(db,edob,0,&idx,ED_END); } wind_update(END_MCTRL); wind_update(END_UPDATE); if (cured) *cured=edob; return nob; }
int main(int argc, char *argv[]){ int n_species; int n_load; int n_used; int flag_used[N_GADGET_TYPE]; char species_name[256]; double h_Hubble; double n_spec; double redshift; int i_species; char n_string[64]; int n[3]; double L[3]; FILE *fp_1D; FILE *fp_2D; cosmo_info *cosmo; field_info *field[N_GADGET_TYPE]; field_info *field_norm[N_GADGET_TYPE]; plist_info plist_header; plist_info plist; FILE *fp; int i_temp; int n_temp; double *k_temp; double *kmin_temp; double *kmax_temp; double *P_temp; size_t *n_mode_temp; double *sigma_P_temp; double *shot_noise_temp; double *dP_temp; int snapshot_number; int i_compute; int distribution_scheme; double k_min_1D; double k_max_1D; double k_min_2D; double k_max_2D; int n_k_1D; int n_k_2D; double *k_1D; double *P_k_1D; double *dP_k_1D; int *n_modes_1D; double *P_k_2D; double *dP_k_2D; int *n_modes_2D; int n_groups=1; double dk_1D; double dk_2D; char *grid_identifier; // Initialization -- MPI etc. SID_init(&argc,&argv,NULL,NULL); // Parse arguments int grid_size; char filename_in_root[MAX_FILENAME_LENGTH]; char filename_out_root[MAX_FILENAME_LENGTH]; strcpy(filename_in_root, argv[1]); snapshot_number=(int)atoi(argv[2]); strcpy(filename_out_root, argv[3]); grid_size =(int)atoi(argv[4]); if(!strcmp(argv[5],"ngp") || !strcmp(argv[5],"NGP")) distribution_scheme=MAP2GRID_DIST_NGP; else if(!strcmp(argv[5],"cic") || !strcmp(argv[5],"CIC")) distribution_scheme=MAP2GRID_DIST_CIC; else if(!strcmp(argv[5],"tsc") || !strcmp(argv[5],"TSC")) distribution_scheme=MAP2GRID_DIST_TSC; else if(!strcmp(argv[5],"d12") || !strcmp(argv[5],"D12")) distribution_scheme=MAP2GRID_DIST_DWT12; else if(!strcmp(argv[5],"d20") || !strcmp(argv[5],"D20")) distribution_scheme=MAP2GRID_DIST_DWT20; else SID_trap_error("Invalid distribution scheme {%s} specified.",ERROR_SYNTAX,argv[5]); SID_log("Smoothing Gadget file {%s;snapshot=#%d} to a %dx%dx%d grid with %s kernel...",SID_LOG_OPEN|SID_LOG_TIMER, filename_in_root,snapshot_number,grid_size,grid_size,grid_size,argv[5]); // Initialization -- fetch header info SID_log("Reading Gadget header...",SID_LOG_OPEN); gadget_read_info fp_gadget; int flag_filefound=init_gadget_read(filename_in_root,snapshot_number,&fp_gadget); int flag_multifile=fp_gadget.flag_multifile; int flag_file_type=fp_gadget.flag_file_type; gadget_header_info header =fp_gadget.header; double box_size =(double)(header.box_size); size_t *n_all =(size_t *)SID_calloc(sizeof(size_t)*N_GADGET_TYPE); size_t n_total; if(flag_filefound){ if(SID.I_am_Master){ FILE *fp_in; char filename[MAX_FILENAME_LENGTH]; int block_length_open; int block_length_close; set_gadget_filename(&fp_gadget,0,filename); fp_in=fopen(filename,"r"); fread_verify(&block_length_open, sizeof(int),1,fp_in); fread_verify(&header, sizeof(gadget_header_info),1,fp_in); fread_verify(&block_length_close,sizeof(int),1,fp_in); fclose(fp_in); if(block_length_open!=block_length_close) SID_trap_error("Block lengths don't match (ie. %d!=%d).",ERROR_LOGIC,block_length_open,block_length_close); } SID_Bcast(&header,sizeof(gadget_header_info),MASTER_RANK,SID.COMM_WORLD); redshift=header.redshift; h_Hubble=header.h_Hubble; box_size=header.box_size; if(SID.n_proc>1) n_load=1; else n_load=header.n_files; for(i_species=0,n_total=0,n_used=0;i_species<N_GADGET_TYPE;i_species++){ n_all[i_species]=(size_t)header.n_all_lo_word[i_species]+((size_t)header.n_all_hi_word[i_species])<<32; n_total+=n_all[i_species]; if(n_all[i_species]>0){ n_used++; flag_used[i_species]=TRUE; } else flag_used[i_species]=FALSE; } // Initialize cosmology double box_size =((double *)ADaPS_fetch(plist.data,"box_size"))[0]; double h_Hubble =((double *)ADaPS_fetch(plist.data,"h_Hubble"))[0]; double redshift =((double *)ADaPS_fetch(plist.data,"redshift"))[0]; double expansion_factor=((double *)ADaPS_fetch(plist.data,"expansion_factor"))[0]; double Omega_M =((double *)ADaPS_fetch(plist.data,"Omega_M"))[0]; double Omega_Lambda =((double *)ADaPS_fetch(plist.data,"Omega_Lambda"))[0]; double Omega_k =1.-Omega_Lambda-Omega_M; double Omega_b=0.; // not needed, so doesn't matter double f_gas =Omega_b/Omega_M; double sigma_8=0.; // not needed, so doesn't matter double n_spec =0.; // not needed, so doesn't matter char cosmo_name[16]; sprintf(cosmo_name,"Gadget file's"); init_cosmo(&cosmo, cosmo_name, Omega_Lambda, Omega_M, Omega_k, Omega_b, f_gas, h_Hubble, sigma_8, n_spec); } SID_log("Done.",SID_LOG_CLOSE); grid_identifier=(char *)SID_calloc(GRID_IDENTIFIER_SIZE*sizeof(char)); // Only process if there are >0 particles present if(n_used>0){ // Loop over ithe real-space and 3 redshift-space frames int i_write; int i_run; int n_run; int n_grids_total; n_grids_total=4; // For now, hard-wire real-space density and velocity grids only n_run=1; // For now, hard-wire real-space calculation only for(i_run=0,i_write=0;i_run<n_run;i_run++){ // Read catalog int n_grid; char i_run_identifier[8]; switch(i_run){ case 0: SID_log("Processing real-space ...",SID_LOG_OPEN|SID_LOG_TIMER); sprintf(i_run_identifier,"r"); n_grid=4; break; case 1: SID_log("Processing v_x redshift space...",SID_LOG_OPEN|SID_LOG_TIMER); sprintf(i_run_identifier,"x"); n_grid=1; break; case 2: SID_log("Processing v_y redshift space...",SID_LOG_OPEN|SID_LOG_TIMER); sprintf(i_run_identifier,"y"); n_grid=1; break; case 3: SID_log("Processing v_z redsift space...",SID_LOG_OPEN|SID_LOG_TIMER); sprintf(i_run_identifier,"z"); n_grid=1; break; } // For each i_run case, loop over the fields we want to produce int i_grid; for(i_grid=0;i_grid<n_grid;i_grid++){ char i_grid_identifier[8]; switch(i_grid){ case 0: SID_log("Processing density grid ...",SID_LOG_OPEN|SID_LOG_TIMER); sprintf(i_grid_identifier,"rho"); break; case 1: SID_log("Processing v_x velocity grid...",SID_LOG_OPEN|SID_LOG_TIMER); sprintf(i_grid_identifier,"v_x"); break; case 2: SID_log("Processing v_y velocity grid...",SID_LOG_OPEN|SID_LOG_TIMER); sprintf(i_grid_identifier,"v_y"); break; case 3: SID_log("Processing v_z velocity grid...",SID_LOG_OPEN|SID_LOG_TIMER); sprintf(i_grid_identifier,"v_z"); break; } // Initialize the field that will hold the grid int n[]={grid_size,grid_size,grid_size}; double L[]={box_size, box_size, box_size}; int i_init; for(i_species=0;i_species<N_GADGET_TYPE;i_species++){ if(flag_used[i_species]){ field[i_species] =(field_info *)SID_malloc(sizeof(field_info)); field_norm[i_species]=(field_info *)SID_malloc(sizeof(field_info)); init_field(3,n,L,field[i_species]); init_field(3,n,L,field_norm[i_species]); i_init=i_species; } else{ field[i_species] =NULL; field_norm[i_species]=NULL; } } // Loop over all the files that this rank will read int i_load; for(i_load=0;i_load<n_load;i_load++){ if(n_load>1) SID_log("Processing file No. %d of %d...",SID_LOG_OPEN|SID_LOG_TIMER,i_load+1,n_load); // Initialization -- read gadget file GBPREAL mass_array[N_GADGET_TYPE]; init_plist(&plist,&((field[i_init])->slab),GADGET_LENGTH,GADGET_MASS,GADGET_VELOCITY); char filename_root[MAX_FILENAME_LENGTH]; read_gadget_binary_local(filename_in_root, snapshot_number, i_run, i_load, n_load, mass_array, &(field[i_init]->slab), cosmo, &plist); // Generate power spectra for(i_species=0;i_species<plist.n_species;i_species++){ // Determine how many particles of species i_species there are if(n_all[i_species]>0){ // Fetch the needed information size_t n_particles; size_t n_particles_local; int flag_alloc_m; GBPREAL *x_particles_local; GBPREAL *y_particles_local; GBPREAL *z_particles_local; GBPREAL *vx_particles_local; GBPREAL *vy_particles_local; GBPREAL *vz_particles_local; GBPREAL *m_particles_local; GBPREAL *v_particles_local; GBPREAL *w_particles_local; n_particles =((size_t *)ADaPS_fetch(plist.data,"n_all_%s",plist.species[i_species]))[0]; n_particles_local=((size_t *)ADaPS_fetch(plist.data,"n_%s", plist.species[i_species]))[0]; x_particles_local= (GBPREAL *)ADaPS_fetch(plist.data,"x_%s", plist.species[i_species]); y_particles_local= (GBPREAL *)ADaPS_fetch(plist.data,"y_%s", plist.species[i_species]); z_particles_local= (GBPREAL *)ADaPS_fetch(plist.data,"z_%s", plist.species[i_species]); vx_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"vx_%s", plist.species[i_species]); vy_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"vy_%s", plist.species[i_species]); vz_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"vz_%s", plist.species[i_species]); if(ADaPS_exist(plist.data,"M_%s",plist.species[i_species])){ flag_alloc_m=FALSE; m_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"M_%s",plist.species[i_species]); } else{ flag_alloc_m=TRUE; m_particles_local=(GBPREAL *)SID_malloc(n_particles_local*sizeof(GBPREAL)); int i_particle; for(i_particle=0;i_particle<n_particles_local;i_particle++) m_particles_local[i_particle]=mass_array[i_species]; } // Decide the map_to_grid() mode int mode; if(n_load==1) mode=MAP2GRID_MODE_DEFAULT; else if(i_load==0 || n_load==1) mode=MAP2GRID_MODE_DEFAULT|MAP2GRID_MODE_NONORM; else if(i_load==(n_load-1)) mode=MAP2GRID_MODE_NOCLEAN; else mode=MAP2GRID_MODE_NOCLEAN|MAP2GRID_MODE_NONORM; // Set the array that will weight the grid field_info *field_i; field_info *field_norm_i; double factor; switch(i_grid){ case 0: v_particles_local=m_particles_local; w_particles_local=NULL; field_i =field[i_species]; field_norm_i =NULL; mode|=MAP2GRID_MODE_APPLYFACTOR; factor=pow((double)grid_size/box_size,3.); break; case 1: v_particles_local=vx_particles_local; w_particles_local=m_particles_local; field_i =field[i_species]; field_norm_i =field_norm[i_species]; factor=1.; break; case 2: v_particles_local=vy_particles_local; w_particles_local=m_particles_local; field_i =field[i_species]; field_norm_i =field_norm[i_species]; factor=1.; break; case 3: v_particles_local=vz_particles_local; w_particles_local=m_particles_local; field_i =field[i_species]; field_norm_i =field_norm[i_species]; factor=1.; break; } // Generate grid map_to_grid(n_particles_local, x_particles_local, y_particles_local, z_particles_local, v_particles_local, w_particles_local, cosmo, redshift, distribution_scheme, factor, field_i, field_norm_i, mode); if(flag_alloc_m) SID_free(SID_FARG m_particles_local); } } // Clean-up free_plist(&plist); if(n_load>1) SID_log("Done.",SID_LOG_CLOSE); } // loop over i_load // Write results to disk char filename_out_species[MAX_FILENAME_LENGTH]; init_plist(&plist,NULL,GADGET_LENGTH,GADGET_MASS,GADGET_VELOCITY); for(i_species=0;i_species<plist.n_species;i_species++){ if(flag_used[i_species]){ sprintf(grid_identifier,"%s_%s_%s",i_grid_identifier,i_run_identifier,plist.species[i_species]); sprintf(filename_out_species,"%s_%s",filename_out_root,plist.species[i_species]); write_grid(field[i_species], filename_out_species, i_write, n_grids_total, distribution_scheme, grid_identifier, header.box_size); free_field(field[i_species]); free_field(field_norm[i_species]); SID_free(SID_FARG field[i_species]); SID_free(SID_FARG field_norm[i_species]); i_write++; } } // Clean-up free_plist(&plist); SID_log("Done.",SID_LOG_CLOSE); } // loop over i_grid SID_log("Done.",SID_LOG_CLOSE); } // loop over i_run } // if n_used>0 // Clean-up free_cosmo(&cosmo); SID_free(SID_FARG grid_identifier); SID_free(SID_FARG n_all); SID_log("Done.",SID_LOG_CLOSE); SID_exit(ERROR_NONE); }
int main(int ac, char *av[]) { bool show_only; extern char Scorefile[PATH_MAX]; int score_wfd; /* high score writable file descriptor */ int score_err = 0; /* hold errno from score file open */ int ch; int ret; extern int optind; char *home; #ifdef FANCY char *sp; #endif if (pledge("stdio rpath wpath cpath tty", NULL) == -1) err(1, "pledge"); home = getenv("HOME"); if (home == NULL || *home == '\0') err(1, "getenv"); ret = snprintf(Scorefile, sizeof(Scorefile), "%s/%s", home, ".robots.scores"); if (ret < 0 || ret >= PATH_MAX) errc(1, ENAMETOOLONG, "%s/%s", home, ".robots.scores"); if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) < 0) score_err = errno; show_only = FALSE; while ((ch = getopt(ac, av, "srajt")) != -1) switch (ch) { case 's': show_only = TRUE; break; case 'r': Real_time = TRUE; /* Could be a command-line option */ tv.tv_sec = 3; break; case 'a': Start_level = 4; break; case 'j': Jump = TRUE; break; case 't': Teleport = TRUE; break; case '?': default: usage(); } ac -= optind; av += optind; if (ac > 1) usage(); if (ac == 1) { if (strlcpy(Scorefile, av[0], sizeof(Scorefile)) >= sizeof(Scorefile)) errc(1, ENAMETOOLONG, "%s", av[0]); if (score_wfd >= 0) close(score_wfd); /* This file requires no special privileges. */ if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) < 0) score_err = errno; #ifdef FANCY sp = strrchr(Scorefile, '/'); if (sp == NULL) sp = Scorefile; if (strcmp(sp, "pattern_roll") == 0) Pattern_roll = TRUE; else if (strcmp(sp, "stand_still") == 0) Stand_still = TRUE; if (Pattern_roll || Stand_still) Teleport = TRUE; #endif } if (show_only) { show_score(); return 0; } if (score_wfd < 0) { warnx("%s: %s; no scores will be saved", Scorefile, strerror(score_err)); sleep(1); } initscr(); signal(SIGINT, quit); cbreak(); noecho(); nonl(); if (LINES != Y_SIZE || COLS != X_SIZE) { if (LINES < Y_SIZE || COLS < X_SIZE) { endwin(); errx(1, "Need at least a %dx%d screen", Y_SIZE, X_SIZE); } delwin(stdscr); stdscr = newwin(Y_SIZE, X_SIZE, 0, 0); } do { init_field(); for (Level = Start_level; !Dead; Level++) { make_level(); play_level(); } if (My_pos.x > X_FIELDSIZE - 16) move(My_pos.y, X_FIELDSIZE - 16); else move(My_pos.y, My_pos.x); printw("AARRrrgghhhh...."); refresh(); score(score_wfd); } while (another()); quit(0); }
/* Einlesen und plotten der Trajektorien */ void plot_trajectories(struct state* state, struct trajectory* current) { double *plot_field; /* Plotmatrix fuer genau eine Trajektorie */ int i, j; plot_field = calloc(sizeof(double), state->field_max); if (plot_field == NULL) { printf("Out of memory!\n"); exit(1); } /* Unterordner fuer die Trajektorien in der KML-Ausgabedatei * anlegen */ fprintf(state->fh, "<Folder>\n"); fprintf(state->fh, "<name>Trajektorien</name>\n"); /* Einlesen aller Trajektorienaufpunkte in Trajektoriendichtenetz * und Ausgabe der Trajektorienverlaeufe in der KML-Datei */ for (i = 0; i < state->list_max; i++) { current->name = state->list[i]; /* Name der aktuellen Trajektorie schreiben und Unterordner * anlegen */ printf("%s\n", current->name); fprintf(state->fh, "<Folder>\n"); fprintf(state->fh, "<name>%s</name>\n", current->name); /* Weglesen des Trajektoriendatei-Headers und einlesen des ersten * Trajektorienpunkts */ get_trajectory_start_point(current); /* Uebernehmen der Startkoordinaten in die weitere * Berechnung */ current->x_old = current->x_begin; current->y_old = current->y_begin; /* Oeffnen der Ordnerstruktur zum Speichern der momentanen Trajektorie * in die KML-Datei und schreiben des Trajektorienstartpunkts */ print_trajectory_header(state->fh, current->x_begin, current->y_begin); /* Zuruecksetzen der Zaehlvariablen fuer das Einlesen der naechsten * Trajektorie */ reset_counter(current); /* Initialisieren der lokalen Abbildungsmatrix der Trajektorie */ init_field(plot_field, state->field_max); /* Einlesen und abbilden der Trajektorie auf der lokalen * Berechnungsnetzmatrix */ read_trajectory(current, state, plot_field); /* Addieren der Trajektorienabbildung der lokalen Abbildungsmatrix * zur Gesamtdarstellungsmatrix */ for (j = 0; j < state->field_max; j++) state->field_grid[j] += plot_field[j]; /* Aktuelle Trajektoriendatei schliessen */ fclose(current->fh); /* Struktur der aktuellen Trajektorie in der KML-Datei * schliessen */ fprintf(state->fh, "</coordinates>\n"); fprintf(state->fh, "</LineString>\n"); fprintf(state->fh, "</Placemark>\n"); fprintf(state->fh, "</Folder>\n\n"); } /* Unterordner fuer Trajektorien schliessen */ fprintf(state->fh, "</Folder>\n\n"); free(plot_field); }