ID:50873
Title:Nearly immortal 8
Author:Markus
Date:2008-11-09 11:59:33
Score:21635.3040
Result:19669.72 (cyc: 19, node: 737)
CPU Time:148.2175
Status:Passed
Comments:
Based on:none
Code:
function [dRow,dCol,action,mark] = solver_fe6_10_5_mod(mainMap,foodMap,myAntMap,opAntMap, ...
	myScentMap,opScentMap,myDeathMap,opDeathMap,myPos,timeStep)

% Commented by Alan Chalker
% PLEASE LEAVE THE COMMENTS IN IF YOU REUSE!
%
% Setup obstacle mask
mainMap(opAntMap > 0) = NaN; % pretend opposing ants are walls
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
Y = [7 8 9 12 13 14 17 18 19]; % Mask for immediate neighbours
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
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
if (any(opAntMap(Y))) % check if enemy ants are immediate neighbours
	action = -1; % if so, set action to don't move and fight
	if sum(myAntMap(:))<6 || rand<0.5 || sum(opAntMap(Y))>19 % fight conditions
		return
	end
end

%% Food Search
% look for food in view
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);
myScentMap(mask) = 0;
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
		if isempty(r)
			dRow=0;
			dCol=0;
			action = -1;
			return
		end
		pick = ceil(rand*numel(r)); % pick one
		dRow = sign(r(pick)-2); % convert to contest directions
		dCol = sign(c(pick)-2);
	elseif rand < 0.05 % move to opponent base
		opScentMap(mask) = -inf; %mask obstacled areas
		opScentMap(13)   = -inf;
		[r,c] = find(opScentMap==max(opScentMap(:))); % 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);
	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
if isnan(mainMap(dRow+3,dCol+3))
	dRow = 0;
	dCol = 0;
end
if dRow==0 && dCol==0
	action=-1;
end