Permalink
Browse files

Chase hills when they are at sight.

  • Loading branch information...
1 parent c9dabc6 commit abe7eb7b8d23541b09924ac826ed354e0d6c25f8 @liberforce committed Nov 6, 2011
Showing with 73 additions and 1 deletion.
  1. +1 −1 Makefile
  2. +18 −0 hill.c
  3. +6 −0 hill.h
  4. +48 −0 termite.c
View
@@ -8,7 +8,7 @@ LDFLAGS=-O0 -lm
#CFLAGS=-O3 -funroll-loops -c -Wall -DNDEBUG
#LDFLAGS=-O2 -lm
-SOURCES=MyBot.c termite.c ant.c map.c utils.c state.c tile.c pathfinder.c
+SOURCES=MyBot.c termite.c ant.c map.c utils.c state.c tile.c pathfinder.c hill.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=termite
ARCHIVE=$(EXECUTABLE).zip
View
@@ -0,0 +1,18 @@
+#include <assert.h> // for assert
+#include <stdlib.h> // for NULL
+
+#include "hill.h"
+
+inline guint hill_get_owner (Hill *hill)
+{
+ assert (hill != NULL);
+ return hill->owner;
+}
+
+inline void hill_set_owner (Hill *hill, guint owner)
+{
+ assert (hill != NULL);
+ assert (owner < 26); // owner is in the [0;25] range
+ hill->owner = owner;
+}
+
View
@@ -1,9 +1,15 @@
#ifndef HILL_H
#define HILL_H
+#include "types.h"
+
typedef struct hill
{
guint owner;
} Hill;
+guint hill_get_owner (Hill *hill);
+
+void hill_set_owner (Hill *hill, guint owner);
+
#endif // HILL_H
View
@@ -16,8 +16,21 @@ void termite_play_turn (Rules *rules,
guint distance; // Manhattan distance
Tile *food;
Tile *ant;
+ Tile *hill;
gchar dir;
+ // Keep enemy hills only
+ for (i = 0; i < state->n_hills; ++i)
+ {
+ Tile *tile = state->hills[i];
+ if (hill_get_owner (&tile->with.hill) == 0)
+ {
+ state->hills[i] = state->hills[state->n_hills - 1];
+ state->n_hills--;
+ }
+ }
+
+ // Process each ant
while (state->n_ants > 0)
{
// Pre-select first ant
@@ -28,8 +41,40 @@ void termite_play_turn (Rules *rules,
food_index = -1;
food = NULL;
+ // Pre-select no hill
+ hill = NULL;
+
dir = DIR_NONE;
+ // Look for enemy hills to destroy
+ if (dir == DIR_NONE && state->n_hills > 0)
+ {
+ // Find nearest enemy hill for that ant
+ g_debug ("Ant = [%d,%d], searching nearest hill...\n", ant->row, ant->col);
+
+ distance = G_MAX_UINT;
+ for (i = 0; i < state->n_hills; ++i)
+ {
+ guint d = map_get_manhattan_distance (state->map,
+ ant,
+ state->hills[i]);
+ if (d < distance)
+ {
+ distance = d;
+ hill = state->hills[i];
+ ant_index = i;
+ }
+ g_debug ("Distance to hill [%d,%d] = %d (closest = %d)\n",
+ state->hills[i]->row,
+ state->hills[i]->col,
+ d,
+ distance);
+ }
+
+ // Find path to go there !
+ dir = pathfinder_get_closest_direction (state->pf, ant, hill);
+ }
+
if (dir == DIR_NONE && state->n_food > 0)
{
if (state->n_food < state->n_ants)
@@ -129,6 +174,9 @@ void termite_play_turn (Rules *rules,
state->ants[ant_index] = state->ants[state->n_ants - 1];
state->n_ants--;
}
+
+ // We don't handle hills the same way as food, so that several
+ // ants can chase the same hill
}
// Inform the server we finished sending our actions for the turn

0 comments on commit abe7eb7

Please sign in to comment.