Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Online C compiler to run C program online
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- enum TERRAIN_TYPE {
- GREEN,
- WASTELAND,
- WATER,
- ROAD,
- BUILDING
- };
- typedef struct terrain {
- float height;
- int type;
- float attendance;
- } Terrain;
- // Function prototypes
- int generateRandomInt(const int from, const int to);
- float generateDiff();
- int generateSign();
- float generateMultiplier();
- float generateFirstHeight();
- Terrain** generateEmptyMap(const int height, const int width);
- void DeleteMap(Terrain **map, const int height);
- Terrain **fillMapHeight(Terrain **map, const int height, const int width);
- Terrain **fillMapType(Terrain **map, const int height, const int width);
- void PrintMapByTerrainType(Terrain **map, const int height, const int width);
- void PrintMapByTerrainHeight(Terrain **map, const int height, const int width);
- void BuildRoad(Terrain **map, const int height, const int width);
- void ExportMap(Terrain **map, const int height, const int width, const char *file_name);
- Terrain** ImportMap(const char *file_name, int *height, int *width);
- float calculateIncline(float h1, float h2);
- int checkIncline(float h1, float h2, float h3, float h4, float h5);
- void PrintJumpingRoutes(Terrain **map, const int height, const int width);
- // Generate random int between 'from' and 'to'
- int generateRandomInt(const int from, const int to) {
- return rand() % (to - from) + from;
- }
- // Generate random floating point number between 0.0 and 1.0
- float generateDiff() {
- return ((float)generateRandomInt(1, 101)) / 100;
- }
- // Flip a coin to pick a sign (+ or -). Returns +1 or -1 as a result
- int generateSign() {
- if (generateRandomInt(1, 1001) % 2) {
- return -1;
- }
- return 1;
- }
- // Generate random floating point number between 0.5 and 1.5
- float generateMultiplier() {
- return ((float)generateRandomInt(20, 150)) / 100;
- }
- // Generate a random floating point number between -1.0 & 1.0
- float generateFirstHeight() {
- return ((float)generateRandomInt(1, 20001)) / 10000.0 - 1.0;
- }
- // Generate an empty map
- Terrain** generateEmptyMap(const int height, const int width) {
- if (height < 1 || width < 1) {
- fprintf(stderr, "Invalid size!\n");
- return NULL;
- }
- Terrain **emptyMap = malloc(height * sizeof(Terrain *));
- for (int i = 0; i < height; ++i) {
- emptyMap[i] = malloc(width * sizeof(Terrain));
- }
- return emptyMap;
- }
- void DeleteMap(Terrain **map, const int height) {
- for (int row = 0; row < height; ++row) {
- free(map[row]);
- }
- free(map);
- }
- Terrain **fillMapHeight(Terrain **map, const int height, const int width) {
- for (int col = 0; col < width; ++col) {
- for (int row = 0; row < height; ++row) {
- if (row == 0) {
- map[row][col].height = generateFirstHeight();
- continue; // Skip to next iteration
- }
- // Current height = previousHeight + (random float between 0.0 and +1.0) * (multiplier between 0.5 and 1.5) * sign
- map[row][col].height = (map[row - 1][col].height + generateDiff()) * generateMultiplier() * generateSign();
- }
- }
- return map;
- }
- Terrain **fillMapType(Terrain **map, const int height, const int width) {
- for (int row = 0; row < height; ++row) {
- for (int col = 0; col < width; ++col) {
- map[row][col].type = generateRandomInt(0, 5);
- }
- }
- return map;
- }
- void PrintMapByTerrainType(Terrain **map, const int height, const int width) {
- if (!map) {
- fprintf(stderr, "Invalid Pointer\n");
- return;
- }
- if (height < 1 || width < 1) {
- fprintf(stderr, "Incorrect map size!\n");
- }
- for (int row = 0; row < height; ++row) {
- for (int col = 0; col < width; ++col) {
- printf("%d ", map[row][col].type);
- }
- printf("\n");
- }
- return;
- }
- void PrintMapByTerrainHeight(Terrain **map, const int height, const int width) {
- if (!map) {
- fprintf(stderr, "Invalid Pointer\n");
- return;
- }
- if (height < 1 || width < 1) {
- fprintf(stderr, "Incorrect map size!\n");
- }
- for (int row = 0; row < height; ++row) {
- for (int col = 0; col < width; ++col) {
- printf("%5.2f ", map[row][col].height);
- }
- printf("\n");
- }
- return;
- }
- void BuildRoad(Terrain **map, const int height, const int width) {
- printf("To exit, enter -1.\n");
- printf("Otherwise, enter coordinates of the field you'd like to transform to road.\n");
- int row, col, previousRow, previousCol, roadLength = 0;
- do {
- scanf("%d", &row);
- if (row == -1) {
- return;
- }
- scanf("%d", &col);
- if (col == -1) {
- return;
- }
- if (row < 0 || row >= height || col < 0 || col >= width) {
- printf("Coordinates are out of the map. Please input valid coordinates\n");
- continue;
- }
- if (roadLength > 0) {
- if (abs(previousRow - row) + abs(previousCol - col) > 1) {
- printf("Road builders can only move 1 square away each time.\n");
- continue;
- }
- }
- map[row][col].type = ROAD;
- previousRow = row;
- previousCol = col;
- roadLength++;
- } while (1);
- }
- void ExportMap(Terrain **map, const int height, const int width, const char *file_name) {
- if (!file_name || !map) {
- fprintf(stderr, "Invalid pointer!\n");
- return;
- }
- for (int i = 0; i < height; ++i) {
- if (!map[i]) {
- fprintf(stderr, "Invalid pointer!\n");
- return;
- }
- }
- if (height < 1 || width < 1) {
- fprintf(stderr, "Invalid map size!\n");
- return;
- }
- FILE *export = fopen(file_name, "wb");
- if (!export) {
- fprintf(stderr, "File failed to open!\n");
- }
- fwrite(&width, sizeof(int), 1, export);
- fwrite(&height, sizeof(int), 1, export);
- for(int row = 0; row < height; ++row) {
- fwrite(map[row], sizeof(Terrain), width, export);
- }
- fclose(export);
- return;
- }
- Terrain** ImportMap(const char *file_name, int *height, int *width) {
- if (!file_name) {
- fprintf(stderr, "Invalid pointer!\n");
- return NULL;
- }
- FILE *import = fopen(file_name, "rb");
- if (!import) {
- fprintf(stderr, "File failed to open!\n");
- return NULL;
- }
- fread(width, sizeof(int), 1, import);
- fread(height, sizeof(int), 1, import);
- Terrain **result = malloc(*height * sizeof(Terrain *));
- for (int i = 0; i < *height; ++i) {
- result[i] = malloc(*width * sizeof(Terrain));
- }
- for (int row = 0; row < *height; ++row) {
- fread(result[row], sizeof(Terrain), *width, import);
- }
- fclose(import);
- return result;
- }
- float calculateIncline(float h1, float h2) {
- float a, b, c, A, B, C, R, s, pi, area;
- pi = acos(-1);
- a = 1.0;
- b = abs(h2 - h1);
- c = sqrt(a*a + b*b);
- s = (a + b + c) / 2;
- area = sqrt(s * (s - a) * (s - b) * (s - c));
- R = (a * b * c) / (4 * area);
- A = (180 / pi) * asin(a / (2 * R));
- B = (180 / pi) * asin(b / (2 * R));
- C = (180 / pi) * asin(c / (2 * R));
- return B;
- }
- int checkIncline(float h1, float h2, float h3, float h4, float h5) {
- if (h1 < h2 && h2 < h3 && h3 > h4 && h4 > h5) {
- if(calculateIncline(h1, h2) < 45.0 && calculateIncline(h2, h3) < 45.0 &&
- calculateIncline(h3, h4) < 80.0 && calculateIncline(h4, h5) < 80.0) {
- return 1;
- }
- }
- return 0;
- }
- void PrintJumpingRoutes(Terrain **map, const int height, const int width) {
- for(int col = 4; col < width; ++col) {
- for (int row = 0; row < height; ++row) {
- if (map[row][col].type == WASTELAND && map[row][col-1].type == WASTELAND &&
- map[row][col-2].type == WASTELAND && map[row][col-3].type == WASTELAND &&
- map[row][col-4].type == WASTELAND) {
- if (checkIncline(map[row][col].height, map[row][col-1].height,
- map[row][col-2].height, map[row][col-3].height, map[row][col-4].height)) {
- printf("[%d|%d]<-[%d|%d]<-[%d|%d]<-[%d|%d]<-[%d|%d]\n",
- row, col - 4, row, col - 3, row, col - 2, row, col - 1, row, col);
- }
- }
- }
- }
- }
- int main() {
- srand(time(NULL));
- Terrain** map = NULL;
- int choice = 0;
- int mapWidth, mapHeight;
- do {
- printf("1. Generate Map\n");
- printf("2. Print Map By Terrain Height\n");
- printf("3. Print Map By Terrain Type\n");
- printf("4. Build Road\n");
- printf("5. Print Bikers Jumping Routes\n");
- printf("6. Export to File\n");
- printf("7. Import From file\n");
- printf("0. EXIT\n");
- printf("Your Choice: ");
- scanf("%d", &choice);
- switch(choice) {
- case 1:
- if(map != NULL) {
- DeleteMap(map, mapWidth);
- mapWidth = -1;
- mapHeight = -1;
- }
- printf("Please input: MAP_WIDTH MAP_HEIGHT\n");
- scanf("%d %d", &mapWidth, &mapHeight);
- map = generateEmptyMap(mapHeight, mapWidth);
- fillMapHeight(map, mapHeight, mapWidth);
- fillMapType(map, mapHeight, mapWidth);
- break;
- case 2:
- PrintMapByTerrainHeight(map, mapHeight, mapWidth);
- break;
- case 3:
- PrintMapByTerrainType(map, mapHeight, mapWidth);
- break;
- case 4:
- BuildRoad(map, mapHeight, mapWidth);
- break;
- case 5:
- ExportMap(map, mapHeight, mapWidth, "map.bin");
- break;
- case 6:
- if (map) {
- DeleteMap(map, mapWidth);
- mapWidth = -1;
- mapHeight = -1;
- }
- map = ImportMap("map.bin", &mapHeight, &mapWidth);
- break;
- case 0:
- break;
- default: break;
- }
- } while (choice != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement