Tests About Natural Selection In Virtual Environment
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

map.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright (C) 2011 Weber Yann, Schuck Clement
  3. This file is part of Tansive.
  4. Tansive is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Tansive is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Tansive. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "map.h"
  16. t_map* create_map(int x, int y)
  17. {
  18. int i,j;
  19. t_map* res = malloc(sizeof(t_map));
  20. res->x = x;
  21. res->y = y;
  22. res->map = malloc(sizeof(t_map_element*)*x);
  23. for(i=0;i<res->x;i++)
  24. {
  25. res->map[i] = malloc(sizeof(t_map_element)*y);
  26. for(j=0;j<res->y;j++)
  27. res->map[i][j].type = 0;
  28. }
  29. return res;
  30. }
  31. void map_regen_veget(t_map* mp)
  32. {
  33. int i,j,k,l, mod = 0, pred_mod = 0;
  34. char end = 0;
  35. for(j=1; j<mp->y-1;j++)
  36. {
  37. pred_mod = 0;
  38. if(mp->map[0][j-1].type == MAP_ELT_VEGET)
  39. pred_mod++;
  40. if(mp->map[0][j].type == MAP_ELT_VEGET)
  41. pred_mod++;
  42. if(mp->map[0][j+1].type == MAP_ELT_VEGET)
  43. pred_mod++;
  44. if(mp->map[1][j-1].type == MAP_ELT_VEGET)
  45. pred_mod++;
  46. if(mp->map[1][j+1].type == MAP_ELT_VEGET)
  47. pred_mod++;
  48. for(i=1;i<mp->x-1;i++)
  49. {
  50. mod = pred_mod;
  51. pred_mod = 0;
  52. k = i+1;
  53. if(mp->map[k][j-1].type == MAP_ELT_VEGET)
  54. {
  55. mod++;
  56. pred_mod++;
  57. }
  58. if(mp->map[k][j].type == MAP_ELT_VEGET)
  59. {
  60. mod++;
  61. pred_mod++;
  62. }
  63. if(mp->map[k][j+1].type == MAP_ELT_VEGET)
  64. {
  65. mod++;
  66. pred_mod++;
  67. }
  68. if(mp->map[i][j].type == MAP_ELT_EMPTY && !(rand()%(MAP_VEG_SPAWN/( (mod==0?1:mod * MAP_VEG_COEFPROX)))) )
  69. {
  70. mp->map[i][j].type = MAP_ELT_VEGET;
  71. mp->map[i][j].timeleft_veget = rand()%(MAP_VEG_MAX_TIMELEFT-MAP_VEG_MIN_TIMELEFT)+MAP_VEG_MIN_TIMELEFT;
  72. pred_mod++;
  73. }
  74. else if(mp->map[i][j].type == MAP_ELT_VEGET)
  75. {
  76. mp->map[i][j].timeleft_veget--;
  77. if(mp->map[i][j].timeleft_veget == 0)
  78. mp->map[i][j].type = MAP_ELT_EMPTY;
  79. }
  80. }
  81. }
  82. }
  83. void disp_map(t_map* mp, int xbeg, int ybeg, int xend, int yend)
  84. {
  85. int i,j,k;
  86. if(xbeg>=xend || ybeg>=yend || yend > mp->y || xend > mp->x || xbeg<0 || ybeg<0)
  87. fprintf(stderr,"disp_map : Bad parameters\n");
  88. for(j=ybeg;j<yend-1;j++)
  89. {
  90. printf("+");
  91. for(k=xbeg;k<xend;k++)
  92. printf("-+");
  93. printf("-+\n");
  94. printf("|");
  95. for(i=xbeg;i<xend;i++)
  96. {
  97. printf("%c|",(mp->map[i][j].type == MAP_ELT_EMPTY?' ':(mp->map[i][j].type == MAP_ELT_VEGET?'#':'C')));
  98. }
  99. printf("%c|\n",(mp->map[i][j].type == MAP_ELT_EMPTY?' ':(mp->map[i][j].type == MAP_ELT_VEGET?'#':'C')));
  100. }
  101. printf("+");
  102. for(k=xbeg;k<xend;k++)
  103. printf("-+");
  104. printf("-+\n");
  105. }