%Simple erosion model
%By Jeffrey Smith
clear
clf
f=1;    %For the saved frames
x = 100; %x spacial dimension of landscape
y = 100; %y spacial dimension of landscape

z = setelev2(x,y);

drawlandscape(x,y,z)

location = rain(x,y);
nx = location(1);
ny = location(2);

randcount = 0;

for i = 1:330000

    
    if ( nx == 0 )
        
        location = rain(x,y);
        nx = location(1);
        ny = location(2);
        
    end
   
    if ( z(nx,ny) > 0 )
        z(nx,ny) = (z(nx,ny)) - 1;
    else
        location = rain(x,y);
        nx = location(1);
        ny = location(2);
    end
    
    newindex = flownew2(x,y,nx,ny,z);
    
    
    nx = newindex(1);
    ny = newindex(2);
    
 
    
    if (mod(i,8000) == 0) 
        
        drawlandscape(x,y,z)
        camorbit(i/7500,75,'camera')
        pause(0.05)
    end
    
    if (mod(i,5000) == 0 | i==1)
        saveas(gcf, ['frame_',sprintf('%03d',f),'.png']);
        f=f+1;
    end
    
end

view([90 90])

    