ID:50854
Title:Free Child v0.1
Author:Nick Howe
Date:2008-11-09 11:58:45
Score:23387.9017
Result:23193.50 (cyc: 21, node: 689)
CPU Time:112.6572
Status:Passed
Comments:
Based on:none
Code:
function [dRow,dCol,action,mark] = freechild(mainMap,foodMap,myAntMap,opAntMap, ...
    myScentMap,opScentMap,myDeathMap,opDeathMap)

% Commented by Alan Chalker
% PLEASE LEAVE THE COMMENTS IN IF YOU REUSE!
%
% Setup obstacle mask
mask = isnan(mainMap); % Find existing obstacles
%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
mY = [7 12 17; 8 13 18; 9 14 19]; % Mask for immediate neighbours
Y = mY(:);
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

% 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)&(myScent<1000)
    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
if opAntMap(13)||(any(opAntMap(Y))&&sum(opAntMap(:)>=8)&&(rand < 1/(myAntMap(13)-1)))
    action = -1;
    dRow = 0;
    dCol = 0;
    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 > (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
            [r,c] = find(~mask(mY));
            if isempty(r)
                return;
            end;
            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(~mask&(myScentMap==maxScent));
            if isempty(r)
                return;
            end;
            pick = ceil(rand*numel(r));
            dRow = sign(r(pick)-3); % convert to contest directions
            dCol = sign(c(pick)-3);
        end
    else % if food in view further away from base, go towards it
        % gather lower
        lowFoodPositive = lowFood > 0;
        [r,c] = find(~mask&lowFoodPositive&(myScentMap==min(myScentMap(lowFoodPositive)))); % find visible areas of food with less scent trail
        if isempty(r)
            return;
        end;
            pick = ceil(rand*numel(r));
            dRow = sign(r(pick)-3); % convert to contest directions
            dCol = sign(c(pick)-3);
    end
elseif any(netFood(:) > 0) % else I'm not on food, go towards any available food
    % gather
    [r,c] = find(~mask&netFoodPositive&(myScentMap<myScent)); % find direction of food furthest away from base
    if isempty(r)
        return;
    end;
    %pick = ceil(rand*numel(pick));
    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>.82 % move irregardless of scent trail part of the time
        [r,c] = find(~mask(mY));
        if isempty(r)
            return;
        end;
        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
        [r,c] = find(~mask&(myScentMap==min(myScentMap(~mask(:))))); % 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