# Now each wolf has its subset of cells as potential next locations for dispersal
# Probability of going to the potential next locations regarding their directions
headDispersers <- of(agents = dispersers, var = "heading")
nextLocs[,nextAngle:={
dnorm(mean = 0, sd = params(sim)$wolfAlps$sigma, # calculate the probability using the Normal distribution of ...
subHeadings(angle1 = headDispersers[id], # ... the rotation of each wolf's heading to ...
angle2 = towards(agents = turtle(sim$wolves, who = whoDispersers[id]), # ... the direction towards each of its next potential locations
agents2 = cbind(x = x, y = y))
)
)}, by = id] # data.table use of by = id, so each of the above happens within each id
# Probability of going to the potential next locations regarding habitat suitability
data.table::set(nextLocs,,"suitabilityValGood", sim$suitabilityValGood[nextLocs$indices])
# Probability of going to the potential next locations regarding the directions, habitat suitability and other wolves presence
data.table::set(nextLocs,,"prob", nextLocs$nextAngle * nextLocs$suitabilityValGood * nextLocs$empty)
probLoc <- runif(n = NLcount(dispersers), min = 0, max = 1)
setkeyv(nextLocs, c("id"))
# Selected next potential locations, based on probLoc
nextLocs <- nextLocs[,.SD[findInterval(probLoc[id], cumsum(prob/sum(prob))) + 1],
by = id, .SDcols = c("x", "y", "prob")]
selectedLocID <- as.matrix(nextLocs)[,c(2,3,1), drop = FALSE]