summary refs log tree commit diff
diff options
context:
space:
mode:
authorvenomade <venomade@venomade.com>2025-02-27 17:30:36 +0000
committervenomade <venomade@venomade.com>2025-02-27 17:30:36 +0000
commit2155be63d154d7d613c6227cc0376144fa254c03 (patch)
tree6b6255af5987a921577157f7396d2deaf45ce150
parent259c727658485ea00d6ef8617ecab579be871470 (diff)
Add experimental branch experimental
-rw-r--r--src/game.c104
-rw-r--r--src/game.h12
-rw-r--r--src/simulator/main.c17
-rw-r--r--src/simulator/rendering.c20
-rw-r--r--src/trainer/logging.c2
5 files changed, 77 insertions, 78 deletions
diff --git a/src/game.c b/src/game.c
index 9714cde..2059d32 100644
--- a/src/game.c
+++ b/src/game.c
@@ -76,25 +76,11 @@ Dir random_dir(void) {
 
 int is_cell_empty(const Game *game, Coord pos) {
 
-  //if (game->agents_map[pos.y][pos.x]) {
-  //  return 0;
-  //}
-
-  for (size_t i = 0; i < AGENTS_COUNT; ++i) {
-    if (coord_equals(game->agents[i].pos, pos)) {
-      return 0;
-    }
+  if (game->env_map[pos.y][pos.x]>=ENV_MAP_AGENTS) {
+    return game->agents[game->env_map[pos.y][pos.x]-ENV_MAP_AGENTS].health<=0;
   }
 
-  if (game->foods_map[pos.y][pos.x]) {
-    return 0;
-  }
-
-  if (game->walls_map[pos.y][pos.x]) {
-    return 0;
-  }
-
-  return 1;
+  return game->env_map[pos.y][pos.x]==ENV_MAP_NONE;
 }
 
 Coord random_coord_on_board(void) {
@@ -121,7 +107,9 @@ void init_game(Game *game) {
   memset(game, 0, sizeof(*game));
 
   for (size_t i = 0; i < AGENTS_COUNT; ++i) {
-    game->agents[i].pos = random_empty_coord_on_board(game);
+    Coord pos = random_empty_coord_on_board(game);
+    game->env_map[pos.y][pos.x] = ENV_MAP_AGENTS + i;
+    game->agents[i].pos = pos;
     game->agents[i].dir = random_dir();
     game->agents[i].hunger = HUNGER_MAX;
     game->agents[i].health = HEALTH_MAX;
@@ -139,12 +127,12 @@ void init_game(Game *game) {
 
   for (size_t i = 0; i < FOODS_COUNT; ++i) {
     Coord pos = random_empty_coord_on_board(game);
-    game->foods_map[pos.y][pos.x] = 1;
+    game->env_map[pos.y][pos.x] = ENV_MAP_FOOD;
   }
 
   for (size_t i = 0; i < WALLS_COUNT; ++i) {
     Coord pos = random_empty_coord_on_board(game);
-    game->walls_map[pos.y][pos.x] = 1;
+    game->env_map[pos.y][pos.x] = ENV_MAP_WALL;
   }
 
 }
@@ -166,8 +154,8 @@ int *food_infront_of_agent(Game *game, size_t agent_index) {
 
   Coord infront = coord_infront_of_agent(&game->agents[agent_index]);
 
-  if (game->foods_map[infront.y][infront.x]) {
-    return &game->foods_map[infront.y][infront.x];
+  if (game->env_map[infront.y][infront.x] == ENV_MAP_FOOD) {
+    return &game->env_map[infront.y][infront.x];
   }
 
   return NULL;
@@ -176,23 +164,23 @@ int *food_infront_of_agent(Game *game, size_t agent_index) {
 Agent *agent_infront_of_agent(Game *game, size_t agent_index) {
 
   Coord infront = coord_infront_of_agent(&game->agents[agent_index]);
+  int env = game->env_map[infront.y][infront.x];
 
   //if (game->agents_map[infront.y][infront.x]) {
   //  size_t infront_index = game->agents_map[infront.y][infront.x] - 1;
-  //  // TODO: when agent dies, remove from agents_map
+  //  // when agent dies, remove from agents_map
   //  // Then this can be removed
   //  if (game->agents[infront_index].health > 0) {
   //    return &game->agents[infront_index];
   //  }
   //}
 
-  for (size_t i = 0; i < AGENTS_COUNT; ++i) {
-    if (i != agent_index &&
-        game->agents[i].health > 0 &&
-        coord_equals(infront, game->agents[i].pos))
-      {
-        return &game->agents[i];
-      }
+  if(env >= ENV_MAP_AGENTS) {
+    int infront_agent_index = env - ENV_MAP_AGENTS;
+    if(infront_agent_index != agent_index &&
+       game->agents[infront_agent_index].health>0) {
+      return &game->agents[infront_agent_index];
+    }
   }
 
   return NULL;
@@ -202,8 +190,8 @@ int *wall_infront_of_agent(Game *game, size_t agent_index) {
 
   Coord infront = coord_infront_of_agent(&game->agents[agent_index]);
 
-  if (game->walls_map[infront.y][infront.x]) {
-    return &game->walls_map[infront.y][infront.x];
+  if (game->env_map[infront.y][infront.x] == ENV_MAP_WALL) {
+    return &game->env_map[infront.y][infront.x];
   }
 
   return NULL;
@@ -226,38 +214,40 @@ Env env_infront_of_agent(Game *game, size_t agent_index) {
   return ENV_NOTHING;
 }
 
-void step_agent(Agent *agent) {
-  Coord d = coord_dirs[agent->dir];
-  agent->pos.x = mod_int(agent->pos.x + d.x, BOARD_WIDTH);
-  agent->pos.y = mod_int(agent->pos.y + d.y, BOARD_HEIGHT);
+void step_agent(Game *game, size_t agent_index) {
+  Agent *agent = &game->agents[agent_index];
+  Coord new_pos = coord_infront_of_agent(agent);
+  game->env_map[agent->pos.y][agent->pos.x] = ENV_MAP_NONE; // set prev tile to empty
+  agent->pos.x = new_pos.x;
+  agent->pos.y = new_pos.y;
+  game->env_map[agent->pos.y][agent->pos.x] = ENV_MAP_AGENTS + agent_index; // register infront tile to agent
 }
 
-// TODO: void step_agent(Game *game, size_t agent_index) {}
-// Update Agent Map
-
 void execute_action(Game *game, size_t agent_index, Action action) {
   switch (action) {
   case ACTION_NOP: {
   } break;
   case ACTION_STEP: {
-    int *food = food_infront_of_agent(game, agent_index);
-    Agent *other_agent = agent_infront_of_agent(game, agent_index);
+    Coord infront = coord_infront_of_agent(&game->agents[agent_index]);
+    int *env = &game->env_map[infront.y][infront.x];
+    Agent *other_agent = agent_at(game, infront);
 
-    if (food != NULL) {
-      *food = 0;
+    if (*env == ENV_MAP_FOOD) {
+      *env = ENV_MAP_NONE; // set food tile to empty
       game->agents[agent_index].hunger += FOOD_HUNGER_RECOVERY;
       if (game->agents[agent_index].hunger > HUNGER_MAX) {
         game->agents[agent_index].hunger = HUNGER_MAX;
       }
-      step_agent(&game->agents[agent_index]);
     } else if (other_agent != NULL) {
       other_agent->health -= ATTACK_DAMAGE;
       if (other_agent->health <= 0){
+        *env = ENV_MAP_NONE; // set other agent tile to empty;
         game->agents[agent_index].hunger += FOOD_HUNGER_RECOVERY;
-        step_agent(&game->agents[agent_index]);
       }
-    } else if (wall_infront_of_agent(game, agent_index) != NULL) {
-      step_agent(&game->agents[agent_index]);
+
+      if (*env == ENV_MAP_NONE) {
+        step_agent(game, agent_index);
+      }
     }
   } break;
   case ACTION_TURN_LEFT: {
@@ -268,8 +258,8 @@ void execute_action(Game *game, size_t agent_index, Action action) {
     game->agents[agent_index].dir =
         mod_int(game->agents[agent_index].dir - 1, 4);
   } break;
-  case ACTION_COUNT: {
-    assert(0 && "UNREACHABLE ACTION_COUNT");
+  default: {
+    assert(0 && "UNREACHABLE DEFAULT ACTION");
   } break;
   }
 }
@@ -300,12 +290,10 @@ void step_game(Game *game) {
 }
 
 Agent *agent_at(Game *game, Coord pos) {
-  for (size_t i = 0; i < AGENTS_COUNT; ++i) {
-    if (coord_equals(game->agents[i].pos, pos)) {
-      return &game->agents[i];
-    }
+  int env = game->env_map[pos.y][pos.x];
+  if(env >= ENV_MAP_AGENTS) {
+    return &game->agents[env];
   }
-
   return NULL;
 }
 
@@ -354,12 +342,12 @@ void make_next_generation(Game *prev_game, Game *next_game){
 
   for (size_t i = 0; i < FOODS_COUNT; ++i) {
     Coord pos = random_empty_coord_on_board(next_game);
-    next_game->foods_map[pos.x][pos.y] = 1;
+    next_game->env_map[pos.x][pos.y] = ENV_MAP_FOOD;
   }
 
   for (size_t i = 0; i < WALLS_COUNT; ++i) {
     Coord pos = random_empty_coord_on_board(next_game);
-    next_game->walls_map[pos.x][pos.y] = 1;
+    next_game->env_map[pos.x][pos.y] = ENV_MAP_WALL;
   }
 
   for (size_t i = 0; i < AGENTS_COUNT; ++i) {
@@ -372,7 +360,9 @@ void make_next_generation(Game *prev_game, Game *next_game){
     mutate_agent(&next_game->agents[i]);
 
     // Work out agent positioning on new game
-    next_game->agents[i].pos = random_empty_coord_on_board(next_game);
+    Coord pos = random_empty_coord_on_board(next_game);
+    next_game->env_map[pos.y][pos.x] = ENV_MAP_AGENTS + i; // register tile for agent[i]
+    next_game->agents[i].pos = pos;
     next_game->agents[i].dir = random_dir();
     next_game->agents[i].hunger = HUNGER_MAX;
     next_game->agents[i].health = HEALTH_MAX;
diff --git a/src/game.h b/src/game.h
index c276f2c..5d5163a 100644
--- a/src/game.h
+++ b/src/game.h
@@ -89,11 +89,17 @@ typedef struct {
   Chromo chromo;
 } Agent;
 
+// TODO: use Env type
+// TODO: Change env_map to struct
+// maybe other env types need to store additional data like agents
+#define ENV_MAP_NONE 0
+#define ENV_MAP_WALL 1
+#define ENV_MAP_FOOD 2
+#define ENV_MAP_AGENTS 3
+
 typedef struct {
   Agent agents[AGENTS_COUNT];
-  //int agents_map[BOARD_HEIGHT][BOARD_WIDTH];
-  int foods_map[BOARD_HEIGHT][BOARD_WIDTH];
-  int walls_map[BOARD_HEIGHT][BOARD_WIDTH];
+  int env_map[BOARD_HEIGHT][BOARD_WIDTH];
 } Game;
 
 void init_game(Game *game);
diff --git a/src/simulator/main.c b/src/simulator/main.c
index 5c875fe..45b2420 100644
--- a/src/simulator/main.c
+++ b/src/simulator/main.c
@@ -3,7 +3,6 @@
 #include <assert.h>
 #include <SDL2/SDL.h>
 
-#include "SDL_events.h"
 #include "./rendering.h"
 #include "../game.h"
 
@@ -37,10 +36,6 @@ int main(int argc, char *argv[]) {
     load_game(input_filepath, &games[current]);
   }
 
-
-
-
-
   //print_chromo(stdout, game.chromos);
 
   scc(SDL_Init(SDL_INIT_VIDEO));
@@ -118,6 +113,18 @@ int main(int argc, char *argv[]) {
     // Render Agents
     render_game(renderer, &games[current]);
 
+    //TODO: Zoom
+
+    //SDL_Rect rect_a = {
+    //  0, 0,
+    //  SCREEN_WIDTH, SCREEN_HEIGHT
+    //};
+
+
+    //char a[10];
+    //a[10] = 0;
+
+    //SDL_RenderCopy(renderer, NULL, &rect_a, &rect_b);
     // Render
     SDL_RenderPresent(renderer);
 
diff --git a/src/simulator/rendering.c b/src/simulator/rendering.c
index 05a93f4..f94700b 100644
--- a/src/simulator/rendering.c
+++ b/src/simulator/rendering.c
@@ -1,5 +1,4 @@
 #include "rendering.h"
-#include "SDL_stdinc.h"
 #include "../game.h"
 
 #include <SDL2/SDL.h>
@@ -161,34 +160,31 @@ void render_agent(SDL_Renderer *renderer, Agent agent) {
 }
 
 void render_game(SDL_Renderer *renderer, const Game *game) {
-  for (size_t i = 0; i < AGENTS_COUNT; ++i) {
-    render_agent(renderer, game->agents[i]);
-  }
 
   // Render Foods
   const int indices[] = {0, 1, 2, 0, 3, 2};
   for(int y = 0; y < BOARD_HEIGHT; ++y) {
     for (int x = 0; x < BOARD_WIDTH; ++x) {
-      if (game->foods_map[y][x]) {
+      int env = game->env_map[y][x];
+      if (env == ENV_MAP_FOOD) {
         SDL_Vertex *vertices = sdl_draw_food_diamond(y, x);
         scc(SDL_RenderGeometry(renderer, NULL, vertices, 4, indices, 6));
         free(vertices); // sdl_draw_food_diamond's malloc
       }
 
-      if (game->walls_map[y][x]) {
+      if (game->env_map[y][x] == ENV_MAP_WALL) {
         SDL_Rect rect = {// Check why int floorf
                          (int)floorf(x * CELL_WIDTH),
                          (int)floorf(y * CELL_HEIGHT),
                          (int)floorf(CELL_WIDTH), (int)floorf(CELL_HEIGHT)};
         SDL_SetRenderDrawColor(renderer, HEX_COLOR(WALL_COLOR));
         SDL_RenderFillRect(renderer, &rect);
+      } else if(env >= ENV_MAP_AGENTS) {
+        int agent_index = env - ENV_MAP_AGENTS;
+        if (game->agents[agent_index].health > 0) {
+          render_agent(renderer, game->agents[agent_index]);
+        }
       }
     }
   }
-
-  // Render Walls
-  for (size_t i = 0; i < WALLS_COUNT; ++i) {
-
-  }
-
 }
diff --git a/src/trainer/logging.c b/src/trainer/logging.c
index f35ab73..0296f10 100644
--- a/src/trainer/logging.c
+++ b/src/trainer/logging.c
@@ -52,7 +52,7 @@ void log_generation(FILE *stream, int gen, Game *game) {
   int food_eaten = FOODS_COUNT;
   for (size_t y = 0; y < BOARD_HEIGHT; ++y) {
     for (size_t x = 0; x < BOARD_WIDTH; ++x) {
-      if (game->foods_map[y][x]) {
+      if (game->env_map[y][x] == ENV_MAP_FOOD) {
         food_eaten--;
       }
     }