void init_game(int tile_index) { if ((game_hero = load_actor_XML("aron.stat")) == NULL) { fprintf(stderr, "ERROR - Unable to initialize hero.\n"); exit(1); } if ((game_player = new_player(128 * 32, 96 * 32, game_hero)) == NULL) { fprintf(stderr, "ERROR - Unable to initialize player.\n"); exit(1); } if ((game_world = new_world("world.png", tile_index, game_player->x, game_player->y, 300, 200, DEFAULT_CELL_WIDTH, DEFAULT_CELL_HEIGHT)) == NULL) { fprintf(stderr, "ERROR - Unable to initialize world\n"); exit(1); } if ((game_scanner = new_scanner("marker4x4.png")) == NULL) { fprintf(stderr, "ERROR - Unable to initialize scanner\n"); exit(1); } if ((game_battle = new_battle(game_world)) == NULL) { fprintf(stderr, "ERROR - Unable to initialize battle\n"); exit(1); } set_game_state(GAME_STATE_WORLD); update_game(); }
World *read_world_file(FILE *fp) { World *w; w = new_world(); texture_list = new_list(); parse_world(fp, w); delete_list(texture_list); return w; }
// int main(int argc, char *argv[]) { int main(void) { const char *title = "Small Size League Simulator by RoboIME"; puts(title); { // Handle SIGINT (Ctrl+C) struct sigaction sa; sa.sa_handler = sigint_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGINT, &sa, NULL); } // Create a world struct World *world = new_world(&FIELD_2015); // Add some robots for (int i = 0; i < 6; i++) { world_add_robot(world, i, TEAM_BLUE); world_add_robot(world, i, TEAM_YELLOW); } // Create and bind socket struct Socket *socket = new_socket(11002, "224.5.23.2"); socket_sender_bind(socket); #define BUFFER_SIZE 10240 char buffer[BUFFER_SIZE]; int send_size; int send_geometry = 0; while (keep_going) { world_step(world, 1.0 / 60, 10, 1.0 / 600); // TODO: log errors send_size = serialize_world(world, buffer, BUFFER_SIZE); if (send_size > 0) socket_send(socket, buffer, send_size); if (send_geometry++ % 120 == 0) { send_size = serialize_field(world_get_field(world), buffer, BUFFER_SIZE); if (send_size > 0) socket_send(socket, buffer, send_size); } // TODO: realtime update // sleep for 16ms, prevents going too fast and intensive nanosleep(&(struct timespec){.tv_nsec=16000000}, NULL); }
void test_physics(){ printf("testing physics\n"); world_handle hello_world = new_world(); //tests movement, set/get velocity, set/get location(position) po_vector center; center.x = 1.0; center.y = 1.0; po_circle circle2 = create_circ(center, 1.0); po_geometry geo_circle2 = create_geom_circ(circle2, 1.0); po_handle circle02 = add_object (hello_world, &geo_circle2, 20.0, 1.0, 0.0); po_vector location = get_position(circle02); center.x = 0.0; center.y = 0.0; po_circle circle1 = create_circ(center, 5.0); po_geometry geo_circle1 = create_geom_circ(circle1, 1.0); po_handle circle01 = add_object (hello_world, &geo_circle1, 50.0, 0.0, 0.0); set_velocity(circle02,-1.0,0.0); po_vector velocity = get_velocity(circle02); update(hello_world,3); po_vector pos02 = get_position(circle02); po_vector pos01 = get_position(circle01); assert(pos02.x == 17.0); assert(pos02.y == 1.0); assert(pos01.x == 50.0); assert(pos01.y == 0.0); set_location(circle01, 0.0, 0.0); set_velocity(circle01, -7.76,1.7); set_location(circle02, 20.0, 0.0); set_velocity(circle02, -9.0,2.0); update(hello_world,1); po_vector pos002 = get_position(circle02); po_vector pos001 = get_position(circle01); assert( pos002.x == 11.0); assert( pos002.y == 2.0); assert(fabs(pos001.x - -7.76) <= EPSILON); assert(fabs(pos001.y - 1.7)<= EPSILON); }
int main(void) { World *w = new_world("Adventure In C"); struct room *current = cached_read_room(w, "start"); struct player me; initPlayer(&me, w); read_player(&me); push_alias(&me, "l", "look"); push_alias(&me, "u", "up"); push_alias(&me, "d", "down"); push_alias(&me, "lo", "look"); // Just an extra alias push_alias(&me, "h", "help"); push_alias(&me, "ex", "examine"); me.currentRoom = current; printf("%s enters the world.\n\n",me.name); look_player(&me); loop_player(&me); }
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; }
// Parse all rules from STDIN. int main(int argc, char *argv[]) { golem_world *world = new_world(rules_from_string(slurp_file(stdin))); dump_item(world->current, 0); return 0; }
int main(int argc, char *argv[]) { World *w; View *view; Intent *intent; Graphics_info *ginfo; Tcl_Interp *interp; Object *me; Boolean quit = False; #if PROFILE int fps_count = 0; double total_time = 0.0; double start_time; #endif interp = Tcl_CreateInterp(); updater = updater_create(interp); if (argc != 2) { fprintf(stderr, "Usage: %s <world file>\n", argv[0]); exit(EXIT_FAILURE); } ginfo = init_graphics(); #define TRUECOLOR #ifdef TRUECOLOR set_texture_trans(32, ginfo->palette.color_lookup); #else set_texture_trans(ginfo->palette.rgb_cube_size, ginfo->palette.color_lookup); #endif init_renderer(ginfo->width, ginfo->height); init_input_devices(); /* Initialize the view */ view = new_view(FLOAT_TO_FIXED(3.14159265 / 2.0)); w = new_world(); parser_init(interp, w); if (Tcl_EvalFile(interp, WT_INIT_FILENAME) != TCL_OK) { fprintf(stderr, "%s\n", Tcl_GetVar(interp, "errorCode", 0)); fprintf(stderr, "%s\n", Tcl_GetVar(interp, "errorInfo", 0)); exit(1); } if (Tcl_EvalFile(interp, argv[1]) != TCL_OK) { fprintf(stderr, "%s\n", Tcl_GetVar(interp, "errorCode", 0)); fprintf(stderr, "%s\n", Tcl_GetVar(interp, "errorInfo", 0)); exit(1); } while (!quit) { double sin_facing, cos_facing; double fx, fy, fz, torque; Framebuffer *fb; int i; start_time = current_time(); intent = read_input_devices(); me = get_controlled_object(); if (me == NULL) fatal_error("No controlled object"); for (i = 0; i < intent->n_special; i++) { switch (intent->special[i]) { case INTENT_END_GAME: quit = True; (void) Tcl_Eval(interp, "action_quit"); break; case INTENT_JUMP: (void) Tcl_Eval(interp, "action_jump"); break; case INTENT_ACTION1: (void) Tcl_Eval(interp, "action_1"); break; case INTENT_ACTION2: (void) Tcl_Eval(interp, "action_2"); break; case INTENT_ACTION3: (void) Tcl_Eval(interp, "action_3"); break; case INTENT_ACTION4: (void) (Tcl_Eval(interp, "action_4") != TCL_OK); break; case INTENT_ACTION5: (void) Tcl_Eval(interp, "action_5"); break; default: break; } } /* Determine forces on viewer. */ sin_facing = sin(me->angle); cos_facing = cos(me->angle); fx = cos_facing * intent->force_x - sin_facing * intent->force_y; fy = sin_facing * intent->force_x + cos_facing * intent->force_y; fz = -0.05 * me->mass; /* gravity */ torque = intent->force_rotate; /* Apply the forces. */ object_apply_force(me, fx, fy, fz); object_apply_torque(me, torque); object_update(me); if (me->z <= 0.0 && me->dz <= 0.0) { me->z = 0.0; me->dz = 0.0; } /* Determine the view. */ me = get_viewpoint_object(); if (me == NULL) fatal_error("No viewpoint object"); object_view(me, view); updater_run(updater); /* Display the world. */ fb = render(w, view); update_screen(fb); #if PROFILE fps_count++; total_time += current_time() - start_time; if (fps_count == 100) { printf("fps = %3.2f\n", (double) fps_count / total_time); fps_count = 0; total_time = 0.0; } #endif } end_input_devices(); end_graphics(); return EXIT_SUCCESS; }
MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags): QMainWindow(parent, flags), petri_dish(), histogramDialog(0), changed(true), auto_save(false), update_thread(this, this) { setupUi(this); connect(&timer, SIGNAL(timeout()), &petri_dish, SLOT(step())); connect(&petri_dish, SIGNAL(changed()), worldView, SLOT(queueUpdate())); //connect(&petri_dish, SIGNAL(changed()), worldView, SLOT(repaint())); connect(&petri_dish, SIGNAL(changed()), this, SLOT(set_stats())); connect(runButton, SIGNAL(clicked()), this, SLOT(start_stop())); //connect(checkIntegrity, SIGNAL(clicked()), this, SLOT(check_integrity())); connect(stepBtn, SIGNAL(clicked()), this, SLOT(step())); connect(radViewDefault, SIGNAL(clicked()), &petri_dish, SLOT(set_default_view_mode())); connect(radViewBugs, SIGNAL(clicked()), &petri_dish, SLOT(set_bugs_view_mode())); connect(radViewEnergy, SIGNAL(clicked()), &petri_dish, SLOT(set_energy_view_mode())); connect(radViewAge, SIGNAL(clicked()), &petri_dish, SLOT(set_age_view_mode())); connect(radViewSize, SIGNAL(clicked()), &petri_dish, SLOT(set_dna_size_view_mode())); connect(radViewDNA, SIGNAL(clicked()), &petri_dish, SLOT(set_dna_view_mode())); connect(radViewTerrain, SIGNAL(clicked()), &petri_dish, SLOT(set_terrain_view_mode())); connect(actionNew, SIGNAL(triggered()), this, SLOT(new_world())); connect(actionSave, SIGNAL(triggered()), this, SLOT(save_world())); connect(actionOpen, SIGNAL(triggered()), this, SLOT(load_world())); connect(actionSave_As, SIGNAL(triggered()), this, SLOT(save_as_world())); connect(settings, SIGNAL(settingsChanged()), this, SLOT(change_world())); connect(actionAutoSave, SIGNAL(triggered()), this, SLOT(autosave_world())); connect(actionCheck_Integrity, SIGNAL(triggered()), this, SLOT(check_integrity())); connect(worldView, SIGNAL(clicked(int, int)), this, SLOT(edit_bug(int, int))); connect(idleChk, SIGNAL(clicked(bool)), this, SLOT(enableIdleDetect(bool))); connect(&idleTimer, SIGNAL(timeout()), this, SLOT(checkIdle())); autosave_timer.setInterval(1000 * 60 * 2); // every two mins connect(&autosave_timer, SIGNAL(timeout()), this, SLOT(save_world())); petri_dish.set_widget(worldView); petri_dish.init(settings->getParams()); totalEnergy->setText(QString().setNum(petri_dish.get_total_energy())); spinDNAVal->setMaximum(settings->getParams().max_data); set_stats(); last_time = 0; update_time.start(); updates_per_sec = 0; //update_thread.start(); horizontalSlider->setValue(1000); timer.start(0); setAttribute(Qt::WA_QuitOnClose, true); repaint(); if(!qApp->arguments().size() > 1) { bool running; load_from_file(qApp->arguments().at(1), running); if(running) start(); } }