OSDN Git Service

[Refactor] #37353 マジックナンバー修正(A_MAX) / Fix magic number (A_MAX).
[hengband/hengband.git] / src / util.c
index fb7cc6c..c93b9c1 100644 (file)
@@ -11,6 +11,7 @@
 /* Purpose: Angband utilities -BEN- */
 
 #include "angband.h"
+#include "monsterrace-hook.h"
 
 
 static int num_more = 0;
@@ -97,7 +98,7 @@ int usleep(huge usecs)
  */
 #ifdef SET_UID
 extern struct passwd *getpwuid(uid_t uid);
-extern struct passwd *getpwnam(const char *name);
+extern struct passwd *getpwnam(cptr name);
 #endif
 
 
@@ -283,7 +284,6 @@ static errr path_temp(char *buf, int max)
        /* Temp file */
        s = tmpnam(NULL);
 
-       /* Oops */
        if (!s) return (-1);
 
        /* Format to length */
@@ -438,9 +438,7 @@ FILE *my_fopen_temp(char *buf, int max)
 errr my_fgets(FILE *fff, char *buf, huge n)
 {
        huge i = 0;
-
        char *s;
-
        char tmp[1024];
 
        /* Read a line */
@@ -529,10 +527,8 @@ errr my_fgets(FILE *fff, char *buf, huge n)
 
 /*
  * Hack -- replacement for "fputs()"
- *
  * Dump a string, plus a newline, to a file
- *
- * XXX XXX XXX Process internal weirdness?
+ * Process internal weirdness?
  */
 errr my_fputs(FILE *fff, cptr buf, huge n)
 {
@@ -562,18 +558,6 @@ errr my_fputs(FILE *fff, cptr buf, huge n)
 
 
 /*
- * Code Warrior is a little weird about some functions
- */
-#ifdef BEN_HACK
-extern int open(const char *, int, ...);
-extern int close(int);
-extern int read(int, void *, unsigned int);
-extern int write(int, const void *, unsigned int);
-extern long lseek(int, long, int);
-#endif /* BEN_HACK */
-
-
-/*
  * The Macintosh is a little bit brain-dead sometimes
  */
 #ifdef MACINTOSH
@@ -605,7 +589,6 @@ errr fd_kill(cptr file)
        /* Remove */
        (void)remove(buf);
 
-       /* XXX XXX XXX */
        return (0);
 }
 
@@ -627,7 +610,6 @@ errr fd_move(cptr file, cptr what)
        /* Rename */
        (void)rename(buf, aux);
 
-       /* XXX XXX XXX */
        return (0);
 }
 
@@ -678,23 +660,13 @@ errr fd_copy(cptr file, cptr what)
        fd_close(src_fd);
        fd_close(dst_fd);
 
-       /* XXX XXX XXX */
        return (0);
 }
 
-
 /*
  * Hack -- attempt to open a file descriptor (create file)
- *
  * This function should fail if the file already exists
- *
  * Note that we assume that the file should be "binary"
- *
- * XXX XXX XXX The horrible "BEN_HACK" code is for compiling under
- * the CodeWarrior compiler, in which case, for some reason, none
- * of the "O_*" flags are defined, and we must fake the definition
- * of "O_RDONLY", "O_WRONLY", and "O_RDWR" in "A-win-h", and then
- * we must simulate the effect of the proper "open()" call below.
  */
 int fd_make(cptr file, BIT_FLAGS mode)
 {
@@ -703,19 +675,6 @@ int fd_make(cptr file, BIT_FLAGS mode)
        /* Hack -- Try to parse the path */
        if (path_parse(buf, 1024, file)) return (-1);
 
-#ifdef BEN_HACK
-
-       /* Check for existance */
-       /* if (fd_close(fd_open(file, O_RDONLY | O_BINARY))) return (1); */
-
-       /* Mega-Hack -- Create the file */
-       (void)my_fclose(my_fopen(file, "wb"));
-
-       /* Re-open the file for writing */
-       return (open(buf, O_WRONLY | O_BINARY, mode));
-
-#else /* BEN_HACK */
-
 #if defined(MAC_MPW) || defined(MACH_O_CARBON)
        {
                int fdes;
@@ -726,12 +685,10 @@ int fd_make(cptr file, BIT_FLAGS mode)
                /* Return the descriptor */
                return (fdes);
        }
-# else
+#else
        /* Create the file, fail if exists, write-only, binary */
        return (open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode));
-# endif
-
-#endif /* BEN_HACK */
+#endif
 
 }
 
@@ -936,7 +893,6 @@ errr fd_close(int fd)
        /* Close */
        (void)close(fd);
 
-       /* XXX XXX XXX */
        return (0);
 }
 
@@ -947,7 +903,7 @@ errr fd_close(int fd)
 
 
 /*
- * XXX XXX XXX Important note about "colors" XXX XXX XXX
+ * Important note about "colors" 
  *
  * The "TERM_*" color definitions list the "composition" of each
  * "Angband color" in terms of "quarters" of each of the three color
@@ -1588,8 +1544,6 @@ static sint macro_find_ready(cptr pat)
                n = i;
                s = t;
        }
-
-       /* Result */
        return (n);
 }
 
@@ -1598,15 +1552,15 @@ static sint macro_find_ready(cptr pat)
  * Add a macro definition (or redefinition).
  *
  * We should use "act == NULL" to "remove" a macro, but this might make it
- * impossible to save the "removal" of a macro definition.  XXX XXX XXX
+ * impossible to save the "removal" of a macro definition.  
  *
  * We should consider refusing to allow macros which contain existing macros,
  * or which are contained in existing macros, because this would simplify the
- * macro analysis code.  XXX XXX XXX
+ * macro analysis code.  
  *
  * We should consider removing the "command macro" crap, and replacing it
  * with some kind of "powerful keymap" ability, but this might make it hard
- * to change the "roguelike" option from inside the game.  XXX XXX XXX
+ * to change the "roguelike" option from inside the game.  
  */
 errr macro_add(cptr pat, cptr act)
 {
@@ -1760,7 +1714,7 @@ void select_floor_music(void)
                return;
        }
 
-       for(i = 0; i < max_quests; i++)
+       for(i = 0; i < max_q_idx; i++)
        { // TODO マクロで類似条件を統合すること
                if(quest[i].status == QUEST_STATUS_TAKEN &&
                        (quest[i].type == QUEST_TYPE_KILL_LEVEL || quest[i].type == QUEST_TYPE_RANDOM) &&
@@ -1826,7 +1780,7 @@ void select_floor_music(void)
  * Only 500 (0+1+2+...+29+30) milliseconds may elapse between each key in
  * the macro trigger sequence.  If a key sequence forms the "prefix" of a
  * macro trigger, 500 milliseconds must pass before the key sequence is
- * known not to be that macro trigger.  XXX XXX XXX
+ * known not to be that macro trigger.  
  */
 static char inkey_aux(void)
 {
@@ -1910,7 +1864,6 @@ static char inkey_aux(void)
                        /* Excessive delay */
                        if (w >= 10) break;
 
-                       /* Delay */
                        Term_xtra(TERM_XTRA_DELAY, w);
                }
        }
@@ -2005,7 +1958,7 @@ static void forget_macro_action(void)
 
 
 /*
- * Mega-Hack -- special "inkey_next" pointer.  XXX XXX XXX
+ * Mega-Hack -- special "inkey_next" pointer.  
  *
  * This special pointer allows a sequence of keys to be "inserted" into
  * the stream of keys returned by "inkey()".  This key sequence will not
@@ -2018,7 +1971,7 @@ static cptr inkey_next = NULL;
 #ifdef ALLOW_BORG
 
 /*
- * Mega-Hack -- special "inkey_hack" hook.  XXX XXX XXX
+ * Mega-Hack -- special "inkey_hack" hook.  
  *
  * This special function hook allows the "Borg" (see elsewhere) to take
  * control of the "inkey()" function, and substitute in fake keypresses.
@@ -2205,11 +2158,9 @@ char inkey(void)
                                /* Wait for (and remove) a pending key */
                                if (0 == Term_inkey(&ch, TRUE, TRUE))
                                {
-                                       /* Done */
                                        break;
                                }
 
-                               /* Oops */
                                break;
                        }
 
@@ -2219,7 +2170,6 @@ char inkey(void)
                                /* Check for (and remove) a pending key */
                                if (0 == Term_inkey(&ch, FALSE, TRUE))
                                {
-                                       /* Done */
                                        break;
                                }
 
@@ -2232,12 +2182,10 @@ char inkey(void)
                                        /* Excessive delay */
                                        if (w >= 100) break;
 
-                                       /* Delay */
                                        Term_xtra(TERM_XTRA_DELAY, w);
                                }
                        }
 
-                       /* Done */
                        break;
                }
 
@@ -2251,8 +2199,6 @@ char inkey(void)
                {
                        /* Strip this key */
                        ch = 0;
-
-                       /* Continue */
                        continue;
                }
 
@@ -2351,9 +2297,9 @@ void quark_init(void)
 /*
  * Add a new "quark" to the set of quarks.
  */
-s16b quark_add(cptr str)
+u16b quark_add(cptr str)
 {
-       int i;
+       u16b i;
 
        /* Look for an existing quark */
        for (i = 1; i < quark__num; i++)
@@ -2477,7 +2423,8 @@ cptr message_str(int age)
  */
 void message_add(cptr str)
 {
-       int i, k, x, m, n;
+       u32b i, n;
+       int k, x, m;
 
        char u[4096];
        char splitted1[81];
@@ -2559,7 +2506,7 @@ void message_add(cptr str)
                if (*t)
                {
                        /* Message is too small */
-                       if (strlen(buf) < 6) break;
+                       if (strlen(buf) < A_MAX) break;
 
                        /* Drop the space */
                        *(t - 1) = '\0';
@@ -2592,7 +2539,6 @@ void message_add(cptr str)
                        now_message++;
                }
 
-               /* Done */
                break;
        }
 
@@ -2799,6 +2745,12 @@ static void msg_flush(int x)
 }
 
 
+void msg_erase(void)
+{
+       msg_print(NULL);
+}
+
+
 /*
  * Output a message to the top line of the screen.
  *
@@ -2815,13 +2767,13 @@ static void msg_flush(int x)
  * The global "msg_flag" variable can be cleared to tell us to
  * "erase" any "pending" messages still on the screen.
  *
- * XXX XXX XXX Note that we must be very careful about using the
+ * Note that we must be very careful about using the
  * "msg_print()" functions without explicitly calling the special
  * "msg_print(NULL)" function, since this may result in the loss
  * of information if the screen is cleared, or if anything is
  * displayed on the top line.
  *
- * XXX XXX XXX Note that "msg_print(NULL)" will clear the top line
+ * Note that "msg_print(NULL)" will clear the top line
  * even if no messages are pending.  This is probably a hack.
  */
 void msg_print(cptr msg)
@@ -2846,7 +2798,6 @@ void msg_print(cptr msg)
        /* Hack -- flush when requested or needed */
        if (p && (!msg || ((p + n) > 72)))
        {
-               /* Flush */
                msg_flush(p);
 
                /* Forget it */
@@ -2873,7 +2824,7 @@ void msg_print(cptr msg)
        }
 
        /* New Message Length */
-       n = (buf ? strlen(buf) : 0);
+       n = strlen(buf);
 
        /* Memorize the message */
        if (character_generated) message_add(buf);
@@ -2958,9 +2909,8 @@ void msg_print(cptr msg)
        /* Memorize the tail */
        /* if (character_generated) message_add(t); */
 
-       /* Window stuff */
        p_ptr->window |= (PW_MESSAGE);
-       window_stuff();
+       update_output();
 
        /* Remember the message */
        msg_flag = TRUE;
@@ -3093,7 +3043,7 @@ void msg_format_wizard(int cheat_type, cptr fmt, ...)
  * At the given location, using the given attribute, if allowed,
  * add the given string.  Do not clear the line.
  */
-void c_put_str(byte attr, cptr str, int row, int col)
+void c_put_str(TERM_COLOR attr, cptr str, TERM_LEN row, TERM_LEN col)
 {
        /* Position cursor, Dump the attr/text */
        Term_putstr(col, row, -1, attr, str);
@@ -3102,7 +3052,7 @@ void c_put_str(byte attr, cptr str, int row, int col)
 /*
  * As above, but in "white"
  */
-void put_str(cptr str, int row, int col)
+void put_str(cptr str, TERM_LEN row, TERM_LEN col)
 {
        /* Spawn */
        Term_putstr(col, row, -1, TERM_WHITE, str);
@@ -3114,7 +3064,7 @@ void put_str(cptr str, int row, int col)
  * Display a string on the screen using an attribute, and clear
  * to the end of the line.
  */
-void c_prt(byte attr, cptr str, int row, int col)
+void c_prt(TERM_COLOR attr, cptr str, TERM_LEN row, TERM_LEN col)
 {
        /* Clear line, position cursor */
        Term_erase(col, row, 255);
@@ -3126,7 +3076,7 @@ void c_prt(byte attr, cptr str, int row, int col)
 /*
  * As above, but in "white"
  */
-void prt(cptr str, int row, int col)
+void prt(cptr str, TERM_LEN row, TERM_LEN col)
 {
        /* Spawn */
        c_prt(TERM_WHITE, str, row, col);
@@ -3208,7 +3158,7 @@ void c_roff(byte a, cptr str)
                {
                        int i, n = 0;
 
-                       byte av[256];
+                       TERM_COLOR av[256];
                        char cv[256];
 
                        /* Wrap word */
@@ -3621,7 +3571,7 @@ bool get_string(cptr prompt, char *buf, int len)
 {
        bool res;
 
-       /* Paranoia XXX XXX XXX */
+       /* Paranoia */
        msg_print(NULL);
 
        /* Display prompt */
@@ -3632,8 +3582,6 @@ bool get_string(cptr prompt, char *buf, int len)
 
        /* Clear prompt */
        prt("", 0, 0);
-
-       /* Result */
        return (res);
 }
 
@@ -3667,11 +3615,11 @@ bool get_check_strict(cptr prompt, BIT_FLAGS mode)
        if (auto_more)
        {
                p_ptr->window |= PW_MESSAGE;
-               window_stuff();
+               handle_stuff();
                num_more = 0;
        }
 
-       /* Paranoia XXX XXX XXX */
+       /* Paranoia */
        msg_print(NULL);
 
        if (!rogue_like_commands)
@@ -3703,7 +3651,7 @@ bool get_check_strict(cptr prompt, BIT_FLAGS mode)
                /* HACK : Add the line to message buffer */
                message_add(buf);
                p_ptr->window |= (PW_MESSAGE);
-               window_stuff();
+               handle_stuff();
        }
 
        /* Get an acceptable answer */
@@ -3773,7 +3721,7 @@ bool get_check_strict(cptr prompt, BIT_FLAGS mode)
  */
 bool get_com(cptr prompt, char *command, bool z_escape)
 {
-       /* Paranoia XXX XXX XXX */
+       /* Paranoia */
        msg_print(NULL);
 
        /* Display a prompt */
@@ -3804,10 +3752,11 @@ bool get_com(cptr prompt, char *command, bool z_escape)
  */
 QUANTITY get_quantity(cptr prompt, QUANTITY max)
 {
-       bool res;
-       COMMAND_CODE amt;
+       bool res, result;
+       QUANTITY amt;
        char tmp[80];
        char buf[80];
+       COMMAND_CODE code;
 
 
        /* Use "command_arg" */
@@ -3826,10 +3775,10 @@ QUANTITY get_quantity(cptr prompt, QUANTITY max)
                return (amt);
        }
 
-#ifdef ALLOW_REPEAT /* TNB */
-
        /* Get the item index */
-       if ((max != 1) && repeat_pull(&amt))
+       result = repeat_pull(&code);
+       amt = (QUANTITY)code;
+       if ((max != 1) && result)
        {
                /* Enforce the maximum */
                if (amt > max) amt = max;
@@ -3841,8 +3790,6 @@ QUANTITY get_quantity(cptr prompt, QUANTITY max)
                return (amt);
        }
 
-#endif /* ALLOW_REPEAT -- TNB */
-
        /* Build a prompt if needed */
        if (!prompt)
        {
@@ -3853,7 +3800,7 @@ QUANTITY get_quantity(cptr prompt, QUANTITY max)
                prompt = tmp;
        }
 
-       /* Paranoia XXX XXX XXX */
+       /* Paranoia */
        msg_print(NULL);
 
        /* Display prompt */
@@ -3889,11 +3836,7 @@ QUANTITY get_quantity(cptr prompt, QUANTITY max)
        /* Enforce the minimum */
        if (amt < 0) amt = 0;
 
-#ifdef ALLOW_REPEAT /* TNB */
-
-       if (amt) repeat_push(amt);
-
-#endif /* ALLOW_REPEAT -- TNB */
+       if (amt) repeat_push((COMMAND_CODE)amt);
 
        /* Return the result */
        return (amt);
@@ -3901,7 +3844,7 @@ QUANTITY get_quantity(cptr prompt, QUANTITY max)
 
 
 /*
- * Pause for user response XXX XXX XXX
+ * Pause for user response 
  */
 void pause_line(int row)
 {
@@ -4305,7 +4248,6 @@ static char inkey_from_menu(void)
                /* Place the cursor on the player */
                move_cursor_relative(p_ptr->y, p_ptr->x);
 
-               /* Get a command */
                sub_cmd = inkey();
                if ((sub_cmd == ' ') || (sub_cmd == 'x') || (sub_cmd == 'X') || (sub_cmd == '\r') || (sub_cmd == '\n'))
                {
@@ -4398,14 +4340,14 @@ static char inkey_from_menu(void)
  * Note that this command is used both in the dungeon and in
  * stores, and must be careful to work in both situations.
  *
- * Note that "p_ptr->command_new" may not work any more.  XXX XXX XXX
+ * Note that "p_ptr->command_new" may not work any more.  
  */
 void request_command(int shopping)
 {
        int i;
 
-       char cmd;
-       BIT_FLAGS mode;
+       s16b cmd;
+       int mode;
 
        cptr act;
 
@@ -4443,8 +4385,7 @@ void request_command(int shopping)
                /* Hack -- auto-commands */
                if (command_new)
                {
-                       /* Flush messages */
-                       msg_print(NULL);
+                       msg_erase();
 
                        /* Use auto-command */
                        cmd = command_new;
@@ -4463,7 +4404,6 @@ void request_command(int shopping)
                        /* Activate "command mode" */
                        inkey_flag = TRUE;
 
-                       /* Get a command */
                        cmd = inkey();
 
                        if (!shopping && command_menu && ((cmd == '\r') || (cmd == '\n') || (cmd == 'x') || (cmd == 'X'))
@@ -4561,8 +4501,6 @@ void request_command(int shopping)
                                {
                                        /* Clear count */
                                        command_arg = 0;
-
-                                       /* Continue */
                                        continue;
                                }
                        }
@@ -4599,8 +4537,6 @@ void request_command(int shopping)
 
                        /* Start using the buffer */
                        inkey_next = request_command_buffer;
-
-                       /* Continue */
                        continue;
                }
 
@@ -4612,7 +4548,6 @@ void request_command(int shopping)
                /* Use command */
                command_cmd = (byte)cmd;
 
-               /* Done */
                break;
        }
 
@@ -4851,8 +4786,6 @@ int get_keymap_dir(char ch)
 }
 
 
-#ifdef ALLOW_REPEAT /* TNB */
-
 #define REPEAT_MAX             20
 
 /* Number of chars saved */
@@ -4928,8 +4861,6 @@ void repeat_check(void)
        }
 }
 
-#endif /* ALLOW_REPEAT -- TNB */
-
 
 #ifdef SORT_R_INFO
 
@@ -5304,12 +5235,12 @@ void roff_to_buf(cptr str, int maxlen, char *tbuf, size_t bufsize)
  *
  * This function should be equivalent to the strlcpy() function in BSD.
  */
-size_t my_strcpy(char *buf, const char *src, size_t bufsize)
+size_t my_strcpy(char *buf, cptr src, size_t bufsize)
 {
 #ifdef JP
 
        char *d = buf;
-       const char *s = src;
+       cptr s = src;
        size_t len = 0;
 
        if (bufsize > 0) {
@@ -5372,7 +5303,7 @@ size_t my_strcpy(char *buf, const char *src, size_t bufsize)
  *
  * This function should be equivalent to the strlcat() function in BSD.
  */
-size_t my_strcat(char *buf, const char *src, size_t bufsize)
+size_t my_strcat(char *buf, cptr src, size_t bufsize)
 {
        size_t dlen = strlen(buf);
 
@@ -5395,7 +5326,7 @@ size_t my_strcat(char *buf, const char *src, size_t bufsize)
  *
  * my_strstr() can handle Kanji strings correctly.
  */
-char *my_strstr(const char *haystack, const char *needle)
+char *my_strstr(cptr haystack, cptr needle)
 {
        int i;
        int l1 = strlen(haystack);
@@ -5423,7 +5354,7 @@ char *my_strstr(const char *haystack, const char *needle)
  *
  * my_strchr() can handle Kanji strings correctly.
  */
-char *my_strchr(const char *ptr, char ch)
+char *my_strchr(cptr ptr, char ch)
 {
        for ( ; *ptr != '\0'; ptr++)
        {