| Code: | function [dRow,dCol,action,mark] = solverj(mainMap,foodMap,myAntMap,opAntMap, ...
myScentMap,opScentMap,myDeathMap,opDeathMap)
% Commented by Alan Chalker
% PLEASE LEAVE THE COMMENTS IN IF YOU REUSE!
% NOTE:
% 1. Nicholas Howe fixed the ants' killing bug in the original entry
% 2. a bug in the nh's entry fixed, and some cleaning done - Jin
% 3. ADD ANTI-BLOCKING CODE by Fel
% Setup obstacle mask
mask = isnan(mainMap); % Find existing obstacles
L = [1 2 6 4 5 10 16 21 22 20 24 25 3 11 15 23]'; % Mapping index to outer neighbours
mY = [7 12 17; 8 13 18; 9 14 19]; % Mask for immediate neighbours
Y = mY(:);
% ANTI-BLOCKING CODE
%-----------------------------------------
origMask = mask;
origMask(L(origMask([7 7 7 9 9 9 17 17 17 19 19 19 8 12 14 18]))) = true; %Fill in outer neighbours if inner neighbour is an obstacle
origScentMap = myScentMap;
origScentMap(origMask) = -inf;
origScentMap(13) = -inf;
%-----------------------------------------
mask(Y) = mask(Y)|(opAntMap(Y)>0); % make sure we don't blunder into an opposing ant!
mask(L(mask([7 7 7 9 9 9 17 17 17 19 19 19 8 12 14 18]))) = true; %Fill in outer neighbours if inner neighbour is an obstacle
mainMap(mask) = nan; % Apply mask to map
unmask = ~mask;
% Default Actions are no actions.
action = -1;
dRow = 0;
dCol = 0;
% Setup scent trail to be decreasing further from bases
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 Mode
% ignore enemies unless on same square
% but avoid stepping on them
% SY = sum(opAntMap(Y));
if opAntMap(13) && sum(opAntMap(Y))>5
return
end;
%% Food Search
% look for food in view
foodMap(H) = 0; % mask out food at bases
foodMap(opAntMap>0) = 0; % mask out guarded food
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 > (1-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
[r,c] = find(unmask(mY));
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
% ANTI-BLOCKING CODE
originalMaxScent = max(origScentMap(:));
if (originalMaxScent > maxScent) && rand > 0.9
% I'm possibly blocked
[r,c] = find(~mask&(myScentMap>0));
if isempty(r)
return;
end
pick = ceil(rand*numel(r));
dRow = sign(r(pick)-3); % convert to contest directions
dCol = sign(c(pick)-3);
else
[r,c] = find(unmask&(myScentMap==maxScent));
if isempty(r)
[r,c] = find(unmask(mY));
pick = ceil(rand*numel(r)); % pick one
dRow = sign(r(pick)-2); % convert to contest directions
dCol = sign(c(pick)-2);
return;
end;
pick = ceil(rand*numel(r));
dRow = sign(r(pick)-3); % convert to contest directions
dCol = sign(c(pick)-3);
end
end
else % if food in view further away from base, go towards it
% gather lower
lowFoodPositive = lowFood > 0;
[r,c] = find(unmask&lowFoodPositive&(myScentMap==min(myScentMap(lowFoodPositive)))); % find visible areas of food with less scent trail
if isempty(r)
[r,c] = find(unmask(mY));
pick = ceil(rand*numel(r)); % pick one
dRow = sign(r(pick)-2); % convert to contest directions
dCol = sign(c(pick)-2);
return;
end;
pick = ceil(rand*numel(r));
dRow = sign(r(pick)-3); % convert to contest directions
dCol = sign(c(pick)-3);
end
elseif any(any(netFoodPositive)) % else I'm not on food, go towards any available food
% gather
[r,c] = find(unmask&netFoodPositive&(myScentMap==min(myScentMap(netFoodPositive)))); % find direction of food furthest away from base
if isempty(r)
return;
end;
pick = ceil(rand*numel(r));
dRow = sign(r(pick)-3); % convert to contest directions
dCol = sign(c(pick)-3);
else % else no food in view so randomly move
% explore
if rand>.816 % move irregardless of scent trail part of the time
[r,c] = find(unmask(mY));
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
mmm=min(myScentMap(unmask(:)));
if isempty(mmm)
[r,c] = find(unmask(mY));
pick = ceil(rand*numel(r)); % pick one
dRow = sign(r(pick)-2); % convert to contest directions
dCol = sign(c(pick)-2);
return;
end;
[r,c] = find(unmask&(myScentMap==mmm)); % find directions of min scent trail
if isempty(r)
return;
end;
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 |