terrain.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef TERRAIN
  2. #define TERRAIN
  3. #include <stdlib.h>
  4. #include <omp.h>
  5. typedef struct world_coord_t {
  6. size_t i;
  7. size_t j;
  8. } world_coord_t;
  9. typedef struct terrain_t {
  10. size_t pop;
  11. float pheromone;
  12. float decay;
  13. float increase;
  14. } terrain_t;
  15. void terrain_update(terrain_t *terrain, size_t width, size_t height, float time_delta){
  16. // Extra decay on borders
  17. #pragma omp parallel for
  18. for(size_t i = 0 ; i < height ; ++i){
  19. terrain[i * width].pheromone -= 2 * terrain[i * width].decay * time_delta;
  20. terrain[(i + 1) * width - 1].pheromone -= 2 * terrain[(i + 1) * width - 1].decay * time_delta;
  21. }
  22. #pragma omp parallel for
  23. for(size_t j = 1 ; j < width - 1 ; ++j){
  24. terrain[j].pheromone -= 2 * terrain[j].decay * time_delta;
  25. terrain[(height - 1) * width + j].pheromone -= 2 * terrain[(height - 1) * width + j].decay * time_delta;
  26. }
  27. // Decay and increase
  28. #pragma omp parallel for
  29. for(size_t i = 0 ; i < height ; ++i){
  30. for(size_t j = 0 ; j < width ; ++j){
  31. size_t k = i * width + j;
  32. terrain[k].pheromone += (terrain[k].increase - terrain[k].decay) * time_delta;
  33. if(terrain[k].pheromone < 0.0f)
  34. terrain[k].pheromone = 0.0f;
  35. }
  36. }
  37. // diffusion
  38. const float pourcentage = 2.5f;
  39. for(size_t i = 1 ; i < height -1 ; ++i){
  40. for(size_t j = 1 ; j < width -1 ; ++j){
  41. if(terrain[i * width + j].pheromone == 0.0f)
  42. continue;
  43. terrain[(i - 1) * width + j].pheromone += terrain[i * width + j].pheromone * pourcentage * time_delta;
  44. terrain[(i + 1) * width + j].pheromone += terrain[i * width + j].pheromone * pourcentage * time_delta;
  45. terrain[i * width + j - 1].pheromone += terrain[i * width + j].pheromone * pourcentage * time_delta;
  46. terrain[i * width + j + 1].pheromone += terrain[i * width + j].pheromone * pourcentage * time_delta;
  47. terrain[i * width + j].pheromone -= terrain[i * width + j].pheromone * 4 * pourcentage * time_delta;
  48. }
  49. }
  50. }
  51. #endif //TERRAIN