% dog catching mailman problem
% discrete time event simulation
% cda 6530 
% project 5
% PID: j2966213
% name: Jun Ye

maxIter = 500;% maximum iteration
timeStep = 0.2;% time step



%type 1 mailman
dog_x = zeros(maxIter);
dog_y = 20*ones(maxIter);
mailman_x = 10* ones(maxIter);
%type 2 mailman

dog_x2 = zeros(maxIter);
dog_y2 = 20*ones(maxIter); 
mailman_x2 = 10*ones(maxIter);  


mailman_y = zeros(maxIter);% it is constant, actually not useful
mailman_y2 = zeros(maxIter);

%type 2 dog and type 1 mailman
%main loop

stepCnt = 0;
tRun = timeStep;
for i=2:maxIter
    stepCnt = stepCnt+1;
    curTime = timeStep*(i-1); % current time
    slip = DogSlip(tRun);
    if slip == 1 % slip happens
        dog_speed = 0;
        tRun = 0;
    else         % slip does not happen
        if tRun < 4
            dog_speed = 3*tRun;
        else
            dog_speed = 12;
        end
        % tRun increases
        tRun = tRun+ timeStep;
    end
            
    if curTime <= 3
        mailman_speed = 0; % stand still to tie shoe lance
    else
        mailman_speed = 5;
    end
    
    
    
    % type 1 mailman
    distance = sqrt((abs(dog_x(i-1) - mailman_x(i-1)))^2 +(dog_y(i-1))^2);
    delta_dog_x = dog_speed*(mailman_x(i-1)-dog_x(i-1))/distance*timeStep;
    delta_dog_y = dog_speed*dog_y(i-1)/distance*timeStep;
    delta_mailman_x = mailman_speed*timeStep;
       
    %update the new position after the iteration
    dog_x(i) = dog_x(i-1) + delta_dog_x;
    dog_y(i) = dog_y(i-1) - delta_dog_y;
    
    if dog_y(i) < 0  % cross the x-axis
        dog_y(i) = 0;
        dog_x(i) = mailman_x(i-1);
    end
    mailman_x(i) = mailman_x(i-1) + delta_mailman_x;
    

    if (dog_y(i) == 0) && (dog_x(i)>= mailman_x(i))
        dog_x(i) = mailman_x(i);
        break;
    end
          
end

dog_x = dog_x(1: stepCnt+1);
dog_y = dog_y(1: stepCnt+1);
mailman_x = mailman_x(1: stepCnt+1);
mailman_y = mailman_y(1: stepCnt+1);


% type 2 dog and type 2 mailman
%main loop

stepCnt2 = 0;
tRun = timeStep;
for i=2:maxIter
    stepCnt2 = stepCnt2+1;
    curTime = timeStep*(i-1); % current time
    slip = DogSlip(tRun);
    if slip == 1 % slip happens
        dog_speed = 0;
        tRun = 0;
    else
        if tRun < 4
            dog_speed = 3*tRun;
        else
            dog_speed = 12;
        end
        tRun = tRun+ timeStep;
    end
    
    mailman_speed2 = 2.5;
    
    %current distance
    % type 2 mailman
    distance = sqrt((abs(dog_x2(i-1) - mailman_x2(i-1)))^2 +(dog_y2(i-1))^2);
    delta_dog_x = dog_speed*(mailman_x2(i-1)-dog_x2(i-1))/distance*timeStep;
    delta_dog_y = dog_speed*dog_y2(i-1)/distance*timeStep;
    delta_mailman_x = mailman_speed2*timeStep;
       
    %update the new position after the iteration
    dog_x2(i) = dog_x2(i-1) + delta_dog_x;
    dog_y2(i) = dog_y2(i-1) - delta_dog_y;
    
    if dog_y2(i) < 0  % cross the x-axis
        dog_y2(i) = 0;
        dog_x2(i) = mailman_x2(i-1);
    end
    mailman_x2(i) = mailman_x2(i-1) + delta_mailman_x;
    

    if (dog_y2(i) == 0) && (dog_x2(i)>= mailman_x2(i))
        dog_x2(i) = mailman_x2(i);
        break;
    end
          
end

dog_x2 = dog_x2(1: stepCnt2+1);
dog_y2 = dog_y2(1: stepCnt2+1);
mailman_x2 = mailman_x2(1: stepCnt2+1);
mailman_y2 = mailman_y2(1: stepCnt2+1);


plot(dog_x, dog_y, 'b+-', mailman_x, mailman_y, 'ro-');
hold;
plot(dog_x2, dog_y2, 'bs--', mailman_x2, mailman_y2, 'r^-');
xlabel('y(t)');
ylabel('x(t)');
title('Dog Chasing Mailman--Discrete Time Simulation (Type 2 dog)');
legend('dog Type 2 chasing mailman Type1','mailman Type 1','dog Type 2 chasing mailman Type2','mailman Type2','location','northeast');
