# COSC/MATH 201 - Spreading of Fire (Module 10.3)
# A cellular automaton: fire spreads from one burning tree through a forest grid,
# then repeated forests show how tree density changes the fraction that burns.

EMPTY <- 0
TREE <- 1
BURNING <- 2

set.seed(1031)
n <- 10
prob_tree <- 0.7

forest <- matrix(EMPTY, nrow = n, ncol = n)    # start with all bare ground
forest[runif(n * n) < prob_tree] <- TREE       # one random draw per cell
forest
sum(forest == TREE)

forest_colors <- c("wheat", "forestgreen", "orangered")  # EMPTY, TREE, BURNING

image(t(forest[n:1, ]), col = forest_colors, zlim = c(0, 2),
      axes = FALSE, asp = 1, main = "A 10 × 10 forest, density 0.7")

set.seed(1032)
n <- 40
prob_tree <- 0.7

forest <- matrix(EMPTY, nrow = n + 2, ncol = n + 2)    # includes the ring
inside <- 2:(n + 1)
forest[inside, inside][runif(n * n) < prob_tree] <- TREE
center <- n / 2 + 1
forest[center, center] <- BURNING

new_forest <- forest

for (i in inside) {
  for (j in inside) {
    if (forest[i, j] == BURNING) {
      new_forest[i, j] <- EMPTY                 # burns down
    } else if (forest[i, j] == TREE) {
      if (forest[i - 1, j] == BURNING || forest[i, j + 1] == BURNING ||
          forest[i + 1, j] == BURNING || forest[i, j - 1] == BURNING) {
        new_forest[i, j] <- BURNING             # catches fire
      }
    }
  }
}

forest <- new_forest
sum(forest == BURNING)

set.seed(1032)
forest <- matrix(EMPTY, nrow = n + 2, ncol = n + 2)
forest[inside, inside][runif(n * n) < prob_tree] <- TREE
forest[center, center] <- BURNING

par(mfrow = c(2, 3), mar = c(1, 1, 2, 1))

for (t in 0:30) {
  if (t %in% c(0, 4, 8, 12, 20, 30)) {
    image(t(forest[(n + 2):1, ]), col = forest_colors, zlim = c(0, 2),
          axes = FALSE, asp = 1, main = paste("t =", t))
  }
  new_forest <- forest
  for (i in inside) {
    for (j in inside) {
      if (forest[i, j] == BURNING) {
        new_forest[i, j] <- EMPTY
      } else if (forest[i, j] == TREE) {
        if (forest[i - 1, j] == BURNING || forest[i, j + 1] == BURNING ||
            forest[i + 1, j] == BURNING || forest[i, j - 1] == BURNING) {
          new_forest[i, j] <- BURNING
        }
      }
    }
  }
  forest <- new_forest
}

par(mfrow = c(1, 1))

set.seed(1033)
n <- 30
inside <- 2:(n + 1)
center <- n / 2 + 1
densities <- seq(0.3, 0.9, by = 0.05)
runs <- 20
mean_burned <- numeric(length(densities))

for (k in seq_along(densities)) {
  burned <- numeric(runs)
  for (run in 1:runs) {
    forest <- matrix(EMPTY, nrow = n + 2, ncol = n + 2)
    forest[inside, inside][runif(n * n) < densities[k]] <- TREE
    forest[center, center] <- BURNING
    trees_at_start <- sum(forest != EMPTY)

    while (any(forest == BURNING)) {
      new_forest <- forest
      for (i in inside) {
        for (j in inside) {
          if (forest[i, j] == BURNING) {
            new_forest[i, j] <- EMPTY
          } else if (forest[i, j] == TREE) {
            if (forest[i - 1, j] == BURNING || forest[i, j + 1] == BURNING ||
                forest[i + 1, j] == BURNING || forest[i, j - 1] == BURNING) {
              new_forest[i, j] <- BURNING
            }
          }
        }
      }
      forest <- new_forest
    }

    burned[run] <- (trees_at_start - sum(forest == TREE)) / trees_at_start
  }
  mean_burned[k] <- mean(burned)
}

data.frame(density = densities, mean_fraction_burned = round(mean_burned, 2))

plot(densities, mean_burned, type = "o", pch = 16, col = "orangered",
     ylim = c(0, 1), xlab = "Tree density (prob_tree)",
     ylab = "Mean fraction of trees burned",
     main = "One fire in a 30 × 30 forest (20 forests per density)")
