int floyd_warshall(int * map, int vertex_num) { int i,j,k; int max_path_len=128; int dis[vertex_num*vertex_num]; char *path[vertex_num*vertex_num]; init_dis_from_map(dis,map,vertex_num); malloc_path(path,max_path_len,vertex_num); init_path(path,max_path_len,vertex_num); for(k=0; k < vertex_num; k++) { for(i = 0; i < vertex_num; i++) { for(j = 0; j < vertex_num; j++) { if(dis[i*vertex_num+j] > dis[i*vertex_num+k] + dis[k*vertex_num +j] ) { dis[i*vertex_num+j] = dis[i*vertex_num+k] + dis[k*vertex_num +j]; snprintf(path[i*vertex_num +j],max_path_len,"%s,%s",path[i*vertex_num+k],path[k*vertex_num + j]); } } } } printf("the floyd-warrshall result: \n"); print_distance(dis,vertex_num); printf("the path result is \n"); print_path(path,vertex_num); destroy_path(path,vertex_num); return OK; }
void destroy_game_state(GameState * state) { register int i; for (i = 0; i < state->enemies_length; i++) { destroy_path(&state->enemies[i].enemy.path); } free(state->enemies); for (i = 0; i < state->towers_length; i++) { destroy_tower(&state->world, state->towers[i]); } free(state->towers); destroy_world(&state->world); }
Entity * place_tower(World * world, TowerType type, float world_x, float world_y) { int tile_x = convert_world2tile_x(world_x); int tile_y = convert_world2tile_y(world_y); if (world->entities[tile_x][tile_y].type == EMPTY) { Path path; Entity* tower = (Entity*) malloc(sizeof(Entity)); init_entity(tower, TOWER, world_x, world_y, TILE_SIZE, TILE_SIZE); tower->tower.tower_type = type; switch (type) { case MACHINE_GUN: init_machine_gun(tower, world_x, world_y); break; case ROCKET_LAUNCHER: init_rocket_launcher(tower, world_x, world_y); break; case FLAK_CANNON: init_flak_cannon(tower, world_x, world_y); break; } tower->tower.target = NULL; tower->tower.projectiles = (Projectile*) calloc(tower->tower.ammo, sizeof(Projectile)); world->entities[tile_x][tile_y] = *tower; path.current_node_index = 0; path.length = 0; path.trajectory_type = OVER_LAND; path.nodes = (Node*) malloc(sizeof(Node)); if (create_path(&path, world, world->spawn, world->castle, OVER_LAND) != 0) { destroy_path(&path); return tower; } else { world->entities[convert_world2tile_x(tower->all.world_x)][convert_world2tile_y(tower->all.world_y)].type = EMPTY; destroy_tower(world, tower); destroy_path(&path); return NULL; } } return NULL; }
void setEnv(char **args, int argc, char ***envp,struct pathelement **path) { if(argc==1){ printList(*envp); return; }else if(argc==2){ setenv(args[1],"",OVER_WRITE); }else if(argc==3){ setenv(args[1],args[2],OVER_WRITE); }else{ fprintf(stderr, "%s\n", "format error."); return; } if(args[1] && strcmp(args[1],"PATH")==0){ // printf("%s\n", "change the PATH"); destroy_path(*path); *path = get_path(); } }