function [thrustRow, thrustCol] = solver(chart, aIndex, bIndex, maxThrottle)
% Slightly improved basic solver, does something more
bestResult=1000000;
for maxSpeed=1:30
for maxSteps=1:30;
%Initialize start and target
[currRow, currCol] = ind2sub(size(chart(:,:,1)),aIndex);
[targRow, targCol] = ind2sub(size(chart(:,:,1)),bIndex);
vRow=0;
vCol=0;
panic=0;
bestDistance=ones(2,1)*100000;
%Dummy entries
thrustRow=ones(maxSteps,1)*(maxThrottle+1);
thrustCol=ones(maxSteps,1)*(maxThrottle+1);
%Way to the target and back to the start
for k=1:2
j=0;
while 1
j=j+1;
i=(k-1)*maxSteps+j;
%Panic escape
if(panic==1)
thrustRow=[];
thrustCol=[];
break;
end
%Add the chart entries for the current position to the speed
vRow=vRow+chart(currRow,currCol,1);
vCol=vCol+chart(currRow,currCol,2);
%Go as fast as possible
thrustRow(i)=sign(targRow-currRow-vRow)*min(abs(targRow-currRow-vRow),maxThrottle);
thrustCol(i)=sign(targCol-currCol-vCol)*min(abs(targCol-currCol-vCol),maxThrottle);
%Try to limit speed to avoid exiting the map
if(abs(vRow+thrustRow(i))>maxSpeed)
thrustRow(i)=sign(thrustRow(i))*(maxSpeed-sign(thrustRow(i))*vRow);
end
if(abs(vCol+thrustCol(i))>maxSpeed)
thrustCol(i)=sign(thrustCol(i))*(maxSpeed-sign(thrustCol(i))*vCol);
end
%Take care of Throttle limitation
if((abs(thrustRow(i))+abs(thrustCol(i)))>maxThrottle)
correction=ceil(((abs(thrustRow(i))+abs(thrustCol(i)))-maxThrottle)/2);
thrustRow(i)=thrustRow(i)-sign(thrustRow(i))*correction;
thrustCol(i)=thrustCol(i)-sign(thrustCol(i))*correction;
end
%Check if this exits the map
if(((currRow+vRow+thrustRow(i))<1)||((currRow+vRow+thrustRow(i))>size(chart,1))||((currCol+vCol+thrustCol(i))<1)||((currCol+vCol+thrustCol(i))>size(chart,2)))
panic=1;
end
%Adjust speed for next step
vRow=vRow+thrustRow(i);
vCol=vCol+thrustCol(i);
%Position update
currRow=currRow+vRow;
currCol=currCol+vCol;
%Performance check
if(k==1)
targDistance=(currRow-targRow)^2+(currCol-targCol)^2;
if(targDistance<bestDistance(k))
bestDistance(k)=targDistance;
end
end
%Reached the target or the maximum number of steps?
if(j==maxSteps || (sum(abs(currRow-targRow)+abs(currCol-targCol))==0))
if(k==2)
bestDistance(2)=(currRow-targRow)^2+(currCol-targCol)^2;
end
break;
end
end
%Target for the second run is the start
[targRow, targCol] = ind2sub(size(chart(:,:,1)),aIndex);
end
%Remove dummy entries
thrustRow=thrustRow(thrustRow~=(maxThrottle+1));
thrustCol=thrustCol(thrustCol~=(maxThrottle+1));
%Performance check
result = sum(bestDistance)+sum(abs(thrustRow))+sum(abs(thrustCol));
if(result<bestResult)
bestResult=result;
bestthrustRow=thrustRow;
bestthrustCol=thrustCol;
end
end
end
%Output
thrustRow=bestthrustRow;
thrustCol=bestthrustCol;
|