added type definition for hash table structure
[rocksndiamonds.git] / src / game_bd / bd_cave.c
index b0f1573ef52d96992e70bb6aa6cccdc1680ec9cc..30b9e766c18cece36b23ce5bb1b300b812ee3cc4 100644 (file)
@@ -81,7 +81,7 @@ static const char* scheduling_filename[] =
   "bd2ckatari"
 };
 
-static GHashTable *name_to_element;
+static HashTable *name_to_element;
 GdElement gd_char_to_element[256];
 
 /* color of flashing the screen, gate opening to exit */
@@ -247,7 +247,7 @@ void gd_create_char_to_element_table(void)
 
     if (c)
     {
-      if (gd_char_to_element[c]!=O_UNKNOWN)
+      if (gd_char_to_element[c] != O_UNKNOWN)
        Warn("Character %c already used for element %x", c, gd_char_to_element[c]);
 
       gd_char_to_element[c] = i;
@@ -275,9 +275,8 @@ void gd_cave_init(void)
 
   /* put names to a hash table */
   /* this is a helper for file read operations */
-  /* maps g_strdupped strings to elemenets (integers) */
-  name_to_element = g_hash_table_new_full(gd_str_case_hash, gd_str_case_equal,
-                                         free, NULL);
+  /* maps copied strings to elements (integers) */
+  name_to_element = create_hashtable(str_case_hash, str_case_equal, NULL, NULL);
 
   for (i = 0; i < O_MAX; i++)
   {
@@ -285,36 +284,36 @@ void gd_cave_init(void)
 
     key = g_ascii_strup(gd_elements[i].filename, -1);
 
-    if (g_hash_table_lookup_extended(name_to_element, key, NULL, NULL))
+    if (hashtable_exists(name_to_element, key))                /* hash value may be 0 */
       Warn("Name %s already used for element %x", key, i);
 
-    g_hash_table_insert(name_to_element, key, GINT_TO_POINTER(i));
+    hashtable_insert(name_to_element, key, INT_TO_PTR(i));
     /* ^^^ do not free "key", as hash table needs it during the whole time! */
 
-    key = g_strdup_printf("SCANNED_%s", key);        /* new string */
+    key = getStringCat2("SCANNED_", key);              /* new string */
 
-    g_hash_table_insert(name_to_element, key, GINT_TO_POINTER(i));
+    hashtable_insert(name_to_element, key, INT_TO_PTR(i));
     /* once again, do not free "key" ^^^ */
   }
 
   /* for compatibility with tim stridmann's memorydump->bdcff converter... .... ... */
-  g_hash_table_insert(name_to_element, "HEXPANDING_WALL", GINT_TO_POINTER(O_H_EXPANDING_WALL));
-  g_hash_table_insert(name_to_element, "FALLING_DIAMOND", GINT_TO_POINTER(O_DIAMOND_F));
-  g_hash_table_insert(name_to_element, "FALLING_BOULDER", GINT_TO_POINTER(O_STONE_F));
-  g_hash_table_insert(name_to_element, "EXPLOSION1S", GINT_TO_POINTER(O_EXPLODE_1));
-  g_hash_table_insert(name_to_element, "EXPLOSION2S", GINT_TO_POINTER(O_EXPLODE_2));
-  g_hash_table_insert(name_to_element, "EXPLOSION3S", GINT_TO_POINTER(O_EXPLODE_3));
-  g_hash_table_insert(name_to_element, "EXPLOSION4S", GINT_TO_POINTER(O_EXPLODE_4));
-  g_hash_table_insert(name_to_element, "EXPLOSION5S", GINT_TO_POINTER(O_EXPLODE_5));
-  g_hash_table_insert(name_to_element, "EXPLOSION1D", GINT_TO_POINTER(O_PRE_DIA_1));
-  g_hash_table_insert(name_to_element, "EXPLOSION2D", GINT_TO_POINTER(O_PRE_DIA_2));
-  g_hash_table_insert(name_to_element, "EXPLOSION3D", GINT_TO_POINTER(O_PRE_DIA_3));
-  g_hash_table_insert(name_to_element, "EXPLOSION4D", GINT_TO_POINTER(O_PRE_DIA_4));
-  g_hash_table_insert(name_to_element, "EXPLOSION5D", GINT_TO_POINTER(O_PRE_DIA_5));
-  g_hash_table_insert(name_to_element, "WALL2", GINT_TO_POINTER(O_STEEL_EXPLODABLE));
+  hashtable_insert(name_to_element, "HEXPANDING_WALL", INT_TO_PTR(O_H_EXPANDING_WALL));
+  hashtable_insert(name_to_element, "FALLING_DIAMOND", INT_TO_PTR(O_DIAMOND_F));
+  hashtable_insert(name_to_element, "FALLING_BOULDER", INT_TO_PTR(O_STONE_F));
+  hashtable_insert(name_to_element, "EXPLOSION1S", INT_TO_PTR(O_EXPLODE_1));
+  hashtable_insert(name_to_element, "EXPLOSION2S", INT_TO_PTR(O_EXPLODE_2));
+  hashtable_insert(name_to_element, "EXPLOSION3S", INT_TO_PTR(O_EXPLODE_3));
+  hashtable_insert(name_to_element, "EXPLOSION4S", INT_TO_PTR(O_EXPLODE_4));
+  hashtable_insert(name_to_element, "EXPLOSION5S", INT_TO_PTR(O_EXPLODE_5));
+  hashtable_insert(name_to_element, "EXPLOSION1D", INT_TO_PTR(O_PRE_DIA_1));
+  hashtable_insert(name_to_element, "EXPLOSION2D", INT_TO_PTR(O_PRE_DIA_2));
+  hashtable_insert(name_to_element, "EXPLOSION3D", INT_TO_PTR(O_PRE_DIA_3));
+  hashtable_insert(name_to_element, "EXPLOSION4D", INT_TO_PTR(O_PRE_DIA_4));
+  hashtable_insert(name_to_element, "EXPLOSION5D", INT_TO_PTR(O_PRE_DIA_5));
+  hashtable_insert(name_to_element, "WALL2", INT_TO_PTR(O_STEEL_EXPLODABLE));
 
   /* compatibility with old bd-faq (pre disassembly of bladder) */
-  g_hash_table_insert(name_to_element, "BLADDERd9", GINT_TO_POINTER(O_BLADDER_8));
+  hashtable_insert(name_to_element, "BLADDERd9", INT_TO_PTR(O_BLADDER_8));
 
   /* create table to show errors at the start of the application */
   gd_create_char_to_element_table();
@@ -324,7 +323,7 @@ void gd_cave_init(void)
 GdElement gd_get_element_from_string (const char *string)
 {
   char *upper = g_ascii_strup(string, -1);
-  gpointer value;
+  void *value;
   boolean found;
 
   if (!string)
@@ -333,12 +332,15 @@ GdElement gd_get_element_from_string (const char *string)
     return O_UNKNOWN;
   }
 
-  found = g_hash_table_lookup_extended(name_to_element, upper, NULL, &value);
+  found = hashtable_exists(name_to_element, upper);    /* hash value may be 0 */
+  if (found)
+    value = hashtable_search(name_to_element, upper);
   free(upper);
   if (found)
-    return (GdElement) (GPOINTER_TO_INT(value));
+    return (GdElement) (PTR_TO_INT(value));
+
+  Warn("Invalid string representing element: '%s'", string);
 
-  Warn("Invalid string representing element: %s", string);
   return O_UNKNOWN;
 }
 
@@ -420,6 +422,22 @@ int gd_add_highscore(GdHighScore *highscores, const char *name, int score)
   return -1;
 }
 
+/* for the case-insensitive hash keys */
+int str_case_equal(void *s1, void *s2)
+{
+  return strcasecmp(s1, s2) == 0;
+}
+
+unsigned int str_case_hash(void *v)
+{
+  char *upper = getStringToUpper(v);
+  unsigned int hash = get_hash_from_string(upper);
+
+  free(upper);
+
+  return hash;
+}
+
 /* for the case-insensitive hash keys */
 boolean gd_str_case_equal(gconstpointer s1, gconstpointer s2)
 {
@@ -568,7 +586,7 @@ void gd_cave_free(GdCave *cave)
 
 static void hash_copy_foreach(const char *key, const char *value, GHashTable *dest)
 {
-  g_hash_table_insert(dest, g_strdup(key), g_strdup(value));
+  g_hash_table_insert(dest, getStringCopy(key), getStringCopy(value));
 }
 
 /* copy cave from src to destination, with duplicating dynamically allocated data */
@@ -1293,7 +1311,7 @@ void gd_drawcave_game(const GdCave *cave, int **element_buffer, int **gfx_buffer
 
   /* player with bomb does not blink or tap - no graphics drawn for that.
      running is drawn using w/o bomb cells */
-  if (cave->last_direction!=GD_MV_STILL)
+  if (cave->last_direction != GD_MV_STILL)
   {
     elemmapping[O_PLAYER_BOMB] = map;
     elemdrawing[O_PLAYER_BOMB] = draw;
@@ -1429,26 +1447,6 @@ void gd_replay_store_movement(GdReplay *replay, GdDirection player_move,
   g_byte_array_append(replay->movements, data, 1);
 }
 
-/* get next available movement from a replay; store variables to player_move,
-   player_fire, suicide */
-/* return true if successful */
-boolean gd_replay_get_next_movement(GdReplay *replay, GdDirection *player_move,
-                                   boolean *player_fire, boolean *suicide)
-{
-  guint8 data;
-
-  /* if no more available movements */
-  if (replay->current_playing_pos >= replay->movements->len)
-    return FALSE;
-
-  data = replay->movements->data[replay->current_playing_pos++];
-  *suicide = (data & GD_REPLAY_SUICIDE_MASK) != 0;
-  *player_fire = (data & GD_REPLAY_FIRE_MASK) != 0;
-  *player_move = (data & GD_REPLAY_MOVE_MASK);
-
-  return TRUE;
-}
-
 /* calculate adler checksum for a rendered cave; this can be used for more caves. */
 void gd_cave_adler_checksum_more(GdCave *cave, guint32 *a, guint32 *b)
 {