#include "planetgen.h" #include #include #include "game.h" #include "map.h" #include "mem.h" #include "rand.h" /* Functions */ char fillplanet(int); void display_map(int, int, char*); int planets_at_safe_distance(int, int, int,struct map_position*,int, int); /* These are the types of planets I have currently. Please add more.*/ #define NUM_PLANET_TYPES 5 #define PLANET_DESERT 0 #define PLANET_FREEZING 1 #define PLANET_MOUNTAINOUS 2 #define PLANET_TERRAN 3 #define PLANET_JUNGLE 4 /* These are the planet size that are created. */ #define NUM_PLANET_SIZES 4 #define PLANET_TINY 0 #define PLANET_SMALL 1 #define PLANET_MEDIUM 2 #define PLANET_LARGE 3 /* Used for generating the number of planets based upon landmass*/ #define SPACE_A_PLANET_TAKES 50.0 /* Minimum distances that planets are placed apart */ #define SAFE_DISTANCE 6.0 /* ------------------------------------------------------------------------ This converts the matrix that spacifies where the land is into a terrain map based upon what type of planet you specify. ------------------------------------------------------------------------ */ char fillplanet(int _type) { int i; char retval; i = myrand(100); /* random() % 100; */ switch(_type) { case PLANET_DESERT: if (i < 0 ) retval = 's'; else if (i < 1 ) retval = 'f'; else if (i < 11 ) retval = 'g'; else if (i < 11 ) retval = 'j'; else if (i < 31 ) retval = 'p'; else if (i < 36 ) retval = 'h'; else if (i < 41 ) retval = 'm'; else if (i < 98 ) retval = 'd'; else retval = 'a'; break; case PLANET_FREEZING: if (i < 0 ) retval = 's'; else if (i < 0 ) retval = 'f'; else if (i < 5 ) retval = 'g'; else if (i < 5 ) retval = 'j'; else if (i < 10 ) retval = 'p'; else if (i < 15 ) retval = 'h'; else if (i < 25 ) retval = 'm'; else if (i < 35 ) retval = 'd'; else retval = 'a'; break; case PLANET_MOUNTAINOUS: if (i < 5 ) retval = 's'; else if (i < 8 ) retval = 'f'; else if (i < 8 ) retval = 'g'; else if (i < 10 ) retval = 'j'; else if (i < 10 ) retval = 'p'; else if (i < 25 ) retval = 'h'; else if (i < 90 ) retval = 'm'; else if (i < 95 ) retval = 'd'; else retval = 'a'; break; case PLANET_JUNGLE: if (i < 35 ) retval = 's'; else if (i < 45 ) retval = 'f'; else if (i < 50 ) retval = 'g'; else if (i < 95 ) retval = 'j'; else if (i < 95 ) retval = 'p'; else if (i < 95 ) retval = 'h'; else if (i < 100 ) retval = 'm'; else if (i < 100 ) retval = 'd'; else retval = 'a'; break; default: /* case PLANET_TERRAN: */ if (i < 3 ) retval = 's'; else if (i < 15 ) retval = 'f'; else if (i < 50 ) retval = 'g'; else if (i < 57 ) retval = 'j'; else if (i < 87 ) retval = 'p'; else if (i < 94 ) retval = 'h'; else if (i < 97 ) retval = 'm'; else if (i < 99 ) retval = 'd'; else retval = 'a'; break; } return retval; } /* ------------------------------------------------------------------------ ------------------------------------------------------------------------ */ /* void give_help_message() { printf("gm X Y LAND\n X -> x distance\n Y -> y distance\n LAND -> percent of space that is land ( 15-85 )\n"); } */ /* ------------------------------------------------------------------------ // determine_number_planets(x,y,landper); // ------------------------------------------------------------------------ // Determine the number of planets to have based upon the landper and the // dimensions of the game. // // Ok, here is what I went with: // // n = # of planets // P = space a planet takes = 100 // x,y = dimensions of grid // landper = amount of land vs space // // n = (landper(x)(y)) / P ------------------------------------------------------------------------ */ int determine_number_planets(int x, int y, int landper) { int retval; retval = (int)(( (((double)landper)/100.0)* ((double)(x * y))) / SPACE_A_PLANET_TAKES); return retval; } /* ------------------------------------------------------------------------ Leave it in incase anyone wants to play around with planet maps. Make sure you include stdio. ------------------------------------------------------------------------ */ /*void display_map(int _x, int _y, char* _map) { int i; printf(" "); for(i=0;i<_x;i++) printf("%d",i/10); printf("\n "); for(i=0;i<_x;i++) printf("%d",i%10); for(i=0;i<(_x*_y);i++) { if ( (i%_x == 0) ) printf("\n%3d",i/_x); printf("%c",_map[i]); } printf("\n"); } */ /* ------------------------------------------------------------------------ // ------------------------------------------------------------------------ // _width is the x grid width // _height is the y grid height // // * check for x wrap // * check for too close to top ------------------------------------------------------------------------ */ int planets_at_safe_distance(int _x, int _y, int _pcount,struct map_position* _planets,int _width, int _height) { int i; int flag,retval; double d,d1,d2; flag = 0; i = 0; if ( (_y < 2) || (_y > (_height-3) )) flag = 1; /* fail it b/c too close to top */ while ( (i < _pcount ) && (flag == 0) ) { d1 = sqrt( (double)((_x - _planets[i].x)*(_x- _planets[i].x)+ (_y - _planets[i].y)*(_y-_planets[i].y))); if ( _x > _planets[i].x ) d2 = sqrt( (double)((_x - _planets[i].x - _width)*(_x- _planets[i].x-_width)+ (_y - _planets[i].y)*(_y-_planets[i].y))); else d2 = sqrt( (double)((_x - _planets[i].x + _width)*(_x- _planets[i].x + _width)+ (_y - _planets[i].y)*(_y-_planets[i].y))); if ( d1 < d2) d= d1; else d = d2; /* --- handle the wrap-around problem */ if ( d < SAFE_DISTANCE ) { flag = 1; /* fprintf(stderr,"failed: %d %d vs %d: %d %d\n",_x,_y,i,_planets[i].x,_planets[i].y); */ } /* printf("distance: %lf %d %d .. %d %d\n",d,_x,_y,_planets[i].x,_planets[i].y); */ i++; } if (flag == 1) retval = 0; else retval = 1; return retval; } /* ------------------------------------------------------------------------ // ------------------------------------------------------------------------ // Ok, choose random spots and place the planets. If they are not within // 6 spaces, keep the spot. Else, place it again. // NOTE: this is not guaranteed to halt so if it fails to place the planet // more than 500 times, just go with what you have. How's that for // intelligent algorithms? ------------------------------------------------------------------------ */ void place_planets_on_map(int _x, int _y, char* _map, int _numplanets) { int i,t,pcount; int tx,ty; struct map_position *planets; int flag; /* planets = (struct map_position*) malloc(sizeof(struct map_position)*_numplanets); */ planets = (struct map_position*) fc_malloc( sizeof(struct map_position)*_numplanets); /* --- This is actuall sizeof(map_position) but it errored on me. */ for(i=0;i<_numplanets;i++) { planets[i].x = 0; planets[i].y = 0; } flag = 0; for(pcount = 0;((pcount < _numplanets)&&(flag!= 1));pcount++) { flag = 0; t = 0; do { tx = myrand(_x); /*random() % _x; */ ty = myrand(_y); /* random() % _y; */ if ( t >= 500) flag=1; if ( planets_at_safe_distance(tx,ty,pcount,planets,_x,_y)) { flag = 2; planets[pcount].x = tx; planets[pcount].y = ty; /* printf(" ******* planet %3d: %4d,%4d\n",pcount,planets[pcount].x,planets[pcount].y); */ } t++; } while(flag == 0); } for(i=0;i