clear all;
close all;

N = 100;

initialProbInfection = 0.001;
infectionRate = 0.1;
healRate = 0.05;
dieAfter = 10;
latentUntil = 4;


pop = zeros(N+2);
pop(2:N+1,2:N+1) = ones(N); %+ (rand(N)<initialProbInfection);
pop(2,2)= 2;
pop(1,1) = dieAfter+2;
newpop = pop;

imagesc(pop);
map = zeros(dieAfter+2,3);
map(1,:)= [0 1 0];
map(2,:)= [1 1 1];
map(3:end,1) = flipud(linspace(1,0,dieAfter));
colormap(map);

for t = 1:100
    imagesc(pop);
    colormap(map);
   for i = 2:N+1
       for j = 2:N+1
           guy = pop(i,j);
           if guy == 0
               continue;
           elseif guy >= dieAfter+2
               continue;
           elseif guy > 1 && guy < latentUntil
               newpop(i,j) = guy + 1;
           elseif guy >= latentUntil
               if rand < healRate
                   newpop(i,j) = 0;
               else
                   newpop(i,j) = guy + 1;
               end
           elseif guy == 1
               newpop(i,j) = 1 + (sum(sum((rand(3)<infectionRate)...
                   & (pop(i-1:i+1,j-1:j+1) > 1)...
                   & (pop(i-1:i+1,j-1:j+1) < dieAfter + 2))) >= 1);
           end
       end
   end
   pop = newpop;
   pause(0.01);
end
