function [dRow,dCol,action,mark] = solver(mainMap,foodMap,myAntMap,opAntMap, ...
myScentMap,opScentMap,myDeathMap,opDeathMap)
% PLEASE LEAVE THE COMMENTS IN IF YOU REUSE!
%
% Setup obstacle mask
K = [7 7 7 9 9 9 17 17 17 19 19 19 8 12 14 18]'; % Indexes for just immediate neighbours
L = [1 2 6 4 5 10 16 21 22 20 24 25 3 11 15 23]'; % Mapping index to outer neighbours
mainMap(L(isnan(mainMap(K)))) = nan;
mask = isnan(mainMap);
% Setup scent trail to be decreasing further from bases
Y = [7 8 9 12 13 14 17 18 19]'; % Mask for immediate neighbours
myScent = myScentMap(13);
H = mainMap == 1; % Make for any bases in view
if H(13)
mark = 100; % If at base, make maximium scent
else
mark = floor((max(myScentMap(Y))-myScent-1)/myAntMap(13)); % Else look for max neighbouring scent and make current 1 less than it, accounting for number of ants presents
end
myScentMap(H) = inf; %mask bases as maximium scent
% fight if challenged
if (any(opAntMap(Y))) % check if enemy ants are immediate neighbours
action = -1; % if so, set action to don't move and fight
dRow = 0;
dCol = 0;
if (sum(myAntMap(:))<8)||(rand < 0.3) % check if not enough friends in view or fight half the time
return
else % else check to see if enemy ants are all to one side of me so I can 'block' them
[r,c] = find(opAntMap(2:4,2:4)&~opAntMap(4:-1:2,4:-1:2));
if isempty(r)
return
end
end
end
% look for food in view
action = 0;
foodMap(H) = 0; % mask out food at bases
netFood = foodMap-myAntMap; %determine food to ant ratios
netFood(mask|H) = 0; % mask out food behind obstacles
netFoodPositive = netFood > 0; % find food 'caches'
myFood = foodMap(13);
if (myFood > 0)&&(rand > (myAntMap(13)-myFood)/myAntMap(13)) % if I'm sitting on food and my food to ant ratio is appropriate
lowFood = netFoodPositive&(myScentMap<myScent); % find neighbours further away from base with available food
if ~any(lowFood(:))||((myFood > 1)&&(rand > sqrt(1/myFood))) % if non available and enough food is present, carry
% carry home
action = 1;
myScentMap(mask) = -inf; %mask areas behind obstacles
myScentMap(13) = -inf;
maxScent = max(myScentMap(:)); %find direction of highest scent
if (maxScent == 0) % if no trail available, select random direction to go
mainMap(13) = nan;
[r,c] = find(~isnan(mainMap(2:4,2:4))); % mask out obstacles as possible directions
pick = ceil(rand*numel(r)); % pick random direction
dRow = sign(r(pick)-2); % convert to -1, 0 or 1 as needed by contest
dCol = sign(c(pick)-2);
else % else find direction of max trail and go towards it
[r,c] = find(myScentMap==maxScent);
dRow = sign(r(1)-3); % convert to contest directions
dCol = sign(c(1)-3);
end
else % if food in view further away from base, go towards it
% gather lower
lowFoodPositive = lowFood > 0;
[r,c] = find(lowFoodPositive&(myScentMap==min(myScentMap(lowFoodPositive)))); % find visible areas of food with less scent trail
dRow = sign(r(1)-3); % convert to contest directions
dCol = sign(c(1)-3);
end
elseif any(netFood(:) > 0) % else I'm not on food, go towards any available food
% gather
[r,c] = find(netFoodPositive&(myScentMap==min(myScentMap(netFoodPositive)))); % find direction of food furthest away from base
dRow = sign(r(1)-3); % convert to contest directions
dCol = sign(c(1)-3);
else % else no food in view so randomly move
% explore
if rand>.82 % move irregardless of scent trail part of the time
mainMap(13) = nan;
[r,c] = find(~isnan(mainMap(2:4,2:4))); % find nonblocked directions
pick = ceil(rand*numel(r)); % pick one
dRow = sign(r(pick)-2); % convert to contest directions
dCol = sign(c(pick)-2);
else % most of the time move away from base using scent trail
myScentMap(mask) = inf; %mask obstacled areas
myScentMap(13) = inf;
[r,c] = find(myScentMap==min(myScentMap(:))); % find directions of min scent trail
pick = ceil(rand*numel(r)); % randomly pick one
dRow = sign(r(pick)-3); % convert to contest directions
dCol = sign(c(pick)-3);
end
end
end
|