home · contact · privacy
Merge branch 'master' into 7drl2016
[plomrogue] / libplomrogue.c
index c340e38502d66df9f1fe2d35c3e3bc7d3a9866f9..33da37af6e2085940b2586be22951f9267a15cd4 100644 (file)
@@ -340,7 +340,8 @@ static uint8_t shade_hex(uint32_t left_angle, uint32_t right_angle,
  */
 static uint8_t eval_position(uint16_t dist, uint16_t hex_i, char * fov_map,
                              struct yx_uint8 * test_pos,
-                             struct shadow_angle ** shadows)
+                             struct shadow_angle ** shadows,
+                             const char * symbols_obstacle)
 {
     int32_t left_angle_uncorrected =   ((CIRCLE / 12) / dist)
                                      - (hex_i * (CIRCLE / 6) / dist);
@@ -357,7 +358,7 @@ static uint8_t eval_position(uint16_t dist, uint16_t hex_i, char * fov_map,
     uint16_t pos_in_map = test_pos->y * maplength + test_pos->x;
     uint8_t all_shaded = shade_hex(left_angle, right_angle_1st, middle_angle,
                                    shadows, pos_in_map, fov_map);
-    if (!all_shaded && 'X' == worldmap[pos_in_map])
+    if (!all_shaded && NULL != strchr(symbols_obstacle, worldmap[pos_in_map]))
     {
         if (set_shadow(left_angle, right_angle_1st, shadows))
         {
@@ -378,8 +379,9 @@ static uint8_t eval_position(uint16_t dist, uint16_t hex_i, char * fov_map,
 /* Update field of view in "fovmap" of "worldmap_input" as seen from "y"/"x".
  * Return 1 on malloc error, else 0.
  */
-extern uint8_t build_fov_map(uint8_t y, uint8_t x,
-                             char * fovmap, char * worldmap_input)
+extern uint8_t build_fov_map(uint8_t y, uint8_t x, char * fovmap,
+                             char * worldmap_input,
+                             const char * symbols_obstacle)
 {
     worldmap = worldmap_input;
     struct shadow_angle * shadows = NULL;
@@ -408,7 +410,8 @@ extern uint8_t build_fov_map(uint8_t y, uint8_t x,
             }
             if (mv_yx_in_dir_legal(dir_char, &test_pos))
             {
-                if (eval_position(circle_i, hex_i, fovmap, &test_pos, &shadows))
+                if (eval_position(circle_i, hex_i, fovmap, &test_pos, &shadows,
+                                  symbols_obstacle))
                 {
                     return 1;
                 }
@@ -563,6 +566,110 @@ extern uint8_t dijkstra_map()
     return 0;
 }
 
+
+/* 7DRL/TCE addition: Init AI score map to all-eatable unknown fields. */
+extern uint8_t TCE_init_score_map()
+{
+    uint32_t map_size = maplength * maplength;
+    score_map = malloc(map_size * sizeof(uint16_t));
+    if (!score_map)
+    {
+        return 1;
+    }
+    uint32_t i = 0;
+    for (; i < map_size; i++)
+    {
+        score_map[i] = UINT16_MAX - 1;
+    }
+    return 0;
+}
+
+/* 7DRL/TCE addition: movement cost map setting. */
+static uint8_t * TCE_move_cost_map = NULL;
+extern uint8_t TCE_set_movement_cost_map(char * mem_map)
+{
+    uint32_t map_size = maplength * maplength;
+    free(TCE_move_cost_map);
+    TCE_move_cost_map = malloc(map_size * sizeof(uint8_t));
+    uint32_t pos = 0;
+    for (; pos < map_size; pos++)
+    {
+        TCE_move_cost_map[pos] = 0;
+    }
+    if (!TCE_move_cost_map)
+    {
+        return 1;
+    }
+    for (pos = 0; pos < map_size; pos++)
+    {
+        switch(mem_map[pos]) {
+            case '-':
+            case '+':
+            case '$':
+            case '0':
+                TCE_move_cost_map[pos] = 1;
+                break;
+            case '1':
+                TCE_move_cost_map[pos] = 2;
+                break;
+            case '2':
+                TCE_move_cost_map[pos] = 4;
+                break;
+            case '3':
+                TCE_move_cost_map[pos] = 3;
+                break;
+            case '4':
+                TCE_move_cost_map[pos] = 6;
+                break;
+        }
+    }
+    return 0;
+}
+
+
+/* 7DRL/TCE addition: Like dijkstra_map(), but with movement costs applied. */
+extern uint8_t TCE_dijkstra_map_with_movement_cost()
+{
+    if (!score_map || !TCE_move_cost_map)
+    {
+        return 1;
+    }
+    uint16_t max_score = UINT16_MAX - 1;
+    uint32_t map_size = maplength * maplength;
+    uint32_t pos;
+    uint16_t i_scans, neighbors[6], min_neighbor;
+    uint8_t scores_still_changing = 1;
+    uint8_t i_dirs;
+    for (i_scans = 0; scores_still_changing; i_scans++)
+    {
+        scores_still_changing = 0;
+        for (pos = 0; pos < map_size; pos++)
+        {
+            uint16_t score = score_map[pos];
+            uint8_t mov_cost = TCE_move_cost_map[pos];
+            if (score <= max_score && mov_cost > 0 && score > i_scans)
+            {
+                get_neighbor_scores(pos, max_score, neighbors);
+                min_neighbor = max_score;
+                for (i_dirs = 0; i_dirs < 6; i_dirs++)
+                {
+                    if (min_neighbor > neighbors[i_dirs])
+                    {
+                        min_neighbor = neighbors[i_dirs];
+                    }
+                }
+                if (score_map[pos] > min_neighbor + mov_cost)
+                {
+                    score_map[pos] = min_neighbor + mov_cost;
+                    scores_still_changing = 1;
+                }
+            }
+        }
+    }
+    return 0;
+}
+
+
 extern uint8_t zero_score_map_where_char_on_memdepthmap(char c,
                                                         char * memdepthmap)
 {
@@ -637,6 +744,7 @@ extern void update_mem_and_memdepthmap_via_fovmap(char * map, char * fovmap,
 }
 
 /* USEFUL FOR DEBUGGING
+#include <stdio.h>
 extern void write_score_map()
 {
     FILE *f = fopen("score_map", "a");
@@ -647,7 +755,16 @@ extern void write_score_map()
     {
         for (x = 0; x < maplength; x++)
         {
-            fprintf(f, "%2X", score_map[y * maplength + x] % 256);
+            uint32_t pos = y * maplength + x;
+            uint16_t val = score_map[pos];
+            if (val == UINT16_MAX)
+            {
+                fprintf(f, " Z");
+            } else if (val == UINT16_MAX - 1) {
+                fprintf(f, " Y");
+            } else {
+                fprintf(f, "%2X", score_map[pos] % 256);
+            }
         }
         fprintf(f, "\n");
     }