+#define MAX_EVOL_DEPTH 64
+
+
+/*!
+ * @brief int配列でstrncmp()と似た比較処理を行う /
+ * Compare two int-type array like strncmp() and return TRUE if equals
+ * @param a 比較するint配列1
+ * @param b 比較するint配列2
+ * @param length
+ * @return 両者の値が等しければTRUEを返す
+ */
+static bool int_n_cmp(int *a, int *b, int length)
+{
+ /* Null-string comparation is always TRUE */
+ if (!length) return TRUE;
+
+ do
+ {
+ if (*a != *(b++)) return FALSE;
+ if (!(*(a++))) break;
+ }
+ while (--length);
+
+ return TRUE;
+}
+
+
+/*!
+ * @brief ある木が指定された木の部分木かどうかを返す /
+ * Returns TRUE if an evolution tree is "partial tree"
+ * @param tree 元となる木構造リスト
+ * @param partial_tree 部分木かどうか判定したい木構造リスト
+ * @return 部分木ならばTRUEを返す
+ */
+static bool is_partial_tree(int *tree, int *partial_tree)
+{
+ int pt_head = *(partial_tree++);
+ int pt_len = 0;
+
+ while (partial_tree[pt_len]) pt_len++;
+
+ while (*tree)
+ {
+ if (*(tree++) == pt_head)
+ {
+ if (int_n_cmp(tree, partial_tree, pt_len)) return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+
+/*!
+ * @brief 進化ツリーをソートするためモンスター種族の判定関数 /
+ * Sorting hook -- Comp function
+ * @param u 進化木構造データ
+ * @param v 未使用
+ * @param a 比較したいモンスター種族ID1
+ * @param b 比較したいモンスター種族ID2
+ * @return 2が大きければTRUEを返す
+ */
+static bool ang_sort_comp_evol_tree(vptr u, vptr v, int a, int b)
+{
+ int **evol_tree = (int **)u;
+
+ int w1 = evol_tree[a][0];
+ int w2 = evol_tree[b][0];
+ monster_race *r1_ptr = &r_info[w1];
+ monster_race *r2_ptr = &r_info[w2];
+
+ /* Unused */
+ (void)v;
+
+ /* Used tree first */
+ if (w1 && !w2) return TRUE;
+ if (!w1 && w2) return FALSE;
+
+ /* Sort by monster level */
+ if (r1_ptr->level < r2_ptr->level) return TRUE;
+ if (r1_ptr->level > r2_ptr->level) return FALSE;
+
+ /* Sort by monster experience */
+ if (r1_ptr->mexp < r2_ptr->mexp) return TRUE;
+ if (r1_ptr->mexp > r2_ptr->mexp) return FALSE;
+
+ /* Compare indexes */
+ return w1 <= w2;
+}
+
+/*!
+ * @brief 進化ツリーをソートするため木構造のスワップ関数 /
+ * Sorting hook -- Swap function
+ * @param u 進化木構造データ
+ * @param v 未使用
+ * @param a スワップしたい木構造1
+ * @param b スワップしたい木構造2
+ * @return 2が大きければTRUEを返す
+ */
+static void ang_sort_swap_evol_tree(vptr u, vptr v, int a, int b)
+{
+ int **evol_tree = (int **)u;
+ int *holder;
+
+ /* Unused */
+ (void)v;
+
+ /* Swap */
+ holder = evol_tree[a];
+ evol_tree[a] = evol_tree[b];
+ evol_tree[b] = holder;
+}
+
+/*!
+ * @brief 進化ツリーをスポイラー出力するメインルーチン /
+ * Print monsters' evolution information to file
+ * @param fname 出力ファイル名
+ * @return なし
+ */
+static void spoil_mon_evol(cptr fname)
+{
+ char buf[1024];
+ monster_race *r_ptr;
+ int **evol_tree, i, j, n, r_idx;
+ int *evol_tree_zero; /* For C_KILL() */
+
+ /* Build the filename */
+ path_build(buf, sizeof buf, ANGBAND_DIR_USER, fname);
+
+ /* File type is "TEXT" */
+ FILE_TYPE(FILE_TYPE_TEXT);
+
+ /* Open the file */
+ fff = my_fopen(buf, "w");
+
+ if (!fff)
+ {
+ msg_print("Cannot create spoiler file.");
+ return;
+ }
+
+ /* Dump the header */
+ sprintf(buf, "Monster Spoilers for Hengband Version %d.%d.%d\n",
+ FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
+
+ spoil_out(buf);
+ spoil_out("------------------------------------------\n\n");
+
+ /* Allocate the "evol_tree" array (2-dimension) */
+ C_MAKE(evol_tree, max_r_idx, int *);
+ C_MAKE(*evol_tree, max_r_idx * (MAX_EVOL_DEPTH + 1), int);
+ for (i = 1; i < max_r_idx; i++) evol_tree[i] = *evol_tree + i * (MAX_EVOL_DEPTH + 1);
+ evol_tree_zero = *evol_tree;
+
+ /* Step 1: Build the evolution tree */
+ for (i = 1; i < max_r_idx; i++)
+ {
+ r_ptr = &r_info[i];
+
+ /* No evolution */
+ if (!r_ptr->next_exp) continue;
+
+ /* Trace evolution */
+ n = 0;
+ evol_tree[i][n++] = i;
+ do
+ {
+ evol_tree[i][n++] = r_ptr->next_r_idx;
+ r_ptr = &r_info[r_ptr->next_r_idx];
+ }
+ while (r_ptr->next_exp && (n < MAX_EVOL_DEPTH));
+ }
+
+ /* Step 2: Scan the evolution trees and remove "partial tree" */
+ for (i = 1; i < max_r_idx; i++)
+ {
+ /* Not evolution tree */
+ if (!evol_tree[i][0]) continue;
+
+ for (j = 1; j < max_r_idx; j++)
+ {
+ /* Same tree */
+ if (i == j) continue;
+
+ /* Not evolution tree */
+ if (!evol_tree[j][0]) continue;
+
+ /* Is evolution tree[i] is part of [j]? */
+ if (is_partial_tree(evol_tree[j], evol_tree[i]))
+ {
+ /* Remove this evolution tree */
+ evol_tree[i][0] = 0;
+ break;
+ }
+ }
+ }
+
+ /* Step 3: Sort the evolution trees */
+
+ /* Select the sort method */
+ ang_sort_comp = ang_sort_comp_evol_tree;
+ ang_sort_swap = ang_sort_swap_evol_tree;
+
+ /* Sort the array */
+ ang_sort(evol_tree, NULL, max_r_idx);
+
+ /* Step 4: Print the evolution trees */
+ for (i = 0; i < max_r_idx; i++)
+ {
+ r_idx = evol_tree[i][0];
+
+ /* No evolution or removed evolution tree */
+ if (!r_idx) continue;
+
+ /* Trace the evolution tree */
+ r_ptr = &r_info[r_idx];
+ fprintf(fff, _("[%d]: %s (レベル%d, '%c')\n", "[%d]: %s (Level %d, '%c')\n"),
+ r_idx, r_name + r_ptr->name, (int)r_ptr->level, r_ptr->d_char);
+
+ for (n = 1; r_ptr->next_exp; n++)
+ {
+ fprintf(fff, "%*s-(%ld)-> ", n * 2, "", (long int)r_ptr->next_exp);
+ fprintf(fff, "[%d]: ", r_ptr->next_r_idx);
+ r_ptr = &r_info[r_ptr->next_r_idx];
+ fprintf(fff, _("%s (レベル%d, '%c')\n", "%s (Level %d, '%c')\n"),
+ r_name + r_ptr->name, (int)r_ptr->level, r_ptr->d_char);
+ }
+
+ /* End of evolution tree */
+ fputc('\n', fff);
+ }
+
+ /* Free the "evol_tree" array (2-dimension) */
+ C_KILL(evol_tree_zero, max_r_idx * (MAX_EVOL_DEPTH + 1), int);
+ C_KILL(evol_tree, max_r_idx, int *);
+
+ /* Check for errors */
+ if (ferror(fff) || my_fclose(fff))
+ {
+ msg_print("Cannot close spoiler file.");
+ return;
+ }
+
+ msg_print("Successfully created a spoiler file.");
+}
+
+
+