From 3ea3a282c7e94eb494c0c5773d2f481097c07164 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Wed, 21 May 2025 18:22:01 +0300
Subject: [PATCH 63/63] tiledef: Add functions dealing with the tiledef name

See RM #1422

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/tiledef.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++
 common/tiledef.h | 15 +++++++++++++
 2 files changed, 71 insertions(+)

diff --git a/common/tiledef.c b/common/tiledef.c
index 1b75cbfd0d..f6bdf4a033 100644
--- a/common/tiledef.c
+++ b/common/tiledef.c
@@ -86,3 +86,59 @@ struct tiledef *tiledef_by_number(int id)
 
   return &tiledefs[id];
 }
+
+/************************************************************************//**
+  Return the (translated) name of the tiledef.
+  You don't have to free the return pointer.
+****************************************************************************/
+const char *tiledef_name_translation(const struct tiledef *td)
+{
+  return name_translation_get(&td->name);
+}
+
+/************************************************************************//**
+  Return the (untranslated) rule name of the tiledef.
+  You don't have to free the return pointer.
+****************************************************************************/
+const char *tiledef_rule_name(const struct tiledef *td)
+{
+  return rule_name_get(&td->name);
+}
+
+/************************************************************************//**
+  Returns tiledef matching rule name or nullptr if there is no tiledef
+  with such name.
+****************************************************************************/
+struct tiledef *tiledef_by_rule_name(const char *name)
+{
+  const char *qs;
+
+  if (name == nullptr) {
+    return nullptr;
+  }
+
+  qs = Qn_(name);
+
+  tiledef_iterate(td) {
+    if (!fc_strcasecmp(tiledef_rule_name(td), qs)) {
+      return td;
+    }
+  } tiledef_iterate_end;
+
+  return nullptr;
+}
+
+/************************************************************************//**
+  Returns tiledef matching the translated name, or nullptr if there is no
+  tiledef with that name.
+****************************************************************************/
+struct tiledef *tiledef_by_translated_name(const char *name)
+{
+  tiledef_iterate(td) {
+    if (0 == strcmp(tiledef_name_translation(td), name)) {
+      return td;
+    }
+  } tiledef_iterate_end;
+
+  return nullptr;
+}
diff --git a/common/tiledef.h b/common/tiledef.h
index c6f7312a23..418f7e5a0c 100644
--- a/common/tiledef.h
+++ b/common/tiledef.h
@@ -40,6 +40,21 @@ struct tiledef *tiledef_by_number(int id);
  * correct implementation too. */
 #define tiledef_index(_td_) (_td_)->id
 
+const char *tiledef_name_translation(const struct tiledef *td);
+const char *tiledef_rule_name(const struct tiledef *td);
+struct tiledef *tiledef_by_rule_name(const char *name);
+struct tiledef *tiledef_by_translated_name(const char *name);
+
+#define tiledef_iterate(_p)                                                 \
+{                                                                           \
+  int _i_##_p;                                                              \
+  for (_i_##_p = 0; _i_##_p < game.control.num_tiledef_types; _i_##_p++) {  \
+    struct tiledef *_p = tiledef_by_number(_i_##_p);
+
+#define tiledef_iterate_end                       \
+  }                                               \
+}
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
-- 
2.47.2

