123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- /*
- Copyright (C) 2011 Weber Yann, Schuck Clement
-
- This file is part of Tansive.
-
- Tansive is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- Tansive is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Tansive. If not, see <http://www.gnu.org/licenses/>.
- */
- #include<stdio.h>
- #include<string.h>
- #include<time.h>
-
- #include<SDL/SDL.h>
-
- #include"map.h"
- #include"creature_logic.h"
- #include"visu.h"
- #include"config.h"
- #include"event.h"
-
-
- #define TEST_DISP_EMPTY_GNOME 1
-
- #define NB_TOURS_TEST 200
- #define NB_TOURS_REGEN_VEGET 200
-
- extern unsigned int naturaldead;
- extern unsigned int fightdead;
- extern unsigned int starveddead;
- extern unsigned int eatdead;
- extern unsigned int born;
-
- /*
- Function to display help and exit.
- */
- void usage(char* name)
- {
- fprintf(stderr, "Usage : %s [OPTIONS]\n", name);
- fprintf(stderr,"\nRun tansive\n");
- fprintf(stderr,"\nDisplay mode:\n");
- fprintf(stderr,"\t--display-genome\tDisplay genome tree informations\n");
- fprintf(stderr,"\t--disable-info\t\tDisable the display of infos about the number of creature or the number of elapsed round\n");
- fprintf(stderr,"\t--no-display\t\tDisable the SDL display\n");
- fprintf(stderr,"\nOutput modes:\n");
- fprintf(stderr,"\t-odot output.dot\tSpecify the output file for graphviz\n");
- fprintf(stderr,"\nWorld options:\n");
- fprintf(stderr,"\t--resolution x,y\tSpecify the resolution of the window and the size of the world\n");
- fprintf(stderr,"\t-ni number\t\tSpecify the number of creature initialy generated\n");
- fprintf(stderr,"\t-nm number\t\tSpecify the maximum number of creature allowed in world\n");
- exit(-1);
- }
-
- int main(int argc, char** argv)
- {
-
- naturaldead=eatdead=fightdead=starveddead=born=0; //Initializing global variable
-
- int choix = 1, xc = -1, yc = -1, zoom = 1, mx = 0, my = 0, i=0, j,k,l;
-
- //variable to store command line options
- char type_test = 0, sdl_disp = 1, gnome_disp = 0, info_disp = 1;
- int x_resol = 800, y_resol = 800;
- int init_number_creat = 800, max_number_creat = 15000;
- char* o_dot_name = NULL;
-
- //main loop variable
- char again = 1;
- int count = 0;
-
- //threads variable
- pthread_mutex_t mut_event;
- pthread_t event_catcher_thread;
- t_param_thread_event param_thread;
-
- //SDL variable
- SDL_Surface *screen = NULL, *surface = NULL;
- Uint32 rmask, gmask, bmask, amask;
- SDL_Rect bckgrndPos;
-
-
- for(i=1;i<argc;i++)
- {
- if(strcmp(argv[i], "--help") == 0)
- usage(argv[0]);
- else if(strcmp(argv[i],"--no-display") == 0)
- sdl_disp = 0;
- else if(strcmp(argv[i],"--display-genome") == 0)
- gnome_disp = 1;
- else if(strcmp(argv[i],"--disable-info") == 0)
- info_disp = 0;
- else if(strcmp(argv[i],"-odot") == 0)
- {
- i++;
- if(argc<i)
- usage(argv[0]);
- o_dot_name = argv[i];
- FILE* file = fopen(o_dot_name, "w+");
- if(!file)
- usage(argv[0]);
- fclose(file);
- }
- else if(strcmp(argv[i],"--resolution") == 0)
- {
- i++;
- if(argc<=i)
- usage(argv[0]);
- for(j=0;argv[i][j]!='\0';j++)
- {
- if(argv[i][j] == ',')
- {
- argv[i][j] = '\0';
- break;
- }
- }
- j++;
- if(argv[i][j]=='\0')
- usage(argv[0]);
-
- x_resol = atoi(argv[i]);
- y_resol = atoi(argv[i]+j);
-
- if(x_resol<=0 || y_resol <=0)
- usage(argv[0]);
- }
- else if(strcmp(argv[i],"-ni") == 0)
- {
- i++;
- if(argc<=i)
- usage(argv[0]);
- init_number_creat = atoi(argv[i]);
- if(init_number_creat<=0)
- usage(argv[0]);
- }
- else if(strcmp(argv[i],"-nm") == 0)
- {
- i++;
- if(argc<=i)
- usage(argv[0]);
- max_number_creat = atoi(argv[i]);
- if(max_number_creat<=0)
- usage(argv[0]);
- }
- else
- usage(argv[0]);
- }
-
- //Initializing variable
- t_chain_creature *lst_creat = NULL, *current = NULL;
- t_creature *sav = NULL;
- t_tree_genome* head = NULL;
-
- bckgrndPos.x = 0;
- bckgrndPos.y = 0;
-
- xc = x_resol/2;
- yc = y_resol/2;
-
- srand(time(NULL));
-
- t_map* map = create_map(x_resol, y_resol);
-
- //Generating the first creature, with a random genome
- lst_creat = create_creature_chain(create_random_creature());
- head = lst_creat->creat->genome_tree;
- lst_creat->creat->genome_tree->gnome.nb_creat++;
- current = lst_creat;
- current->creat->x = rand()%x_resol;
- current->creat->y = rand()%y_resol;
- map->map[current->creat->x][current->creat->y].type = MAP_ELT_CREAT;
- map->map[current->creat->x][current->creat->y].creat = current->creat;
-
- //Generating the other creatures
- for(i=1;i<init_number_creat;i++)
- {
- lst_creat->creat->genome_tree->gnome.nb_creat++;
- current = add_creature_chain(lst_creat, create_gnome_creature(current->creat->genome_tree));
- current->creat->x = rand()%x_resol;
- current->creat->y = rand()%y_resol;
- map->map[current->creat->x][current->creat->y].type = MAP_ELT_CREAT;
- map->map[current->creat->x][current->creat->y].creat = current->creat;
- }
-
- //Initialization of SDL variable
- if(sdl_disp)
- {
- #if SDL_BYTEORDER == SDL_BIG_ENDIAN
- rmask = 0xff000000;
- gmask = 0x00ff0000;
- bmask = 0x0000ff00;
- amask = 0x000000ff;
- #else
- rmask = 0x000000ff;
- gmask = 0x0000ff00;
- bmask = 0x00ff0000;
- amask = 0xff000000;
- #endif
- if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
- {
- fprintf(stderr, "SDL initialisation error : %s\n", SDL_GetError());
- exit(EXIT_FAILURE);
- }
- screen = SDL_SetVideoMode(x_resol, y_resol, 32, (SDL_HWSURFACE | SDL_DOUBLEBUF));
- if(screen == NULL)
- {
- fprintf(stderr, "Failed to load video mode : %s\n", SDL_GetError());
- exit(EXIT_FAILURE);
- }
-
- surface = SDL_CreateRGBSurface(SDL_HWSURFACE, x_resol, y_resol, 32, rmask, gmask, bmask, amask);
- SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0, 0, 0));
-
- SDL_EnableKeyRepeat(10, 10);
-
- SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0, 0, 0));
- }
-
- k=j=0;
-
- //Pre-generation of vegetals
- for(i=0;i<NB_TOURS_REGEN_VEGET;i++)
- {
-
- map_regen_veget(map);
- if(sdl_disp)
- {
- for(l=0; l<(x_resol*y_resol)/NB_TOURS_REGEN_VEGET; l++)
- {
- setPixelRGB(surface, j,k,0,255,0);
- j=(j==x_resol-1?0:j+1);
- k=(j==0?k+1:k);
- }
-
- SDL_BlitSurface(surface, NULL, screen, &bckgrndPos);
- SDL_Flip(screen);
- }
- }
-
- char dead = 0;
- int counter_creat = 0, prev_nb_creat;
-
- if(sdl_disp)
- {
- //Launching the event catcher thread
- param_thread.x = &xc;
- param_thread.y = &yc;
- param_thread.mx = &mx;
- param_thread.my = &my;
- param_thread.x_resol = &x_resol;
- param_thread.y_resol = &y_resol;
- param_thread.again = &again;
- param_thread.zoom = &zoom;
- param_thread.mutex = &mut_event;
- pthread_mutex_init(&mut_event,NULL);
- pthread_create(&event_catcher_thread,NULL,event_catcher_loop,¶m_thread);
-
- pthread_mutex_lock(&mut_event);
- }
-
- //Main loop
- while(again)
- {
- if(sdl_disp)
- {
- pthread_mutex_unlock(&mut_event);
- }
- map_regen_veget(map);
-
- //Allowing 1 decision per creature
- current = lst_creat;
- prev_nb_creat = counter_creat;
- counter_creat = 0;
- if(current != NULL)
- {
- do
- {
- dead = 0;
- sav = current->creat;
-
- //The decision
- creature_decision(map,lst_creat, current->creat, &dead, &lst_creat);
-
- current = current->next;
-
- if(dead)
- {
-
- sav->genome_tree->gnome.nb_creat--;
-
- map->map[sav->x][sav->y].type = MAP_ELT_EMPTY;
-
- lst_creat = del_creature_chain(&lst_creat,sav);
-
- }
- else
- {
- counter_creat++;
- }
- }while(current != NULL);
- }
-
- if(info_disp)
- printf("%d | nb creatures = %d | born = %d | deads : natural = %d, starved = %d, fight = %d, eat = %d\n",count, counter_creat,born, naturaldead, starveddead, fightdead,eatdead);
- clean_treegnome(head);
-
- if(gnome_disp)
- disp_genome_tree_info(head,1);
-
- if(o_dot_name && count%1000 == 0)
- {
- //Writing the genome tree for graphviz
- FILE* file = fopen(o_dot_name, "w+");
- write_graphviz_genome_tree(file, head);
- fclose(file);
- }
-
- count++;
-
- if(counter_creat > max_number_creat || counter_creat == 0)
- again = 0;
-
- if(sdl_disp)
- {
- pthread_mutex_lock(&mut_event);
- //Drawing the map
- SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0, 0, 0));
- draw_map_zoom(screen, surface,map, xc, yc, zoom);
- draw_zoom_area(screen,surface, mx, my, map);
- SDL_BlitSurface(surface, NULL, screen, &bckgrndPos);
- SDL_Flip(screen);
- }
- }//Main loop's end
-
- if(sdl_disp)
- {
- pthread_mutex_unlock(&mut_event);
- pthread_join(event_catcher_thread, NULL);
- SDL_Quit();
- }
-
- if(o_dot_name)
- {
- //Writing the genome tree for graphviz
- FILE* file = fopen(o_dot_name, "w+");
- write_graphviz_genome_tree(file, head);
- fclose(file);
- }
-
- return 0;
- }
|