Plague-fx/src/epidemic_engine.c

83 lines
2.7 KiB
C
Raw Normal View History

2021-06-01 11:36:26 +02:00
#include <gint/std/stdlib.h>
2021-05-30 21:33:32 +02:00
#include "epidemic_engine.h"
2021-06-01 11:36:26 +02:00
2021-06-01 14:58:37 +02:00
int grid_get(const struct grid epidemic_grid, const int i, const int j)
{
2021-06-02 21:15:15 +02:00
if (i < 0 || j < 0 || i >= epidemic_grid.width || j >= epidemic_grid.height) return 0;
else return epidemic_grid.data[i + j * epidemic_grid.width];
2021-06-01 14:58:37 +02:00
}
bool can_become_infected(const struct grid epidemic_grid, const int mutations_selected[3], const int i, const int j)
2021-05-30 21:33:32 +02:00
{
2021-06-01 11:36:26 +02:00
extern const unsigned int world[64][128];
// In case of water, low or high temperature
2021-06-02 21:15:15 +02:00
/*if (world[j][i] == 0 && mutations_selected[2] != 3) return false;
2021-06-01 14:58:37 +02:00
if (world[j][i] == 1 && mutations_selected[1] != 0 && mutations_selected[1] != 4) return false;
2021-06-02 21:15:15 +02:00
if (world[j][i] == 3 && mutations_selected[1] != 1 && mutations_selected[1] != 4) return false;*/
2021-06-01 11:36:26 +02:00
2021-06-01 14:58:37 +02:00
// Test cases around
return grid_get(epidemic_grid, i - 1, j) == 1 || grid_get(epidemic_grid, i + 1, j) == 1 || grid_get(epidemic_grid, i, j - 1) == 1 || grid_get(epidemic_grid, i, j + 1) == 1;
2021-05-30 21:33:32 +02:00
}
2021-06-01 11:36:26 +02:00
2021-06-02 21:15:15 +02:00
bool bernoulli(const int p)
2021-05-30 21:33:32 +02:00
{
2021-06-02 21:15:15 +02:00
return (rand() % 101) <= p;
}
void epidemic_simulation(struct game *current_game)
{
2021-06-01 14:58:37 +02:00
srand(15510);
2021-06-01 11:36:26 +02:00
2021-06-02 21:15:15 +02:00
int healed_rate = 100 * (current_game->research / current_game->limit);
2021-06-01 14:58:37 +02:00
for (int i = 0; i < current_game->grid.width; i ++)
2021-06-01 11:36:26 +02:00
{
2021-06-01 14:58:37 +02:00
for (int j = 0; j < current_game->grid.height; j ++)
2021-06-01 11:36:26 +02:00
{
2021-06-02 21:15:15 +02:00
current_game->grid.data[i + j * current_game->grid.width] = 1;
/*
2021-06-01 11:36:26 +02:00
switch (current_game->grid.data[i + j * current_game->grid.width])
{
// Healthy
2021-06-02 21:15:15 +02:00
case 0:
// Become infected
if (can_become_infected(current_game->grid, current_game->mutations_selected, i, j) && bernoulli(current_game->contagion))
2021-06-01 11:36:26 +02:00
{
current_game->grid.data[i + j * current_game->grid.width] = 1;
current_game->humans[0] --;
current_game->humans[1] ++;
}
break;
// Infected
case 1:
// Become healed
2021-06-01 14:58:37 +02:00
if (bernoulli(healed_rate))
2021-06-01 11:36:26 +02:00
{
2021-06-01 14:58:37 +02:00
current_game->grid.data[i + j * current_game->grid.width] = 2;
2021-06-01 11:36:26 +02:00
current_game->humans[1] --;
2021-06-01 14:58:37 +02:00
current_game->humans[2] ++;
2021-06-01 11:36:26 +02:00
}
// Become dead
2021-06-02 21:15:15 +02:00
else if (bernoulli(current_game->lethality))
2021-06-01 11:36:26 +02:00
{
2021-06-01 14:58:37 +02:00
current_game->grid.data[i + j * current_game->grid.width] = 3;
2021-06-01 11:36:26 +02:00
current_game->humans[1] --;
2021-06-01 14:58:37 +02:00
current_game->humans[3] ++;
2021-06-01 11:36:26 +02:00
}
break;
2021-06-02 21:15:15 +02:00
}*/
2021-06-01 11:36:26 +02:00
}
}
2021-06-02 21:15:15 +02:00
}