OSDN Git Service

always_pickupがオンのとき、自動拾い/破壊の設定による破壊だけを無視して
[hengbandforosx/hengbandosx.git] / src / autopick.c
1 /* File: autopick.c */
2
3 /* Purpose: Object Auto-picker/Destroyer */
4
5 /*
6  * Copyright (c) 2002  Mogami
7  *
8  * This software may be copied and distributed for educational, research, and
9  * not for profit purposes provided that this copyright and statement are
10  * included in all such copies.
11  */
12
13 #include "angband.h"
14
15 /*
16  * Macros for Keywords
17  */
18 #define FLG_ALL             0
19 #define FLG_COLLECTING      1
20 #define FLG_UNIDENTIFIED    2
21 #define FLG_IDENTIFIED      3
22 #define FLG_STAR_IDENTIFIED 4
23 #define FLG_BOOSTED         5
24 #define FLG_MORE_THAN       6
25 #define FLG_DICE            7
26 #define FLG_MORE_BONUS      8
27 #define FLG_MORE_BONUS2     9
28 #define FLG_WORTHLESS       10
29 #define FLG_ARTIFACT        11
30 #define FLG_EGO             12
31 #define FLG_NAMELESS        13
32 #define FLG_UNAWARE         14
33 #define FLG_WANTED          15
34 #define FLG_UNIQUE          16
35 #define FLG_HUMAN           17
36 #define FLG_UNREADABLE      18
37 #define FLG_REALM1          19
38 #define FLG_REALM2          20
39 #define FLG_FIRST           21
40 #define FLG_SECOND          22
41 #define FLG_THIRD           23
42 #define FLG_FOURTH          24
43
44 #define FLG_ITEMS           30
45 #define FLG_WEAPONS         31
46 #define FLG_ARMORS          32
47 #define FLG_MISSILES        33
48 #define FLG_DEVICES         34
49 #define FLG_LIGHTS          35
50 #define FLG_JUNKS           36
51 #define FLG_SPELLBOOKS      37
52 #define FLG_HAFTED          38
53 #define FLG_SHIELDS         39
54 #define FLG_BOWS            40
55 #define FLG_RINGS           41
56 #define FLG_AMULETS         42
57 #define FLG_SUITS           43
58 #define FLG_CLOAKS          44
59 #define FLG_HELMS           45
60 #define FLG_GLOVES          46
61 #define FLG_BOOTS           47
62
63 #ifdef JP
64
65 #define KEY_ALL "¤¹¤Ù¤Æ¤Î"
66 #define KEY_COLLECTING "¼ý½¸Ãæ¤Î"
67 #define KEY_UNIDENTIFIED "̤´ÕÄê¤Î"
68 #define KEY_IDENTIFIED "´ÕÄêºÑ¤ß¤Î"
69 #define KEY_STAR_IDENTIFIED "*´ÕÄê*ºÑ¤ß¤Î"
70 #define KEY_BOOSTED "¥À¥¤¥¹Ìܤΰ㤦"
71 #define KEY_MORE_THAN  "¥À¥¤¥¹ÌÜ"
72 #define KEY_DICE  "°Ê¾å¤Î"
73 #define KEY_MORE_BONUS  "½¤ÀµÃͤ¬"
74 #define KEY_MORE_BONUS2  "°Ê¾å¤Î"
75 #define KEY_WORTHLESS "̵²ÁÃͤÎ"
76 #define KEY_ARTIFACT "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È"
77 #define KEY_EGO "¥¨¥´"
78 #define KEY_NAMELESS "̵ÌäÎ"
79 #define KEY_UNAWARE "̤ȽÌÀ¤Î"
80 #define KEY_WANTED "¾Þ¶â¼ó¤Î"
81 #define KEY_UNIQUE "¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼¤Î"
82 #define KEY_HUMAN "¿Í´Ö¤Î"
83 #define KEY_UNREADABLE "Æɤá¤Ê¤¤"
84 #define KEY_REALM1 "Âè°ìÎΰè¤Î"
85 #define KEY_REALM2 "ÂèÆóÎΰè¤Î"
86 #define KEY_FIRST "1ºýÌܤÎ"
87 #define KEY_SECOND "2ºýÌܤÎ"
88 #define KEY_THIRD "3ºýÌܤÎ"
89 #define KEY_FOURTH "4ºýÌܤÎ"
90 #define KEY_ITEMS "¥¢¥¤¥Æ¥à"
91 #define KEY_WEAPONS "Éð´ï"
92 #define KEY_ARMORS "Ëɶñ"
93 #define KEY_MISSILES "Ìð"
94 #define KEY_DEVICES "ËâË¡¥¢¥¤¥Æ¥à"
95 #define KEY_LIGHTS "¸÷¸»"
96 #define KEY_JUNKS "¤¬¤é¤¯¤¿"
97 #define KEY_SPELLBOOKS "ËâË¡½ñ"
98 #define KEY_HAFTED "Æß´ï"
99 #define KEY_SHIELDS "½â"
100 #define KEY_BOWS "µÝ"
101 #define KEY_RINGS "»ØÎØ"
102 #define KEY_AMULETS "¥¢¥ß¥å¥ì¥Ã¥È"
103 #define KEY_SUITS "³»"
104 #define KEY_CLOAKS "¥¯¥í¡¼¥¯"
105 #define KEY_HELMS "³õ"
106 #define KEY_GLOVES "äƼê"
107 #define KEY_BOOTS "·¤"
108
109 #else 
110
111 #define KEY_ALL "all"
112 #define KEY_COLLECTING "collecting"
113 #define KEY_UNIDENTIFIED "unidentified"
114 #define KEY_IDENTIFIED "identified"
115 #define KEY_STAR_IDENTIFIED "*identified*"
116 #define KEY_BOOSTED "dice boosted"
117 #define KEY_MORE_THAN  "more than"
118 #define KEY_DICE  " dice"
119 #define KEY_MORE_BONUS  "more bonus than"
120 #define KEY_MORE_BONUS2  ""
121 #define KEY_WORTHLESS "worthless"
122 #define KEY_ARTIFACT "artifact"
123 #define KEY_EGO "ego"
124 #define KEY_NAMELESS "nameless"
125 #define KEY_UNAWARE "unaware"
126 #define KEY_WANTED "wanted"
127 #define KEY_UNIQUE "unique monster's"
128 #define KEY_HUMAN "human"
129 #define KEY_UNREADABLE "unreadable"
130 #define KEY_REALM1 "first realm's"
131 #define KEY_REALM2 "second realm's"
132 #define KEY_FIRST "first"
133 #define KEY_SECOND "second"
134 #define KEY_THIRD "third"
135 #define KEY_FOURTH "fourth"
136 #define KEY_ITEMS "items"
137 #define KEY_WEAPONS "weapons"
138 #define KEY_ARMORS "armors"
139 #define KEY_MISSILES "missiles"
140 #define KEY_DEVICES "magical devices"
141 #define KEY_LIGHTS "lights"
142 #define KEY_JUNKS "junks"
143 #define KEY_SPELLBOOKS "spellbooks"
144 #define KEY_HAFTED "hafted weapons"
145 #define KEY_SHIELDS "shields"
146 #define KEY_BOWS "bows"
147 #define KEY_RINGS "rings"
148 #define KEY_AMULETS "amulets"
149 #define KEY_SUITS "suits"
150 #define KEY_CLOAKS "cloaks"
151 #define KEY_HELMS "helms"
152 #define KEY_GLOVES "gloves"
153 #define KEY_BOOTS "boots"
154
155 #endif /* JP */
156
157 #define MATCH_KEY(KEY) (!strncmp(ptr, KEY, sizeof(KEY)-1)\
158      ? (prev_ptr = ptr, ptr += sizeof(KEY)-1, (' '==*ptr) ? ptr++ : 0, TRUE) : FALSE)
159
160 #ifdef JP
161 #define ADD_KEY(KEY) strcat(ptr, KEY)
162 #else
163 #define ADD_KEY(KEY) (strcat(ptr, KEY), strcat(ptr, " "))
164 #endif
165 #define ADD_KEY2(KEY) strcat(ptr, KEY)
166
167 #define ADD_FLG(FLG) (entry->flag[FLG / 32] |= (1L << (FLG % 32)))
168 #define REM_FLG(FLG) (entry->flag[FLG / 32] &= ~(1L << (FLG % 32)))
169 #define ADD_FLG2(FLG) (entry->flag[FLG / 32] |= (1L << (FLG % 32)), prev_flg = FLG)
170 #define IS_FLG(FLG) (entry->flag[FLG / 32] & (1L << (FLG % 32)))
171
172 #ifdef JP
173         static char kanji_colon[] = "¡§";
174 #endif
175
176
177 /*
178  * Reconstruct preference line from entry
179  */
180 cptr autopick_line_from_entry(autopick_type *entry)
181 {
182         char buf[1024];
183         char *ptr;
184         bool sepa_flag = TRUE;
185
186         *buf = '\0';
187         if (!(entry->action & DO_DISPLAY)) strcat(buf, "(");
188         if (entry->action & DO_AUTODESTROY) strcat(buf, "!");
189         if (entry->action & DONT_AUTOPICK) strcat(buf, "~");
190
191         ptr = buf;
192
193         if (IS_FLG(FLG_ALL)) ADD_KEY(KEY_ALL);
194         if (IS_FLG(FLG_COLLECTING)) ADD_KEY(KEY_COLLECTING);
195         if (IS_FLG(FLG_UNIDENTIFIED)) ADD_KEY(KEY_UNIDENTIFIED);
196         if (IS_FLG(FLG_IDENTIFIED)) ADD_KEY(KEY_IDENTIFIED);
197         if (IS_FLG(FLG_STAR_IDENTIFIED)) ADD_KEY(KEY_STAR_IDENTIFIED);
198         if (IS_FLG(FLG_BOOSTED)) ADD_KEY(KEY_BOOSTED);
199
200         if (IS_FLG(FLG_MORE_THAN))
201         {
202                 ADD_KEY(KEY_MORE_THAN);
203                 strcat(ptr, format("%d", entry->dice));
204                 ADD_KEY(KEY_DICE);
205         }
206
207         if (IS_FLG(FLG_MORE_BONUS))
208         {
209                 ADD_KEY(KEY_MORE_BONUS);
210                 strcat(ptr, format("%d", entry->bonus));
211                 ADD_KEY(KEY_MORE_BONUS2);
212         }
213
214         if (IS_FLG(FLG_WORTHLESS)) ADD_KEY(KEY_WORTHLESS);
215         if (IS_FLG(FLG_ARTIFACT)) ADD_KEY(KEY_ARTIFACT);
216         if (IS_FLG(FLG_EGO)) ADD_KEY(KEY_EGO);
217         if (IS_FLG(FLG_NAMELESS)) ADD_KEY(KEY_NAMELESS);
218         if (IS_FLG(FLG_UNAWARE)) ADD_KEY(KEY_UNAWARE);
219         if (IS_FLG(FLG_WANTED)) ADD_KEY(KEY_WANTED);
220         if (IS_FLG(FLG_UNIQUE)) ADD_KEY(KEY_UNIQUE);
221         if (IS_FLG(FLG_HUMAN)) ADD_KEY(KEY_HUMAN);
222         if (IS_FLG(FLG_UNREADABLE)) ADD_KEY(KEY_UNREADABLE);
223         if (IS_FLG(FLG_REALM1)) ADD_KEY(KEY_REALM1);
224         if (IS_FLG(FLG_REALM2)) ADD_KEY(KEY_REALM2);
225         if (IS_FLG(FLG_FIRST)) ADD_KEY(KEY_FIRST);
226         if (IS_FLG(FLG_SECOND)) ADD_KEY(KEY_SECOND);
227         if (IS_FLG(FLG_THIRD)) ADD_KEY(KEY_THIRD);
228         if (IS_FLG(FLG_FOURTH)) ADD_KEY(KEY_FOURTH);
229
230         if (IS_FLG(FLG_ITEMS)) ADD_KEY2(KEY_ITEMS);
231         else if (IS_FLG(FLG_WEAPONS)) ADD_KEY2(KEY_WEAPONS);
232         else if (IS_FLG(FLG_ARMORS)) ADD_KEY2(KEY_ARMORS);
233         else if (IS_FLG(FLG_MISSILES)) ADD_KEY2(KEY_MISSILES);
234         else if (IS_FLG(FLG_DEVICES)) ADD_KEY2(KEY_DEVICES);
235         else if (IS_FLG(FLG_LIGHTS)) ADD_KEY2(KEY_LIGHTS);
236         else if (IS_FLG(FLG_JUNKS)) ADD_KEY2(KEY_JUNKS);
237         else if (IS_FLG(FLG_SPELLBOOKS)) ADD_KEY2(KEY_SPELLBOOKS);
238         else if (IS_FLG(FLG_HAFTED)) ADD_KEY2(KEY_HAFTED);
239         else if (IS_FLG(FLG_SHIELDS)) ADD_KEY2(KEY_SHIELDS);
240         else if (IS_FLG(FLG_BOWS)) ADD_KEY2(KEY_BOWS);
241         else if (IS_FLG(FLG_RINGS)) ADD_KEY2(KEY_RINGS);
242         else if (IS_FLG(FLG_AMULETS)) ADD_KEY2(KEY_AMULETS);
243         else if (IS_FLG(FLG_SUITS)) ADD_KEY2(KEY_SUITS);
244         else if (IS_FLG(FLG_CLOAKS)) ADD_KEY2(KEY_CLOAKS);
245         else if (IS_FLG(FLG_HELMS)) ADD_KEY2(KEY_HELMS);
246         else if (IS_FLG(FLG_GLOVES)) ADD_KEY2(KEY_GLOVES);
247         else if (IS_FLG(FLG_BOOTS)) ADD_KEY2(KEY_BOOTS);
248         else
249                 sepa_flag = FALSE;
250
251         if (entry->name && *entry->name)
252         {
253                 if (sepa_flag)
254                         strcat(buf, ":");
255                 strcat(buf, entry->name);
256         }
257         else
258         {
259                 if (entry->flag[0] == 0L && entry->flag[0] == 0L)
260                         return NULL;
261         }
262
263         if (entry->insc)
264         {
265                 strcat(buf, "#");
266                 strcat(buf, entry->insc);
267         }
268
269         return string_make(buf);
270 }
271
272 /*
273  * A function to create new entry
274  */
275 bool autopick_new_entry(autopick_type *entry, cptr str)
276 {
277         cptr insc;
278         int i;
279         byte act = 0;
280         char buf[1024];
281         cptr prev_ptr, ptr;
282         int prev_flg;
283
284         if (str[1] == ':') switch (str[0])
285         {
286         case '?': case '%':
287         case 'A': case 'P': case 'C':
288                 return FALSE;
289         }
290
291         entry->flag[0] = entry->flag[1] = 0L;
292         entry->dice = 0;
293
294         act = DO_AUTOPICK | DO_DISPLAY;
295         while (1)
296         {
297                 if ((act & DO_AUTOPICK) && *str == '!')
298                 {
299                         act &= ~DO_AUTOPICK;
300                         act |= DO_AUTODESTROY;
301                         str++;
302                 }
303                 else if ((act & DO_AUTOPICK) && *str == '~')
304                 {
305                         act &= ~DO_AUTOPICK;
306                         act |= DONT_AUTOPICK;
307                         str++;
308                 }
309                 else if ((act & DO_DISPLAY) && *str == '(')
310                 {
311                         act &= ~DO_DISPLAY;
312                         str++;
313                 }
314                 else
315                         break;
316         }
317
318         /* don't mind upper or lower case */
319         insc = NULL;
320         for (i = 0; *str; i++)
321         {
322                 char c = *str++;
323 #ifdef JP
324                 if (iskanji(c))
325                 {
326                         buf[i++] = c;
327                         buf[i] = *str++;
328                         continue;
329                 }
330 #endif
331                 /* Auto-inscription? */
332                 if (c == '#')
333                 {
334                         buf[i] = '\0';
335                         insc = str;
336                         break;
337                 }
338
339                 if (isupper(c)) c = tolower(c);
340
341                 buf[i] = c;
342         }
343         buf[i] = '\0';
344         
345         /* Skip empty line */
346         if (*buf == 0) return FALSE;
347
348         /* Found flags */
349         prev_ptr = ptr = buf;
350         prev_flg = -1;
351         if (MATCH_KEY(KEY_ALL)) ADD_FLG(FLG_ALL);
352         if (MATCH_KEY(KEY_COLLECTING)) ADD_FLG(FLG_COLLECTING);
353         if (MATCH_KEY(KEY_UNIDENTIFIED)) ADD_FLG(FLG_UNIDENTIFIED);
354         if (MATCH_KEY(KEY_IDENTIFIED)) ADD_FLG(FLG_IDENTIFIED);
355         if (MATCH_KEY(KEY_STAR_IDENTIFIED)) ADD_FLG(FLG_STAR_IDENTIFIED);
356         if (MATCH_KEY(KEY_BOOSTED)) ADD_FLG(FLG_BOOSTED);
357
358         /*** Weapons whose dd*ds is more than nn ***/
359         if (MATCH_KEY(KEY_MORE_THAN))
360         {
361                 int k = 0;
362                 entry->dice = 0;
363
364                 /* Drop leading spaces */
365                 while (' ' == *ptr) ptr++;
366
367                 /* Read number */
368                 while (isdigit(*ptr))
369                 {
370                         entry->dice = 10 * entry->dice + (*ptr - '0');
371                         ptr++;
372                         k++;
373                 }
374
375                 if (k > 0 && k <= 2)
376                 {
377                         (void)MATCH_KEY(KEY_DICE);
378                         ADD_FLG(FLG_MORE_THAN);
379                 }
380                 else
381                         ptr = prev_ptr;
382         }
383
384         /*** Items whose magical bonus is more than n ***/
385         if (MATCH_KEY(KEY_MORE_BONUS))
386         {
387                 int k = 0;
388                 entry->bonus = 0;
389
390                 /* Drop leading spaces */
391                 while (' ' == *ptr) ptr++;
392
393                 /* Read number */
394                 while (isdigit(*ptr))
395                 {
396                         entry->bonus = 10 * entry->bonus + (*ptr - '0');
397                         ptr++;
398                         k++;
399                 }
400
401                 if (k > 0 && k <= 2)
402                 {
403                         (void)MATCH_KEY(KEY_MORE_BONUS2);
404                         ADD_FLG(FLG_MORE_BONUS);
405                 }
406                 else
407                         ptr = prev_ptr;
408         }
409
410         if (MATCH_KEY(KEY_WORTHLESS)) ADD_FLG(FLG_WORTHLESS);
411         if (MATCH_KEY(KEY_ARTIFACT)) ADD_FLG(FLG_ARTIFACT);
412         if (MATCH_KEY(KEY_EGO)) ADD_FLG(FLG_EGO);
413         if (MATCH_KEY(KEY_NAMELESS)) ADD_FLG(FLG_NAMELESS);
414         if (MATCH_KEY(KEY_UNAWARE)) ADD_FLG(FLG_UNAWARE);
415         if (MATCH_KEY(KEY_WANTED)) ADD_FLG(FLG_WANTED);
416         if (MATCH_KEY(KEY_UNIQUE)) ADD_FLG(FLG_UNIQUE);
417         if (MATCH_KEY(KEY_HUMAN)) ADD_FLG(FLG_HUMAN);
418         if (MATCH_KEY(KEY_UNREADABLE)) ADD_FLG(FLG_UNREADABLE);
419         if (MATCH_KEY(KEY_REALM1)) ADD_FLG(FLG_REALM1);
420         if (MATCH_KEY(KEY_REALM2)) ADD_FLG(FLG_REALM2);
421         if (MATCH_KEY(KEY_FIRST)) ADD_FLG(FLG_FIRST);
422         if (MATCH_KEY(KEY_SECOND)) ADD_FLG(FLG_SECOND);
423         if (MATCH_KEY(KEY_THIRD)) ADD_FLG(FLG_THIRD);
424         if (MATCH_KEY(KEY_FOURTH)) ADD_FLG(FLG_FOURTH);
425
426         /* Reset previous word location */
427         prev_ptr = ptr;
428
429         if (MATCH_KEY(KEY_ITEMS)) ADD_FLG2(FLG_ITEMS);
430         else if (MATCH_KEY(KEY_WEAPONS)) ADD_FLG2(FLG_WEAPONS);
431         else if (MATCH_KEY(KEY_ARMORS)) ADD_FLG2(FLG_ARMORS);
432         else if (MATCH_KEY(KEY_MISSILES)) ADD_FLG2(FLG_MISSILES);
433         else if (MATCH_KEY(KEY_DEVICES)) ADD_FLG2(FLG_DEVICES);
434         else if (MATCH_KEY(KEY_LIGHTS)) ADD_FLG2(FLG_LIGHTS);
435         else if (MATCH_KEY(KEY_JUNKS)) ADD_FLG2(FLG_JUNKS);
436         else if (MATCH_KEY(KEY_SPELLBOOKS)) ADD_FLG2(FLG_SPELLBOOKS);
437         else if (MATCH_KEY(KEY_HAFTED)) ADD_FLG2(FLG_HAFTED);
438         else if (MATCH_KEY(KEY_SHIELDS)) ADD_FLG2(FLG_SHIELDS);
439         else if (MATCH_KEY(KEY_BOWS)) ADD_FLG2(FLG_BOWS);
440         else if (MATCH_KEY(KEY_RINGS)) ADD_FLG2(FLG_RINGS);
441         else if (MATCH_KEY(KEY_AMULETS)) ADD_FLG2(FLG_AMULETS);
442         else if (MATCH_KEY(KEY_SUITS)) ADD_FLG2(FLG_SUITS);
443         else if (MATCH_KEY(KEY_CLOAKS)) ADD_FLG2(FLG_CLOAKS);
444         else if (MATCH_KEY(KEY_HELMS)) ADD_FLG2(FLG_HELMS);
445         else if (MATCH_KEY(KEY_GLOVES)) ADD_FLG2(FLG_GLOVES);
446         else if (MATCH_KEY(KEY_BOOTS)) ADD_FLG2(FLG_BOOTS);
447
448         /* Last 'keyword' must be at the correct location */
449         if (*ptr == ':')
450                 ptr++;
451         else if (*ptr == '\0')
452                 ; /* nothing to do */
453 #ifdef JP
454         else if (ptr[0] == kanji_colon[0] && ptr[1] == kanji_colon[1])
455                 ptr += 2;
456 #endif
457         else
458         {
459                 if (prev_flg != -1)
460                         entry->flag[prev_flg/32] &= ~(1L<< (prev_flg%32));
461                 ptr = prev_ptr;
462         }
463         entry->name = string_make(ptr);
464         entry->action = act;
465         entry->insc = string_make(insc);
466
467         return TRUE;
468 }
469
470 /*
471  * A function to delete entry
472  */
473 void autopick_free_entry(autopick_type *entry)
474 {
475         string_free(entry->name);
476         string_free(entry->insc);
477 }
478
479 /*
480  * A function for Auto-picker/destroyer
481  * Examine whether the object matches to the list of keywords or not.
482  */
483 int is_autopick(object_type *o_ptr)
484 {
485         int i;
486         char o_name[MAX_NLEN];
487
488         if (o_ptr->tval == TV_GOLD) return -1;
489         
490         object_desc(o_name, o_ptr, FALSE, 3);
491
492         /* Force to be lower case string */
493         for (i = 0; o_name[i]; i++)
494         {
495 #ifdef JP
496                 if (iskanji(o_name[i]))
497                         i++;
498                 else
499 #endif
500                 if (isupper(o_name[i]))
501                         o_name[i] = tolower(o_name[i]);
502         }
503         
504         for (i=0; i < max_autopick; i++)
505         {
506                 autopick_type *entry = &autopick_list[i];
507                 bool flag = FALSE;
508                 cptr ptr = autopick_list[i].name;
509
510                 /*** Unidentified ***/
511                 if (IS_FLG(FLG_UNIDENTIFIED)
512                     && (object_known_p(o_ptr) || (o_ptr->ident & IDENT_SENSE)))
513                         continue;
514
515                 /*** Identified ***/
516                 if (IS_FLG(FLG_IDENTIFIED) && !object_known_p(o_ptr))
517                         continue;
518
519                 /*** *Identified* ***/
520                 if (IS_FLG(FLG_STAR_IDENTIFIED) &&
521                     (!object_known_p(o_ptr) || !(o_ptr->ident & IDENT_MENTAL)))
522                         continue;
523
524                 /*** Dice boosted (weapon of slaying) ***/
525                 if (IS_FLG(FLG_BOOSTED))
526                 {
527                         object_kind *k_ptr = &k_info[o_ptr->k_idx];
528                         
529                         switch( o_ptr->tval )
530                         {
531                         case TV_HAFTED:
532                         case TV_POLEARM:
533                         case TV_SWORD:
534                         case TV_DIGGING:
535                                 if ((o_ptr->dd != k_ptr->dd) || (o_ptr->ds != k_ptr->ds))
536                                         break;
537                                 else
538                                         continue;
539                         default:
540                                 continue;
541                         }
542                 }
543
544                 /*** Weapons whic dd*ds is more than nn ***/
545                 if (IS_FLG(FLG_MORE_THAN))
546                 {
547                         if (o_ptr->dd * o_ptr->ds < entry->dice)
548                                 continue;
549                 }
550                                 
551                 /*** Weapons whic dd*ds is more than nn ***/
552                 if (IS_FLG(FLG_MORE_BONUS))
553                 {
554                         if (!object_known_p(o_ptr)) continue;
555
556                         if (k_info[o_ptr->k_idx].pval ||
557                             (o_ptr->name2 && e_info[o_ptr->name2].max_pval))
558                         {
559                                 if (o_ptr->pval < entry->bonus) continue;
560                         }
561                         else
562                         {
563                                 if (o_ptr->to_h < entry->bonus &&
564                                     o_ptr->to_d < entry->bonus &&
565                                     o_ptr->to_a < entry->bonus &&
566                                     o_ptr->pval < entry->bonus)
567                                         continue;
568                         }
569                 }
570                                 
571                 /*** Worthless items ***/
572                 if (IS_FLG(FLG_WORTHLESS) && object_value(o_ptr) > 0)
573                         continue;
574
575                 /*** Artifact object ***/
576                 if (IS_FLG(FLG_ARTIFACT))
577                 {
578                         if (!object_known_p(o_ptr) || (!o_ptr->name1 && !o_ptr->art_name))
579                                 continue;
580                 }
581
582                 /*** Ego object ***/
583                 if (IS_FLG(FLG_EGO))
584                 {
585                         if (!object_known_p(o_ptr) || !o_ptr->name2)
586                                 continue;
587                 }
588
589                 /*** Nameless ***/
590                 if (IS_FLG(FLG_NAMELESS))
591                 {
592                         switch (o_ptr->tval)
593                         {
594                         case TV_WHISTLE:
595                         case TV_SHOT: case TV_ARROW: case TV_BOLT: case TV_BOW:
596                         case TV_DIGGING: case TV_HAFTED: case TV_POLEARM: case TV_SWORD: 
597                         case TV_BOOTS: case TV_GLOVES: case TV_HELM: case TV_CROWN:
598                         case TV_SHIELD: case TV_CLOAK:
599                         case TV_SOFT_ARMOR: case TV_HARD_ARMOR: case TV_DRAG_ARMOR:
600                         case TV_LITE: case TV_AMULET: case TV_RING: case TV_CARD:
601                                 if ((!object_known_p(o_ptr) || o_ptr->inscription
602                                      || o_ptr->name1 || o_ptr->name2 || o_ptr->art_name))
603                                         continue;
604                                 break;
605                         default:
606                                 /* don't match */
607                                 continue;
608                         }
609                 }
610
611                 /*** Unaware items ***/
612                 if (IS_FLG(FLG_UNAWARE) && object_aware_p(o_ptr))
613                         continue;
614
615                 /*** Wanted monster's corpse/skeletons ***/
616                 if (IS_FLG(FLG_WANTED) &&
617                     (o_ptr->tval != TV_CORPSE || !object_is_shoukinkubi(o_ptr)))
618                         continue;
619
620                 /*** Unique monster's corpse/skeletons/statues ***/
621                 if (IS_FLG(FLG_UNIQUE) &&
622                     ((o_ptr->tval != TV_CORPSE && o_ptr->tval != TV_STATUE) ||
623                     !(r_info[o_ptr->pval].flags1 & RF1_UNIQUE)))
624                         continue;
625
626                 /*** Human corpse/skeletons (for Daemon magic) ***/
627                 if (IS_FLG(FLG_HUMAN) &&
628                     (o_ptr->tval != TV_CORPSE ||
629                     !strchr("pht", r_info[o_ptr->pval].d_char)))
630                         continue;
631
632                 /*** Unreadable spellbooks ***/
633                 if (IS_FLG(FLG_UNREADABLE) &&
634                     (o_ptr->tval < TV_LIFE_BOOK ||
635                     check_book_realm(o_ptr->tval, o_ptr->sval)))
636                         continue;
637
638                 /*** First realm spellbooks ***/
639                 if (IS_FLG(FLG_REALM1) && 
640                     (REALM1_BOOK != o_ptr->tval ||
641                     p_ptr->pclass == CLASS_SORCERER ||
642                     p_ptr->pclass == CLASS_RED_MAGE))
643                         continue;
644
645                 /*** Second realm spellbooks ***/
646                 if (IS_FLG(FLG_REALM2) &&
647                     (REALM2_BOOK != o_ptr->tval ||
648                     p_ptr->pclass == CLASS_SORCERER ||
649                     p_ptr->pclass == CLASS_RED_MAGE))
650                         continue;
651
652                 /*** First rank spellbooks ***/
653                 if (IS_FLG(FLG_FIRST) &&
654                     (o_ptr->tval < TV_LIFE_BOOK || 0 != o_ptr->sval))
655                         continue;
656
657                 /*** Second rank spellbooks ***/
658                 if (IS_FLG(FLG_SECOND) &&
659                     (o_ptr->tval < TV_LIFE_BOOK || 1 != o_ptr->sval))
660                         continue;
661
662                 /*** Third rank spellbooks ***/
663                 if (IS_FLG(FLG_THIRD) && 
664                     (o_ptr->tval < TV_LIFE_BOOK || 2 != o_ptr->sval))
665                         continue;
666
667                 /*** Fourth rank spellbooks ***/
668                 if (IS_FLG(FLG_FOURTH) &&
669                     (o_ptr->tval < TV_LIFE_BOOK || 3 != o_ptr->sval))
670                         continue;
671
672                 /*** Items ***/
673                 if (IS_FLG(FLG_WEAPONS))
674                 {
675                         switch(o_ptr->tval)
676                         {
677                         case TV_BOW: case TV_HAFTED: case TV_POLEARM:
678                         case TV_SWORD: case TV_DIGGING:
679                                 break;
680                         default: continue;
681                         }
682                 }
683                 else if (IS_FLG(FLG_ARMORS))
684                 {
685                         switch(o_ptr->tval)
686                         {
687                         case TV_BOOTS: case TV_GLOVES: case TV_CLOAK: case TV_CROWN:
688                         case TV_HELM: case TV_SHIELD: case TV_SOFT_ARMOR:
689                         case TV_HARD_ARMOR: case TV_DRAG_ARMOR:
690                                 break;
691                         default: continue;
692                         }
693                 }
694                 else if (IS_FLG(FLG_MISSILES))
695                 {
696                         switch(o_ptr->tval)
697                         {
698                         case TV_SHOT: case TV_BOLT: case TV_ARROW:
699                                 break;
700                         default: continue;
701                         }
702                 }
703                 else if (IS_FLG(FLG_DEVICES))
704                 {
705                         switch(o_ptr->tval)
706                         {
707                         case TV_SCROLL: case TV_STAFF: case TV_WAND: case TV_ROD:
708                                 break;
709                         default: continue;
710                         }
711                 }
712                 else if (IS_FLG(FLG_LIGHTS))
713                 {
714                         if (!(o_ptr->tval == TV_LITE))
715                                 continue;
716                 }
717                 else if (IS_FLG(FLG_JUNKS))
718                 {
719                         switch(o_ptr->tval)
720                         {
721                         case TV_SKELETON: case TV_BOTTLE:
722                         case TV_JUNK: case TV_STATUE:
723                                 break;
724                         default: continue;
725                         }
726                 }
727                 else if (IS_FLG(FLG_SPELLBOOKS))
728                 {
729                         if (!(o_ptr->tval >= TV_LIFE_BOOK))
730                                 continue;
731                 }
732                 else if (IS_FLG(FLG_HAFTED))
733                 {
734                         if (!(o_ptr->tval == TV_HAFTED))
735                                 continue;
736                 }
737                 else if (IS_FLG(FLG_SHIELDS))
738                 {
739                         if (!(o_ptr->tval == TV_SHIELD))
740                                 continue;
741                 }
742                 else if (IS_FLG(FLG_BOWS))
743                 {
744                         if (!(o_ptr->tval == TV_BOW))
745                                 continue;
746                 }
747                 else if (IS_FLG(FLG_RINGS))
748                 {
749                         if (!(o_ptr->tval == TV_RING))
750                                 continue;
751                 }
752                 else if (IS_FLG(FLG_AMULETS))
753                 {
754                         if (!(o_ptr->tval == TV_AMULET))
755                                 continue;
756                 }
757                 else if (IS_FLG(FLG_SUITS))
758                 {
759                         if (!(o_ptr->tval == TV_DRAG_ARMOR ||
760                               o_ptr->tval == TV_HARD_ARMOR ||
761                               o_ptr->tval == TV_SOFT_ARMOR))
762                                 continue;
763                 }
764                 else if (IS_FLG(FLG_CLOAKS))
765                 {
766                         if (!(o_ptr->tval == TV_CLOAK))
767                                 continue;
768                 }
769                 else if (IS_FLG(FLG_HELMS))
770                 {
771                         if (!(o_ptr->tval == TV_CROWN || o_ptr->tval == TV_HELM))
772                                 continue;
773                 }
774                 else if (IS_FLG(FLG_GLOVES))
775                 {
776                         if (!(o_ptr->tval == TV_GLOVES))
777                                 continue;
778                 }
779                 else if (IS_FLG(FLG_BOOTS))
780                 {
781                         if (!(o_ptr->tval == TV_BOOTS))
782                                 continue;
783                 }
784
785
786                 if (*ptr == '^')
787                 {
788                         ptr++;
789                         if (!strncmp(o_name, ptr, strlen(ptr)))
790                                 flag = TRUE;
791                 }
792                 else
793 #ifdef JP
794                         if (strstr_j(o_name, ptr))
795 #else
796                         if (strstr(o_name, ptr))
797 #endif
798                 {
799                         flag = TRUE;
800                 }
801
802                 if (flag)
803                 {
804                         int j;
805                         if (!IS_FLG(FLG_COLLECTING))
806                                 return i;
807                         /* Check if there is a same item */
808                         for (j = 0; j < INVEN_PACK; j++)
809                         {
810                                 /*
811                                  * 'Collecting' means the item must be absorbed 
812                                  * into an inventory slot.
813                                  * But an item can not be absorbed into itself!
814                                  */
815                                 if ((&inventory[j] != o_ptr) &&
816                                     object_similar(&inventory[j], o_ptr))
817                                         return i;
818                         }
819                 }
820         }/* for */
821
822         return -1;
823 }
824
825
826 /*
827  * Automatically destroy items in this grid.
828  */
829 static bool is_opt_confirm_destroy(object_type *o_ptr)
830 {
831         if (!destroy_items) return FALSE;
832
833         /* Known to be worthless? */
834         if (leave_worth)
835                 if (object_value(o_ptr) > 0) return FALSE;
836         
837         if (leave_equip)
838                 if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_DRAG_ARMOR)) return FALSE;
839         
840         if (leave_chest)
841                 if ((o_ptr->tval == TV_CHEST) && o_ptr->pval) return FALSE;
842         
843         if (leave_wanted)
844         {
845                 if (o_ptr->tval == TV_CORPSE
846                     && object_is_shoukinkubi(o_ptr)) return FALSE;
847         }
848         
849         if (leave_corpse)
850                 if (o_ptr->tval == TV_CORPSE) return FALSE;
851         
852         if (leave_junk)
853                 if ((o_ptr->tval == TV_SKELETON) || (o_ptr->tval == TV_BOTTLE) || (o_ptr->tval == TV_JUNK) || (o_ptr->tval == TV_STATUE)) return FALSE;
854         
855         if (o_ptr->tval == TV_GOLD) return FALSE;
856         
857         return TRUE;
858 }
859
860
861 /*
862  *  Auto inscription
863  */
864 void auto_inscribe_item(int item, int idx)
865 {
866         object_type *o_ptr;
867
868         /* Get the item (in the pack) */
869         if (item >= 0) o_ptr = &inventory[item];
870
871         /* Get the item (on the floor) */
872         else o_ptr = &o_list[0 - item];
873
874         /* Auto-inscription or Re-inscribe for resistances {%} */
875         if ((idx < 0 || !autopick_list[idx].insc) && !o_ptr->inscription)
876                 return;
877
878         if (o_ptr->inscription)
879                 o_ptr->inscription = inscribe_flags(o_ptr, quark_str(o_ptr->inscription));
880         else
881                 o_ptr->inscription = inscribe_flags(o_ptr, autopick_list[idx].insc);
882
883         if (item > INVEN_PACK)
884         {
885                 /* Redraw inscription */
886                 p_ptr->window |= (PW_EQUIP);
887
888                 /* {.} and {$} effect p_ptr->warning and TRC_TELEPORT_SELF */
889                 p_ptr->update |= (PU_BONUS);
890         }
891         else if (item >= 0)
892         {
893                 /* Redraw inscription */
894                 p_ptr->window |= (PW_INVEN);
895         }
896 }
897
898
899 /*
900  * Automatically destroy an item if it is to be destroyed
901  *
902  */
903 bool auto_destroy_item(int item, int autopick_idx)
904 {
905         bool destroy = FALSE;
906         char o_name[MAX_NLEN];
907         object_type *o_ptr;
908
909         /* Don't destroy equipped items */
910         if (item > INVEN_PACK) return FALSE;
911
912         /* Get the item (in the pack) */
913         if (item >= 0) o_ptr = &inventory[item];
914
915         /* Get the item (on the floor) */
916         else o_ptr = &o_list[0 - item];
917
918         /* Easy-Auto-Destroyer */
919         if (is_opt_confirm_destroy(o_ptr)) destroy = TRUE;
920
921         if (always_pickup)
922         {
923                 /* Protected by auto-picker */
924                 if (autopick_idx >= 0 &&
925                     !(autopick_list[autopick_idx].action & DO_AUTODESTROY))
926                         destroy = FALSE;
927         }
928         else
929         {
930                 /* Auto-picker/destroyer */
931                 if (autopick_idx >= 0 &&
932                     (autopick_list[autopick_idx].action & DO_AUTODESTROY))
933                         destroy = TRUE;
934         }
935
936         /* Not to be destroyed */
937         if (!destroy) return FALSE;
938
939         /* Now decided to destroy */
940
941         disturb(0,0);
942
943         /* Describe the object (with {terrible/special}) */
944         object_desc(o_name, o_ptr, TRUE, 3);
945
946         /* Artifact? */
947         if (!can_player_destroy_object(o_ptr))
948         {
949                 /* Message */
950 #ifdef JP
951                 msg_format("%s¤ÏÇ˲õÉÔǽ¤À¡£", o_name);
952 #else
953                 msg_format("You cannot auto-destroy %s.", o_name);
954 #endif
955
956                 /* Done */
957                 return TRUE;
958         }
959
960         /* Record name of destroyed item */
961         autopick_free_entry(&autopick_entry_last_destroyed);
962         autopick_entry_from_object(&autopick_entry_last_destroyed, o_ptr);
963
964         /* Eliminate the item (from the pack) */
965         if (item >= 0)
966         {
967                 inven_item_increase(item, -(o_ptr->number));
968                 inven_item_optimize(item);
969         }
970
971         /* Eliminate the item (from the floor) */
972         else
973         {
974                 delete_object_idx(0 - item);
975         }
976
977         /* Print a message */
978 #ifdef JP
979         msg_format("%s¤ò¼«Æ°Ç˲õ¤·¤Þ¤¹¡£", o_name);
980 #else
981         msg_format("Auto-destroying %s.", o_name);
982 #endif
983                         
984         return TRUE;
985 }
986
987
988 /*
989  * Automatically pickup/destroy items in this grid.
990  */
991 void auto_pickup_items(cave_type *c_ptr)
992 {
993         s16b this_o_idx, next_o_idx = 0;
994         
995         /* Scan the pile of objects */
996         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
997         {
998                 int idx;
999         
1000                 /* Acquire object */
1001                 object_type *o_ptr = &o_list[this_o_idx];
1002                 
1003                 /* Acquire next object */
1004                 next_o_idx = o_ptr->next_o_idx;
1005
1006                 idx = is_autopick(o_ptr);
1007
1008                 /* Item index for floor -1,-2,-3,...  */
1009                 auto_inscribe_item((-this_o_idx), idx);
1010
1011                 if (idx >= 0 && (autopick_list[idx].action & DO_AUTOPICK))
1012                 {
1013                         disturb(0,0);
1014
1015                         if (!inven_carry_okay(o_ptr))
1016                         {
1017                                 char o_name[MAX_NLEN];
1018
1019                                 /* Describe the object */
1020                                 object_desc(o_name, o_ptr, TRUE, 3);
1021
1022                                 /* Message */
1023 #ifdef JP
1024                                 msg_format("¥¶¥Ã¥¯¤Ë¤Ï%s¤òÆþ¤ì¤ë·ä´Ö¤¬¤Ê¤¤¡£", o_name);
1025 #else
1026                                 msg_format("You have no room for %s.", o_name);
1027 #endif
1028                                 continue;
1029                         }
1030                         py_pickup_aux(this_o_idx);
1031
1032                         continue;
1033                 }
1034                 
1035                 /*
1036                  * Do auto-destroy;
1037                  * When always_pickup is 'yes', we disable
1038                  * auto-destroyer from autopick function, and do only
1039                  * easy-auto-destroyer.
1040                  */
1041                 else
1042                 {
1043                         if (auto_destroy_item((-this_o_idx), idx))
1044                                 continue;
1045                 }
1046         }
1047 }
1048
1049
1050 /*
1051  * Describe which kind of object is Auto-picked/destroyed
1052  */
1053 static void describe_autopick(char *buff, autopick_type *entry)
1054 {
1055         cptr str = entry->name;
1056         byte act = entry->action;
1057         cptr insc = entry->insc;
1058         int i;
1059
1060         bool top = FALSE;
1061
1062 #ifdef JP
1063         cptr before_str[20], body_str;
1064         int before_n = 0;
1065
1066         body_str = "¥¢¥¤¥Æ¥à";
1067
1068         /*** Collecting items ***/
1069         /*** Which can be absorbed into a slot as a bundle ***/
1070         if (IS_FLG(FLG_COLLECTING))
1071                 before_str[before_n++] = "¼ý½¸Ãæ¤Ç´û¤Ë»ý¤Ã¤Æ¤¤¤ë¥¹¥í¥Ã¥È¤Ë¤Þ¤È¤á¤é¤ì¤ë";
1072         
1073         /*** Unidentified ***/
1074         if (IS_FLG(FLG_UNIDENTIFIED))
1075                 before_str[before_n++] = "̤´ÕÄê¤Î";
1076
1077         /*** Identified ***/
1078         if (IS_FLG(FLG_IDENTIFIED))
1079                 before_str[before_n++] = "´ÕÄêºÑ¤ß¤Î";
1080
1081         /*** *Identified* ***/
1082         if (IS_FLG(FLG_STAR_IDENTIFIED))
1083                 before_str[before_n++] = "´°Á´¤Ë´ÕÄêºÑ¤ß¤Î";
1084
1085         /*** Dice boosted (weapon of slaying) ***/
1086         if (IS_FLG(FLG_BOOSTED))
1087         {
1088                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤¬Ä̾ï¤è¤êÂ礭¤¤";
1089                 body_str = "Éð´ï";
1090         }
1091
1092         /*** Weapons whose dd*ds is more than nn ***/
1093         if (IS_FLG(FLG_MORE_THAN))
1094         {
1095                 static char more_than_desc_str[] = "___";
1096                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤ÎºÇÂçÃͤ¬";
1097                 body_str = "Éð´ï";
1098                         
1099                 sprintf(more_than_desc_str,"%d", entry->dice);
1100                 before_str[before_n++] = more_than_desc_str;
1101                 before_str[before_n++] = "°Ê¾å¤Î";
1102         }
1103
1104         /*** Items whose magical bonus is more than nn ***/
1105         if (IS_FLG(FLG_MORE_BONUS))
1106         {
1107                 static char more_bonus_desc_str[] = "___";
1108                 before_str[before_n++] = "½¤ÀµÃͤ¬(+";
1109                         
1110                 sprintf(more_bonus_desc_str,"%d", entry->bonus);
1111                 before_str[before_n++] = more_bonus_desc_str;
1112                 before_str[before_n++] = ")°Ê¾å¤Î";
1113         }
1114
1115         /*** Worthless items ***/
1116         if (IS_FLG(FLG_WORTHLESS))
1117                 before_str[before_n++] = "Ź¤Ç̵²ÁÃͤÈȽÄꤵ¤ì¤ë";
1118
1119         /*** Artifact ***/
1120         if (IS_FLG(FLG_ARTIFACT))
1121         {
1122                 before_str[before_n++] = "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Î";
1123                 body_str = "ÁõÈ÷";
1124         }
1125
1126         /*** Ego ***/
1127         if (IS_FLG(FLG_EGO))
1128         {
1129                 before_str[before_n++] = "¥¨¥´¥¢¥¤¥Æ¥à¤Î";
1130                 body_str = "ÁõÈ÷";
1131         }
1132
1133         /*** Nameless ***/
1134         if (IS_FLG(FLG_NAMELESS))
1135         {
1136                 before_str[before_n++] = "¥¨¥´¤Ç¤â¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Ç¤â¤Ê¤¤";
1137                 body_str = "ÁõÈ÷";
1138         }
1139
1140         /*** Unaware items ***/
1141         if (IS_FLG(FLG_UNAWARE))
1142                 before_str[before_n++] = "̤´ÕÄê¤Ç¤½¤Î¸ú²Ì¤âȽÌÀ¤·¤Æ¤¤¤Ê¤¤";
1143
1144         /*** Wanted monster's corpse/skeletons ***/
1145         if (IS_FLG(FLG_WANTED))
1146         {
1147                 before_str[before_n++] = "¥Ï¥ó¥¿¡¼»ö̳½ê¤Ç¾Þ¶â¼ó¤È¤µ¤ì¤Æ¤¤¤ë";
1148                 body_str = "»àÂΤä¹ü";
1149         }
1150
1151         /*** Human corpse/skeletons (for Daemon magic) ***/
1152         if (IS_FLG(FLG_HUMAN))
1153         {
1154                 before_str[before_n++] = "°­ËâËâË¡¤Ç»È¤¦¤¿¤á¤Î¿Í´Ö¤ä¥Ò¥å¡¼¥Þ¥Î¥¤¥É¤Î";
1155                 body_str = "»àÂΤä¹ü";
1156         }
1157
1158         /*** Unique monster's corpse/skeletons/statues ***/
1159         if (IS_FLG(FLG_UNIQUE))
1160         {
1161                 before_str[before_n++] = "¥æ¥Ë¡¼¥¯¥â¥ó¥¹¥¿¡¼¤Î";
1162                 body_str = "»àÂΤä¹ü";
1163         }
1164
1165         /*** Unreadable spellbooks ***/
1166         if (IS_FLG(FLG_UNREADABLE))
1167         {
1168                 before_str[before_n++] = "¤¢¤Ê¤¿¤¬Æɤá¤Ê¤¤Îΰè¤Î";
1169                 body_str = "ËâË¡½ñ";
1170         }
1171
1172         /*** First realm spellbooks ***/
1173         if (IS_FLG(FLG_REALM1))
1174         {
1175                 before_str[before_n++] = "Âè°ìÎΰè¤Î";
1176                 body_str = "ËâË¡½ñ";
1177         }
1178
1179         /*** Second realm spellbooks ***/
1180         if (IS_FLG(FLG_REALM2))
1181         {
1182                 before_str[before_n++] = "ÂèÆóÎΰè¤Î";
1183                 body_str = "ËâË¡½ñ";
1184         }
1185
1186         /*** First rank spellbooks ***/
1187         if (IS_FLG(FLG_FIRST))
1188         {
1189                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î1ºýÌܤÎ";
1190                 body_str = "ËâË¡½ñ";
1191         }
1192
1193         /*** Second rank spellbooks ***/
1194         if (IS_FLG(FLG_SECOND))
1195         {
1196                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î2ºýÌܤÎ";
1197                 body_str = "ËâË¡½ñ";
1198         }
1199
1200         /*** Third rank spellbooks ***/
1201         if (IS_FLG(FLG_THIRD))
1202         {
1203                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î3ºýÌܤÎ";
1204                 body_str = "ËâË¡½ñ";
1205         }
1206
1207         /*** Fourth rank spellbooks ***/
1208         if (IS_FLG(FLG_FOURTH))
1209         {
1210                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î4ºýÌܤÎ";
1211                 body_str = "ËâË¡½ñ";
1212         }
1213
1214         /*** Items ***/
1215         if (IS_FLG(FLG_ITEMS))
1216                 ; /* Nothing to do */
1217         else if (IS_FLG(FLG_WEAPONS))
1218                 body_str = "Éð´ï";
1219         else if (IS_FLG(FLG_ARMORS))
1220                 body_str = "Ëɶñ";
1221         else if (IS_FLG(FLG_MISSILES))
1222                 body_str = "ÃƤäÌð¤ä¥¯¥í¥¹¥Ü¥¦¤ÎÌð";
1223         else if (IS_FLG(FLG_DEVICES))
1224                 body_str = "´¬Êª¤äËâË¡ËÀ¤ä¾ó¤ä¥í¥Ã¥É";
1225         else if (IS_FLG(FLG_LIGHTS))
1226                 body_str = "¸÷¸»ÍѤΥ¢¥¤¥Æ¥à";
1227         else if (IS_FLG(FLG_JUNKS))
1228                 body_str = "Àޤ줿ËÀÅù¤Î¥¬¥é¥¯¥¿";
1229         else if (IS_FLG(FLG_SPELLBOOKS))
1230                 body_str = "ËâË¡½ñ";
1231         else if (IS_FLG(FLG_HAFTED))
1232                 body_str = "Æß´ï";
1233         else if (IS_FLG(FLG_SHIELDS))
1234                 body_str = "½â";
1235         else if (IS_FLG(FLG_BOWS))
1236                 body_str = "¥¹¥ê¥ó¥°¤äµÝ¤ä¥¯¥í¥¹¥Ü¥¦";
1237         else if (IS_FLG(FLG_RINGS))
1238                 body_str = "»ØÎØ";
1239         else if (IS_FLG(FLG_AMULETS))
1240                 body_str = "¥¢¥ß¥å¥ì¥Ã¥È";
1241         else if (IS_FLG(FLG_SUITS))
1242                 body_str = "³»";
1243         else if (IS_FLG(FLG_CLOAKS))
1244                 body_str = "¥¯¥í¡¼¥¯";
1245         else if (IS_FLG(FLG_HELMS))
1246                 body_str = "¥Ø¥ë¥á¥Ã¥È¤ä´§";
1247         else if (IS_FLG(FLG_GLOVES))
1248                 body_str = "¾®¼ê";
1249         else if (IS_FLG(FLG_BOOTS))
1250                 body_str = "¥Ö¡¼¥Ä";
1251
1252         *buff = '\0';
1253         if (!before_n) 
1254                 strcat(buff, "Á´¤Æ¤Î");
1255         else for (i = 0; i < before_n && before_str[i]; i++)
1256                 strcat(buff, before_str[i]);
1257
1258         strcat(buff, body_str);
1259
1260         if (*str)
1261         {
1262                 if (*str == '^')
1263                 {
1264                         str++;
1265                         top = TRUE;
1266                 }
1267
1268                 strcat(buff, "¤Ç¡¢Ì¾Á°¤¬¡Ö");
1269                 strncat(buff, str, 80);
1270                 if (top)
1271                         strcat(buff, "¡×¤Ç»Ï¤Þ¤ë¤â¤Î");
1272                 else
1273                         strcat(buff, "¡×¤ò´Þ¤à¤â¤Î");
1274         }
1275
1276         if (insc)
1277                 strncat(buff, format("¤Ë¡Ö%s¡×¤È¹ï¤ó¤Ç", insc), 80);
1278         else
1279                 strcat(buff, "¤ò");
1280
1281         if (act & DONT_AUTOPICK)
1282                 strcat(buff, "ÊüÃÖ¤¹¤ë¡£");
1283         else if (act & DO_AUTODESTROY)
1284                 strcat(buff, "Ç˲õ¤¹¤ë¡£");
1285         else
1286                 strcat(buff, "½¦¤¦¡£");
1287
1288         if (act & DO_DISPLAY)
1289         {
1290                 if (act & DONT_AUTOPICK)
1291                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'N'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1292                 else if (act & DO_AUTODESTROY)
1293                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'K'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1294                 else
1295                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'M'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1296         }
1297         else
1298                 strcat(buff, "Á´ÂΥޥåפˤÏɽ¼¨¤·¤Ê¤¤");
1299
1300 #else /* JP */
1301
1302         cptr before_str[20], after_str[20], which_str[20], whose_str[20], body_str;
1303         int before_n = 0, after_n = 0, which_n = 0, whose_n = 0;
1304
1305         body_str = "items";
1306
1307         /*** Collecting items ***/
1308         /*** Which can be absorbed into a slot as a bundle ***/
1309         if (IS_FLG(FLG_COLLECTING))
1310                 which_str[which_n++] = "can be absorbed into an existing inventory slot";
1311         
1312         /*** Unidentified ***/
1313         if (IS_FLG(FLG_UNIDENTIFIED))
1314                 before_str[before_n++] = "unidentified";
1315
1316         /*** Identified ***/
1317         if (IS_FLG(FLG_IDENTIFIED))
1318                 before_str[before_n++] = "identified";
1319
1320         /*** *Identified* ***/
1321         if (IS_FLG(FLG_STAR_IDENTIFIED))
1322                 before_str[before_n++] = "fully identified";
1323
1324         /*** Worthless items ***/
1325         if (IS_FLG(FLG_WORTHLESS))
1326         {
1327                 before_str[before_n++] = "worthless";
1328                 which_str[which_n++] = "can not be sold at stores";
1329         }
1330
1331         /*** Artifacto ***/
1332         if (IS_FLG(FLG_ARTIFACT))
1333         {
1334                 before_str[before_n++] = "artifact";
1335         }
1336
1337         /*** Ego ***/
1338         if (IS_FLG(FLG_EGO))
1339         {
1340                 before_str[before_n++] = "ego";
1341         }
1342
1343         /*** Nameless ***/
1344         if (IS_FLG(FLG_NAMELESS))
1345         {
1346                 body_str = "equipment";
1347                 which_str[which_n++] = "is neither ego-item nor artifact";
1348         }
1349
1350         /*** Unaware items ***/
1351         if (IS_FLG(FLG_UNAWARE))
1352         {
1353                 before_str[before_n++] = "unidentified";
1354                 whose_str[whose_n++] = "basic abilities are not known";
1355         }
1356
1357         /*** Dice boosted (weapon of slaying) ***/
1358         if (IS_FLG(FLG_BOOSTED))
1359         {
1360                 body_str = "weapons";
1361                 whose_str[whose_n++] = "damage dice is bigger than normal";
1362         }
1363
1364         /*** Weapons whose dd*ds is more than nn ***/
1365         if (IS_FLG(FLG_MORE_THAN))
1366         {
1367                 static char more_than_desc_str[] =
1368                         "maximum damage from dice is bigger than __";
1369                 body_str = "weapons";
1370                         
1371                 sprintf(more_than_desc_str + sizeof(more_than_desc_str) - 3,
1372                         "%d", entry->dice);
1373                 whose_str[whose_n++] = more_than_desc_str;
1374         }
1375
1376         /*** Items whose magical bonus is more than nn ***/
1377         if (IS_FLG(FLG_MORE_BONUS))
1378         {
1379                 static char more_bonus_desc_str[] =
1380                         "magical bonus is bigger than (+__)";
1381                         
1382                 sprintf(more_bonus_desc_str + sizeof(more_bonus_desc_str) - 4,
1383                         "%d)", entry->bonus);
1384                 whose_str[whose_n++] = more_bonus_desc_str;
1385         }
1386
1387         /*** Wanted monster's corpse/skeletons ***/
1388         if (IS_FLG(FLG_WANTED))
1389         {
1390                 body_str = "corpse or skeletons";
1391                 which_str[which_n++] = "is wanted at the Hunter's Office";
1392         }
1393
1394         /*** Human corpse/skeletons (for Daemon magic) ***/
1395         if (IS_FLG(FLG_HUMAN))
1396         {
1397                 before_str[before_n++] = "humanoid";
1398                 body_str = "corpse or skeletons";
1399                 which_str[which_n++] = "can be used for Daemon magic";
1400         }
1401
1402         /*** Unique monster's corpse/skeletons/statues ***/
1403         if (IS_FLG(FLG_UNIQUE))
1404         {
1405                 before_str[before_n++] = "unique monster's";
1406                 body_str = "corpse or skeletons";
1407         }
1408
1409         /*** Unreadable spellbooks ***/
1410         if (IS_FLG(FLG_UNREADABLE))
1411         {
1412                 body_str = "spellbooks";
1413                 after_str[after_n++] = "of different realms from yours";
1414         }
1415
1416         /*** First realm spellbooks ***/
1417         if (IS_FLG(FLG_REALM1))
1418         {
1419                 body_str = "spellbooks";
1420                 after_str[after_n++] = "of your first realm";
1421         }
1422
1423         /*** Second realm spellbooks ***/
1424         if (IS_FLG(FLG_REALM2))
1425         {
1426                 body_str = "spellbooks";
1427                 after_str[after_n++] = "of your second realm";
1428         }
1429
1430         /*** First rank spellbooks ***/
1431         if (IS_FLG(FLG_FIRST))
1432         {
1433                 before_str[before_n++] = "first one of four";
1434                 body_str = "spellbooks";
1435         }
1436
1437         /*** Second rank spellbooks ***/
1438         if (IS_FLG(FLG_SECOND))
1439         {
1440                 before_str[before_n++] = "second one of four";
1441                 body_str = "spellbooks";
1442         }
1443
1444         /*** Third rank spellbooks ***/
1445         if (IS_FLG(FLG_THIRD))
1446         {
1447                 before_str[before_n++] = "third one of four";
1448                 body_str = "spellbooks";
1449         }
1450
1451         /*** Fourth rank spellbooks ***/
1452         if (IS_FLG(FLG_FOURTH))
1453         {
1454                 before_str[before_n++] = "fourth one of four";
1455                 body_str = "spellbooks";
1456         }
1457
1458         /*** Items ***/
1459         if (IS_FLG(FLG_ITEMS))
1460                 ; /* Nothing to do */
1461         else if (IS_FLG(FLG_WEAPONS))
1462                 body_str = "weapons";
1463         else if (IS_FLG(FLG_ARMORS))
1464                 body_str = "armors";
1465         else if (IS_FLG(FLG_MISSILES))
1466                 body_str = "shots, arrows or crossbow bolts";
1467         else if (IS_FLG(FLG_DEVICES))
1468                 body_str = "scrolls, wands, staves or rods";
1469         else if (IS_FLG(FLG_LIGHTS))
1470                 body_str = "light sources";
1471         else if (IS_FLG(FLG_JUNKS))
1472                 body_str = "junk such as broken sticks";
1473         else if (IS_FLG(FLG_SPELLBOOKS))
1474                 body_str = "spellbooks";
1475         else if (IS_FLG(FLG_HAFTED))
1476                 body_str = "hafted weapons";
1477         else if (IS_FLG(FLG_SHIELDS))
1478                 body_str = "shields";
1479         else if (IS_FLG(FLG_BOWS))
1480                 body_str = "slings, bows or crossbows";
1481         else if (IS_FLG(FLG_RINGS))
1482                 body_str = "rings";
1483         else if (IS_FLG(FLG_AMULETS))
1484                 body_str = "amulets";
1485         else if (IS_FLG(FLG_SUITS))
1486                 body_str = "body armors";
1487         else if (IS_FLG(FLG_CLOAKS))
1488                 body_str = "cloaks";
1489         else if (IS_FLG(FLG_HELMS))
1490                 body_str = "helms or crowns";
1491         else if (IS_FLG(FLG_GLOVES))
1492                 body_str = "gloves";
1493         else if (IS_FLG(FLG_BOOTS))
1494                 body_str = "boots";
1495
1496         /* Prepare a string for item name */
1497         if (*str)
1498         {
1499                 if (*str == '^')
1500                 {
1501                         str++;
1502                         top = TRUE;
1503                         whose_str[whose_n++] = "name is beginning with \"";
1504                 }
1505                 else
1506                         which_str[which_n++] = "have \"";
1507         }
1508
1509
1510         /* Describe action flag */
1511         if (act & DONT_AUTOPICK)
1512                 strcpy(buff, "Leave on floor ");
1513         else if (act & DO_AUTODESTROY)
1514                 strcpy(buff, "Destroy ");
1515         else
1516                 strcpy(buff, "Pickup ");
1517
1518         /* Auto-insctiption */
1519         if (insc)
1520                 strncat(buff, format("and inscribe \"%s\" on ", insc), 80);
1521
1522         /* Adjective */
1523         if (!before_n) 
1524                 strcat(buff, "all ");
1525         else for (i = 0; i < before_n && before_str[i]; i++)
1526         {
1527                 strcat(buff, before_str[i]);
1528                 strcat(buff, " ");
1529         }
1530
1531         /* Item class */
1532         strcat(buff, body_str);
1533
1534         /* of ... */
1535         for (i = 0; i < after_n && after_str[i]; i++)
1536         {
1537                 strcat(buff, " ");
1538                 strcat(buff, after_str[i]);
1539         }
1540
1541         /* which ... */
1542         for (i = 0; i < whose_n && whose_str[i]; i++)
1543         {
1544                 if (i == 0)
1545                         strcat(buff, " whose ");
1546                 else
1547                         strcat(buff, ", and ");
1548
1549                 strcat(buff, whose_str[i]);
1550         }
1551
1552         /* Item name ; whose name is beginning with "str" */
1553         if (*str && top)
1554         {
1555                 strcat(buff, str);
1556                 strcat(buff, "\"");
1557         }
1558
1559         /* whose ..., and which .... */
1560         if (whose_n && which_n)
1561                 strcat(buff, ", and ");
1562
1563         /* which ... */
1564         for (i = 0; i < which_n && which_str[i]; i++)
1565         {
1566                 if (i == 0)
1567                         strcat(buff, " which ");
1568                 else
1569                         strcat(buff, ", and ");
1570
1571                 strcat(buff, which_str[i]);
1572         }
1573
1574         /* Item name ; which have "str" as part of its name */
1575         if (*str && !top)
1576         {
1577                 strncat(buff, str, 80);
1578                 strcat(buff, "\" as part of its name");
1579         }
1580         strcat(buff, ".");
1581
1582         /* Describe whether it will be displayed on the full map or not */
1583         if (act & DO_DISPLAY)
1584         {
1585                 if (act & DONT_AUTOPICK)
1586                         strcat(buff, "  Display these items when you press 'N' in the full map('M').");
1587                 else if (act & DO_AUTODESTROY)
1588                         strcat(buff, "  Display these items when you press 'K' in the full map('M').");
1589                 else
1590                         strcat(buff, "  Display these items when you press 'M' in the full map('M').");
1591         }
1592         else
1593                 strcat(buff, " Not displayed in the full map.");
1594 #endif /* JP */
1595
1596 }
1597
1598
1599 #define MAX_LINES 3000
1600
1601 /*
1602  * Read whole lines of a file to memory
1603  */
1604 static cptr *read_text_lines(cptr filename, bool user)
1605 {
1606         cptr *lines_list = NULL;
1607         FILE *fff;
1608
1609         int lines = 0;
1610         char buf[1024];
1611
1612         if (user)
1613         {
1614                 /* Hack -- drop permissions */
1615                 safe_setuid_drop();
1616                 path_build(buf, 1024, ANGBAND_DIR_USER, filename);
1617         }
1618         else
1619         {
1620                 path_build(buf, 1024, ANGBAND_DIR_PREF, filename);
1621         }
1622         
1623         /* Open the file */
1624         fff = my_fopen(buf, "r");
1625
1626         if (fff)
1627         {
1628                 /* Allocate list of pointers */
1629                 C_MAKE(lines_list, MAX_LINES, cptr);
1630
1631                 /* Parse it */
1632                 while (0 == my_fgets(fff, buf, 1024))
1633                 {
1634                         lines_list[lines++] = string_make(buf);
1635                         if (lines >= MAX_LINES - 1) break;
1636                 }
1637                 if (lines == 0)
1638                         lines_list[0] = string_make("");
1639
1640                 my_fclose(fff);
1641         }
1642
1643         /* Grab priv's */
1644         safe_setuid_grab();
1645
1646         if (!fff) return NULL;
1647         return lines_list;
1648 }
1649
1650 static cptr *read_pickpref_text_lines(void)
1651 {
1652         char buf[1024];
1653         cptr *lines_list;
1654
1655 #ifdef JP
1656         sprintf(buf, "picktype-%s.prf", player_name);
1657 #else
1658         sprintf(buf, "pickpref-%s.prf", player_name);
1659 #endif
1660         lines_list = read_text_lines(buf, TRUE);
1661
1662         if (!lines_list)
1663         {
1664 #ifdef JP
1665                 lines_list = read_text_lines("picktype.prf", TRUE);
1666 #else
1667                 lines_list = read_text_lines("pickpref.prf", TRUE);
1668 #endif
1669         }
1670
1671         if (!lines_list)
1672         {
1673 #ifdef JP
1674                 lines_list = read_text_lines("picktype.prf", FALSE);
1675 #else
1676                 lines_list = read_text_lines("pickpref.prf", FALSE);
1677 #endif
1678         }
1679
1680         if (!lines_list)
1681         {
1682                 /* Allocate list of pointers */
1683                 C_MAKE(lines_list, MAX_LINES, cptr);
1684                 lines_list[0] = string_make("");
1685         }
1686         return lines_list;
1687 }
1688
1689 /*
1690  * Write whole lines of memory to a file.
1691  */
1692 static bool write_text_lines(cptr filename, cptr *lines_list)
1693 {
1694         FILE *fff;
1695
1696         int lines = 0;
1697         char buf[1024];
1698
1699         /* Hack -- drop permissions */
1700         safe_setuid_drop();
1701
1702         /* Build the filename */
1703         path_build(buf, 1024, ANGBAND_DIR_USER, filename);
1704         
1705         /* Open the file */
1706         fff = my_fopen(buf, "w");
1707         if (fff)
1708         {
1709                 for (lines = 0; lines_list[lines]; lines++)
1710                         my_fputs(fff, lines_list[lines], 1024);
1711
1712                 my_fclose(fff);
1713         }
1714
1715         /* Grab priv's */
1716         safe_setuid_grab();
1717
1718         if (!fff) return FALSE;
1719         return TRUE;
1720 }
1721
1722
1723 /*
1724  * Free memory of lines_list.
1725  */
1726 static void free_text_lines(cptr *lines_list)
1727 {
1728         int lines;
1729
1730         for (lines = 0; lines_list[lines]; lines++)
1731                 string_free(lines_list[lines]);
1732
1733         /* free list of pointers */
1734         C_FREE((char **)lines_list, MAX_LINES, char *);
1735 }
1736
1737
1738 /*
1739  * Insert string
1740  */
1741 static void insert_string(cptr *lines_list, cptr str, int x, int y)
1742 {
1743         char buf[1024];
1744         int i, j;
1745
1746         for (i = j = 0; lines_list[y][i] && i < x; i++)
1747                 buf[j++] = lines_list[y][i];
1748
1749         while (*str) buf[j++] = *str++;
1750
1751         for (; lines_list[y][i]; i++)
1752                 buf[j++] = lines_list[y][i];
1753         buf[j] = '\0';
1754         string_free(lines_list[y]);
1755         lines_list[y] = string_make(buf);
1756 }
1757
1758 /*
1759  * Delete n letters
1760  */
1761 static void delete_string(cptr *lines_list, int n, int x, int y)
1762 {
1763         int i, j;
1764         char buf[1024];
1765
1766         for (i = j = 0; lines_list[y][i] && i < x; i++)
1767         {
1768 #ifdef JP
1769                 if (iskanji(lines_list[y][i]))
1770                         buf[j++] = lines_list[y][i++];
1771 #endif
1772                 buf[j++] = lines_list[y][i];
1773         }
1774         i += n;
1775
1776         for (; lines_list[y][i]; i++)
1777                 buf[j++] = lines_list[y][i];
1778         buf[j] = '\0';
1779         string_free(lines_list[y]);
1780         lines_list[y] = string_make(buf);
1781 }
1782
1783
1784 /*
1785  * Delete or insert string
1786  */
1787 static void toggle_string(cptr *lines_list, int flg, int y)
1788 {
1789         autopick_type an_entry, *entry = &an_entry;
1790
1791         if (!autopick_new_entry(entry, lines_list[y]))
1792                 return;
1793
1794         string_free(lines_list[y]);
1795         if (IS_FLG(flg)) 
1796                 REM_FLG(flg);
1797         else
1798                 ADD_FLG(flg);
1799
1800         lines_list[y] = autopick_line_from_entry(entry);
1801 }
1802
1803 /*
1804  * Insert return code and split the line
1805  */
1806 static bool insert_return_code(cptr *lines_list, int cx, int cy)
1807 {
1808         char buf[1024];
1809         int i, j, k;
1810
1811         for (k = 0; lines_list[k]; k++)
1812                 /* count number of lines */ ;
1813
1814         if (k >= MAX_LINES - 2) return FALSE;
1815         k--;
1816
1817         /* Move down lines */
1818         for (; cy < k; k--)
1819                 lines_list[k+1] = lines_list[k];
1820
1821         /* Split current line */
1822         for (i = j = 0; lines_list[cy][i] && i < cx; i++)
1823         {
1824 #ifdef JP
1825                 if (iskanji(lines_list[cy][i]))
1826                         buf[j++] = lines_list[cy][i++];
1827 #endif
1828                 buf[j++] = lines_list[cy][i];
1829         }
1830         buf[j] = '\0';
1831         lines_list[cy+1] = string_make(&lines_list[cy][i]);
1832         string_free(lines_list[cy]);
1833         lines_list[cy] = string_make(buf);
1834         return TRUE;
1835 }
1836
1837
1838 /*
1839  * Get auto-picker entry from o_ptr.
1840  */
1841 void autopick_entry_from_object(autopick_type *entry, object_type *o_ptr)
1842 {
1843         char o_name[MAX_NLEN];
1844         object_desc(o_name, o_ptr, FALSE, 0);
1845
1846         entry->name = string_make(o_name);
1847         entry->insc = string_make(quark_str(o_ptr->inscription));
1848         entry->action = DO_AUTOPICK | DO_DISPLAY;
1849         entry->flag[0] = entry->flag[1] = 0L;
1850         entry->dice = 0;
1851
1852         if (!object_aware_p(o_ptr))
1853                 ADD_FLG(FLG_UNAWARE);
1854         if (object_value(o_ptr) <= 0)
1855                 ADD_FLG(FLG_WORTHLESS);
1856
1857         if (object_known_p(o_ptr))
1858         {
1859                 if (o_ptr->name2)
1860                         ADD_FLG(FLG_EGO);
1861                 else if (o_ptr->name1 || o_ptr->art_name)
1862                         ADD_FLG(FLG_ARTIFACT);
1863         }
1864
1865         switch(o_ptr->tval)
1866         {
1867                 object_kind *k_ptr; 
1868         case TV_HAFTED: case TV_POLEARM: case TV_SWORD: case TV_DIGGING:
1869                 k_ptr = &k_info[o_ptr->k_idx];
1870                 if ((o_ptr->dd != k_ptr->dd) || (o_ptr->ds != k_ptr->ds))
1871                         ADD_FLG(FLG_BOOSTED);
1872         }
1873
1874         if (o_ptr->tval == TV_CORPSE && object_is_shoukinkubi(o_ptr))
1875         {
1876                 REM_FLG(FLG_WORTHLESS);
1877                 ADD_FLG(FLG_WANTED);
1878         }
1879
1880         if ((o_ptr->tval == TV_CORPSE || o_ptr->tval == TV_STATUE)
1881             && (r_info[o_ptr->pval].flags1 & RF1_UNIQUE))
1882         {
1883                 REM_FLG(FLG_WORTHLESS);
1884                 ADD_FLG(FLG_UNIQUE);
1885         }
1886
1887         if (o_ptr->tval == TV_CORPSE && strchr("pht", r_info[o_ptr->pval].d_char))
1888         {
1889                 REM_FLG(FLG_WORTHLESS);
1890                 ADD_FLG(FLG_HUMAN);
1891         }
1892
1893         if (o_ptr->tval >= TV_LIFE_BOOK &&
1894             !check_book_realm(o_ptr->tval, o_ptr->sval))
1895                 ADD_FLG(FLG_UNREADABLE);
1896
1897         if (REALM1_BOOK == o_ptr->tval &&
1898             p_ptr->pclass != CLASS_SORCERER &&
1899             p_ptr->pclass != CLASS_RED_MAGE)
1900                 ADD_FLG(FLG_REALM1);
1901
1902         if (REALM2_BOOK == o_ptr->tval &&
1903             p_ptr->pclass != CLASS_SORCERER &&
1904             p_ptr->pclass != CLASS_RED_MAGE)
1905                 ADD_FLG(FLG_REALM2);
1906
1907         if (o_ptr->tval >= TV_LIFE_BOOK && 0 == o_ptr->sval)
1908                 ADD_FLG(FLG_FIRST);
1909         if (o_ptr->tval >= TV_LIFE_BOOK && 1 == o_ptr->sval)
1910                 ADD_FLG(FLG_SECOND);
1911         if (o_ptr->tval >= TV_LIFE_BOOK && 2 == o_ptr->sval)
1912                 ADD_FLG(FLG_THIRD);
1913         if (o_ptr->tval >= TV_LIFE_BOOK && 3 == o_ptr->sval)
1914                 ADD_FLG(FLG_FOURTH);
1915
1916         if (o_ptr->tval == TV_SHOT || o_ptr->tval == TV_BOLT
1917                  || o_ptr->tval == TV_ARROW)
1918                 ADD_FLG(FLG_MISSILES);
1919         else if (o_ptr->tval == TV_SCROLL || o_ptr->tval == TV_STAFF
1920                  || o_ptr->tval == TV_WAND || o_ptr->tval == TV_ROD)
1921                 ADD_FLG(FLG_DEVICES);
1922         else if (o_ptr->tval == TV_LITE)
1923                 ADD_FLG(FLG_LIGHTS);
1924         else if (o_ptr->tval == TV_SKELETON || o_ptr->tval == TV_BOTTLE
1925                  || o_ptr->tval == TV_JUNK || o_ptr->tval == TV_STATUE)
1926                 ADD_FLG(FLG_JUNKS);
1927         else if (o_ptr->tval >= TV_LIFE_BOOK)
1928                 ADD_FLG(FLG_SPELLBOOKS);
1929         else if (o_ptr->tval == TV_HAFTED)
1930                 ADD_FLG(FLG_HAFTED);
1931         else if (o_ptr->tval == TV_POLEARM || o_ptr->tval == TV_SWORD
1932                  || o_ptr->tval == TV_DIGGING)
1933                 ADD_FLG(FLG_WEAPONS);
1934         else if (o_ptr->tval == TV_SHIELD)
1935                 ADD_FLG(FLG_SHIELDS);
1936         else if (o_ptr->tval == TV_BOW)
1937                 ADD_FLG(FLG_BOWS);
1938         else if (o_ptr->tval == TV_RING)
1939                 ADD_FLG(FLG_RINGS);
1940         else if (o_ptr->tval == TV_AMULET)
1941                 ADD_FLG(FLG_AMULETS);
1942         else if (o_ptr->tval == TV_DRAG_ARMOR || o_ptr->tval == TV_HARD_ARMOR ||
1943                  o_ptr->tval == TV_SOFT_ARMOR)
1944                 ADD_FLG(FLG_SUITS);
1945         else if (o_ptr->tval == TV_CLOAK)
1946                 ADD_FLG(FLG_CLOAKS);
1947         else if (o_ptr->tval == TV_HELM)
1948                 ADD_FLG(FLG_HELMS);
1949         else if (o_ptr->tval == TV_GLOVES)
1950                 ADD_FLG(FLG_GLOVES);
1951         else if (o_ptr->tval == TV_BOOTS)
1952                 ADD_FLG(FLG_BOOTS);
1953
1954         return;
1955 }
1956
1957 /*
1958  * Choose an item and get auto-picker entry from it.
1959  */
1960 static bool entry_from_choosed_object(autopick_type *entry)
1961 {
1962         int item;
1963         object_type *o_ptr;
1964         cptr q, s;
1965
1966         /* Get an item */
1967 #ifdef JP
1968         q = "¤É¤Î¥¢¥¤¥Æ¥à¤òÅÐÏ¿¤·¤Þ¤¹¤«? ";
1969         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
1970 #else
1971         q = "Entry which item? ";
1972         s = "You have nothing to entry.";
1973 #endif
1974         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR | USE_EQUIP))) return FALSE;
1975
1976         /* Get the item (in the pack) */
1977         if (item >= 0)
1978         {
1979                 o_ptr = &inventory[item];
1980         }
1981
1982         /* Get the item (on the floor) */
1983         else
1984         {
1985                 o_ptr = &o_list[0 - item];
1986         }
1987
1988         autopick_entry_from_object(entry, o_ptr);
1989         return TRUE;
1990 }
1991
1992
1993 /*
1994  * Initialize auto-picker preference
1995  */
1996 void init_autopicker(void)
1997 {
1998         static const char easy_autopick_inscription[] = "(:=g";
1999         autopick_type entry;
2000         int i;
2001
2002         /* Clear old entries */
2003         for( i = 0; i < max_autopick; i++)
2004                 autopick_free_entry(&autopick_list[i]);
2005
2006         max_autopick = 0;
2007
2008         /* There is always one entry "=g" */
2009         autopick_new_entry(&entry, easy_autopick_inscription);
2010         autopick_list[max_autopick++] = entry;
2011 }
2012
2013
2014 /*
2015  * Description of control commands
2016  */
2017
2018 #define WID_DESC 31
2019
2020 static cptr ctrl_command_desc[] =
2021 {
2022 #ifdef JP
2023 #define LAST_DESTROYED 6
2024         "^P ^N ^B ^F ¾å²¼º¸±¦¤Ë°ÜÆ°",
2025         "^A ^E ¹Ô¤ÎÀèƬ¡¢½ªÃ¼",
2026         "^Q ÆþÎÏ/¥³¥Þ¥ó¥É¥â¡¼¥ÉÀÚ¤êÂؤ¨",
2027         "^R Êѹ¹¤òÁ´¤Æ¼è¤ê¾Ã¤·¤Æ¸µ¤ËÌ᤹",
2028         "------------------------------------",
2029         "^I »ý¤Áʪ/ÁõÈ÷¤«¤éÁªÂò",
2030         "^L",
2031         "^K ¥«¡¼¥½¥ë¤«¤é½ªÃ¼¤Þ¤Çºï½ü",
2032         "^Y ºï½ü(^K)¤·¤¿¹Ô¤òÁÞÆþ",
2033         "^C ¼ï²¡¢¿¦¶È¤Î¾ò·ï¼°¤òÁÞÆþ",
2034         "------------------------------------",
2035         "^S Êѹ¹ (!Ç˲õ/~ÊüÃÖ/½¦¤¦)",
2036         "^G \"(\" Á´ÂΥޥåפÇɽ¼¨¤·¤Ê¤¤",
2037         "^O \"#\" ¼«Æ°¹ï¤ß",
2038         "------------------------------------",
2039         "^U Ì¤´ÕÄê/̤ȽÌÀ/´ÕÄê/*´ÕÄê*",
2040         "^W \"̵²ÁÃͤÎ\"",
2041         "^X ÌµÌÃ/¥¨¥´/¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È",
2042         "^Z \"¼ý½¸Ãæ¤Î\"",
2043         NULL
2044 #else
2045 #define LAST_DESTROYED 6
2046         "^P ^N ^B ^F Move Cursor",
2047         "^A ^E Beginning and End of Line",
2048         "^Q Toggle Insert/Command mode",
2049         "^R Revert to Original File",
2050         "------------------------------------",
2051         "^I Object in Inventry/Equipment",
2052         "^L",
2053         "^K Kill Rest of Line",
2054         "^Y Insert killed(^K) text",
2055         "^C Insert conditional expression",
2056         "------------------------------------",
2057         "^S Toggle(!Destroy/~Leave/Pick)",
2058         "^G \"(\" No display in the 'M'ap",
2059         "^O \"#\" Auto-Inscribe",
2060         "------------------------------------",
2061         "^U Toggle 'identified' state",
2062         "^W \"worthless\"",
2063         "^X Toggle nameless/ego/artifact",
2064         "^Z \"collecting\"",
2065         NULL
2066 #endif
2067 };
2068
2069
2070 #define MAX_YANK 1024
2071 #define DIRTY_ALL 0x01
2072 #define DIRTY_COMMAND 0x02
2073 #define DIRTY_MODE 0x04
2074 #define DIRTY_SCREEN 0x04
2075
2076 /*
2077  * In-game editor of Object Auto-picker/Destoryer
2078  */
2079 void do_cmd_edit_autopick(void)
2080 {
2081         static int cx = 0, cy = 0;
2082         static int upper = 0, left = 0;
2083
2084         cptr last_destroyed;
2085         char last_destroyed_command[WID_DESC+3];
2086         char yank_buf[MAX_YANK];
2087         char classrace[80];
2088         autopick_type an_entry, *entry = &an_entry;
2089         char buf[1024];
2090         cptr *lines_list;
2091
2092         int i, j, k, len;
2093         cptr tmp;
2094
2095         int old_upper = -1, old_left = -1;
2096         int old_cy = -1;
2097         int key = -1, old_key;
2098
2099         bool edit_mode = FALSE;
2100
2101         byte dirty_flags = DIRTY_ALL | DIRTY_COMMAND | DIRTY_MODE;
2102         int dirty_line = -1;
2103
2104         int wid, hgt, old_wid = -1, old_hgt = -1;
2105
2106         /* Free old entries */
2107         init_autopicker();
2108
2109         /* Name of the Last Destroyed Item */
2110         last_destroyed = autopick_line_from_entry(&autopick_entry_last_destroyed);
2111
2112         /* Command Description of the Last Destroyed Item */
2113         if (last_destroyed)
2114         {
2115                 strcpy(last_destroyed_command, "^L \"");
2116                 strncpy(last_destroyed_command + 4, last_destroyed, WID_DESC-4);
2117                 last_destroyed_command[WID_DESC+2] = '\0';
2118         }
2119         else
2120         {
2121 #ifdef JP
2122                 strcpy(last_destroyed_command, "^L ºÇ¸å¤Ë¼«Æ°Ç˲õ¤·¤¿¥¢¥¤¥Æ¥à̾");
2123 #else
2124                 strcpy(last_destroyed_command, "^L Last destroyed object");
2125 #endif
2126         }
2127         ctrl_command_desc[LAST_DESTROYED] = last_destroyed_command;
2128
2129         /* Conditional Expression for Class and Race */
2130         sprintf(classrace, "?:[AND [EQU $RACE %s] [EQU $CLASS %s]]", 
2131 #ifdef JP
2132                 rp_ptr->E_title, cp_ptr->E_title
2133 #else
2134                 rp_ptr->title, cp_ptr->title
2135 #endif
2136                 );
2137
2138         /* Clear yank buffer */
2139         yank_buf[0] = '\0';
2140
2141         /* Read or initialize whole text */
2142         lines_list = read_pickpref_text_lines();
2143
2144         /* Reset cursor position if needed */
2145         for (i = 0; i < cy; i++)
2146         {
2147                 if (!lines_list[i])
2148                 {
2149                         cy = cx = 0;
2150                         break;
2151                 }
2152         }
2153
2154         /* Save the screen */
2155         screen_save();
2156
2157         /* Process requests until done */
2158         while (1)
2159         {
2160                 /* Get size */
2161                 Term_get_size(&wid, &hgt);
2162
2163 #ifdef JP
2164                 /* Don't let cursor at second byte of kanji */
2165                 for (i = 0; lines_list[cy][i]; i++)
2166                         if (iskanji(lines_list[cy][i]))
2167                         {
2168                                 i++;
2169                                 if (i == cx)
2170                                 {
2171                                         cx--;
2172                                         break;
2173                                 }
2174                         }
2175 #endif
2176
2177                 /* Scroll if necessary */
2178                 if (cy < upper || upper + hgt - 4 <= cy)
2179                         upper = cy - (hgt-4)/2;
2180                 if (upper < 0)
2181                         upper = 0;
2182                 if ((cx < left + 10 && left > 0) || left + wid - WID_DESC - 5 <= cx)
2183                         left = cx - (wid - WID_DESC)*2/3;
2184                 if (left < 0)
2185                         left = 0;
2186
2187                 /* Redraw whole window after resize */
2188                 if (old_wid != wid || old_hgt != hgt)
2189                         dirty_flags |= DIRTY_SCREEN;
2190
2191                 /* Redraw all text after scroll */
2192                 else if (old_upper != upper || old_left != left)
2193                         dirty_flags |= DIRTY_ALL;
2194
2195
2196                 if (dirty_flags & DIRTY_SCREEN)
2197                 {
2198                         dirty_flags = DIRTY_ALL | DIRTY_COMMAND | DIRTY_MODE;
2199
2200                         /* Clear screen */
2201                         Term_clear();
2202                 }
2203
2204                 if (dirty_flags & DIRTY_COMMAND)
2205                 {
2206                         /* Display control command */
2207                         for (i = 0; ctrl_command_desc[i]; i++)
2208                                 Term_putstr(wid - WID_DESC, i + 1, WID_DESC, TERM_WHITE, ctrl_command_desc[i]);
2209                 }
2210
2211                 /* Redraw mode line */
2212                 if (dirty_flags & DIRTY_MODE)
2213                 {
2214                         int sepa_length = wid - WID_DESC;
2215
2216                         /* Separator */
2217                         for (i = 0; i < sepa_length; i++)
2218                                 buf[i] = '-';
2219                         buf[i] = '\0';
2220
2221                         /* Mode line */
2222                         if (edit_mode)
2223                                 strncpy(buf + sepa_length - 21, " (INSERT MODE)  ", 16);
2224                         else
2225                                 strncpy(buf + sepa_length - 21, " (COMMAND MODE) ", 16);
2226
2227                         Term_putstr(0, hgt - 3, sepa_length, (byte) (edit_mode ? TERM_YELLOW : TERM_WHITE), buf);
2228                 }
2229                 
2230                 /* Dump up to 20, or hgt-4, lines of messages */
2231                 for (i = 0; i < hgt - 4; i++)
2232                 {
2233                         int leftcol = 0;
2234                         cptr msg;
2235
2236                         /* clean or dirty? */
2237                         if (!(dirty_flags & DIRTY_ALL) && (dirty_line != upper+i))
2238                                 continue;
2239
2240                         msg = lines_list[upper+i];
2241                         if (!msg) break;
2242
2243                         /* Apply horizontal scroll */
2244                         for (j = 0; *msg; msg++, j++)
2245                         {
2246                                 if (j == left) break;
2247 #ifdef JP
2248                                 if (j > left)
2249                                 {
2250                                         leftcol = 1;
2251                                         break;
2252                                 }
2253                                 if (iskanji(*msg))
2254                                 {
2255                                         msg++;
2256                                         j++;
2257                                 }
2258 #endif
2259                         }
2260
2261                         /* Erase line */
2262                         Term_erase(0, i + 1, wid - WID_DESC);
2263
2264                         /* Dump the messages, bottom to top */
2265                         Term_putstr(leftcol, i + 1, wid - WID_DESC - 1, TERM_WHITE, msg);
2266                 }
2267
2268                 for (; i < hgt - 4; i++)
2269                 {
2270                         /* Erase line */
2271                         Term_erase(0, i + 1, wid - WID_DESC);
2272                 }
2273
2274                 /* Display header line */
2275 #ifdef JP
2276                 if (edit_mode)
2277                         prt("^Q ESC ¤Ç¥³¥Þ¥ó¥É¥â¡¼¥É¤Ø°Ü¹Ô¡¢Ä̾ï¤Îʸ»ú¤Ï¤½¤Î¤Þ¤ÞÆþÎÏ", 0, 0);
2278                 else
2279                         prt("q _ ¤Ç½ªÎ»¡¢hjkl2468 ¤Ç°ÜÆ°¡¢^Q a i ¤ÇÆþÎϥ⡼¥É", 0, 0);
2280 #else   
2281                 if (edit_mode)
2282                         prt("Press ^Q ESC to command mode, any letters to insert", 0, 0);
2283                 else
2284                         prt("Press q _ to quit, hjkl2468 to move, ^Q a i to insert mode", 0, 0);
2285 #endif
2286                 /* Display current position */
2287                 prt (format("(%d,%d)", cx, cy), 0, 70);
2288
2289                 /* Display information when updated */
2290                 if (old_cy != cy || (dirty_flags & DIRTY_ALL) || dirty_line == cy)
2291                 {
2292                         /* Clear information line */
2293                         Term_erase(0, hgt - 3 + 1, wid);
2294                         Term_erase(0, hgt - 3 + 2, wid);
2295
2296                         /* Display information */
2297                         if (lines_list[cy][0] == '#')
2298                         {
2299 #ifdef JP
2300                                 prt("¤³¤Î¹Ô¤Ï¥³¥á¥ó¥È¤Ç¤¹¡£", hgt - 3 + 1, 0);
2301 #else
2302                                 prt("This line is comment.", hgt - 3 + 1, 0);
2303 #endif
2304                         }
2305                         else if (lines_list[cy][1] == ':')
2306                         {
2307                                 switch(lines_list[cy][0])
2308                                 {
2309                                 case '?':
2310 #ifdef JP
2311                                         prt("¤³¤Î¹Ô¤Ï¾ò·ïʬ´ô¼°¤Ç¤¹¡£", hgt - 3 + 1, 0);
2312 #else
2313                                         prt("This line is Conditional Expression.", hgt - 3 + 1, 0);
2314 #endif
2315                                         break;
2316                                 case 'A':
2317 #ifdef JP
2318                                         prt("¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¼Â¹ÔÆâÍƤòÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2319 #else
2320                                         prt("This line defines Macro action.", hgt - 3 + 1, 0);
2321 #endif
2322                                         break;
2323                                 case 'P':
2324 #ifdef JP
2325                                         prt("¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¥È¥ê¥¬¡¼¡¦¥­¡¼¤òÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2326 #else
2327                                         prt("This line defines Macro trigger key.", hgt - 3 + 1, 0);
2328 #endif
2329                                         break;
2330                                 case 'C':
2331 #ifdef JP
2332                                         prt("¤³¤Î¹Ô¤Ï¥­¡¼ÇÛÃÖ¤òÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2333 #else
2334                                         prt("This line defines Keymap.", hgt - 3 + 1, 0);
2335 #endif
2336                                         break;
2337                                 }
2338                         }
2339
2340                         /* Get description of an autopicker preference line */
2341                         else if (autopick_new_entry(entry, lines_list[cy]))
2342                         {
2343                                 char temp[80*4];
2344                                 cptr t;
2345
2346                                 describe_autopick(buf, entry);
2347
2348                                 roff_to_buf(buf, 79, temp);
2349                                 t = temp;
2350                                 for (i = 0; i< 2; i++)
2351                                 {
2352                                         if(t[0] == 0)
2353                                                 break; 
2354                                         else
2355                                         {
2356                                                 prt(t, hgt - 3 + 1 + i, 0);
2357                                                 t += strlen(t) + 1;
2358                                         }
2359                                 }
2360                                 autopick_free_entry(entry);
2361                         }
2362                 }
2363
2364                 /* Place cursor */
2365                 Term_gotoxy(cx - left, cy - upper + 1);
2366
2367                 /* Now clean */
2368                 dirty_flags = 0;
2369                 dirty_line = -1;
2370
2371                 /* Save old key and location */
2372                 old_cy = cy;
2373                 old_key = key;
2374                 old_upper = upper;
2375                 old_left = left;
2376                 old_wid = wid;
2377                 old_hgt = hgt;
2378
2379                 /* Do not process macros except special keys */
2380                 inkey_special = TRUE;
2381
2382                 /* Get a command */
2383                 key = inkey();
2384
2385                 if (edit_mode)
2386                 {
2387                         if (key == ESCAPE)
2388                         {
2389                                 edit_mode = FALSE;
2390
2391                                 /* Mode line is now dirty */
2392                                 dirty_flags |= DIRTY_MODE;
2393                         }
2394                         else if (!iscntrl(key&0xff))
2395                         {
2396                                 int next;
2397
2398                                 for (i = j = 0; lines_list[cy][i] && i < cx; i++)
2399                                         buf[j++] = lines_list[cy][i];
2400
2401 #ifdef JP
2402                                 if (iskanji(key))
2403                                 {
2404                                         inkey_base = TRUE;
2405                                         next = inkey();
2406                                         if (j+2 < 1024)
2407                                         {
2408                                                 buf[j++] = key;
2409                                                 buf[j++] = next;
2410                                                 cx += 2;
2411                                         }
2412                                         else
2413                                                 bell();
2414                                 }
2415                                 else
2416 #endif
2417                                 {
2418                                         if (j+1 < 1024)
2419                                                 buf[j++] = key;
2420                                         cx++;
2421                                 }
2422                                 for (; lines_list[cy][i] && j + 1 < 1024; i++)
2423                                         buf[j++] = lines_list[cy][i];
2424                                 buf[j] = '\0';
2425                                 string_free(lines_list[cy]);
2426                                 lines_list[cy] = string_make(buf);
2427                                 len = strlen(lines_list[cy]);
2428                                 if (len < cx) cx = len;
2429
2430                                 /* Now dirty */
2431                                 dirty_line = cy;
2432                         }
2433                 }
2434                 else
2435                 {
2436                         /* Exit on 'q' */
2437                         if (key == 'q' || key == '_') break;
2438
2439                         switch(key)
2440                         {
2441                         case 'a': case 'i':
2442                                 edit_mode = TRUE;
2443
2444                                 /* Mode line is now dirty */
2445                                 dirty_flags |= DIRTY_MODE;
2446                                 break;
2447                         case '~':
2448                                 if (!autopick_new_entry(entry, lines_list[cy]))
2449                                         break;
2450                                 string_free(lines_list[cy]);
2451
2452                                 entry->action &= ~DO_AUTODESTROY;
2453                                 if (entry->action & DO_AUTOPICK)
2454                                 {
2455                                         entry->action &= ~DO_AUTOPICK;
2456                                         entry->action |= DONT_AUTOPICK;
2457                                 }
2458                                 else 
2459                                 {
2460                                         entry->action &= ~DONT_AUTOPICK;
2461                                         entry->action |= DO_AUTOPICK;
2462                                 }
2463
2464                                 lines_list[cy] = autopick_line_from_entry(entry);
2465
2466                                 /* Now dirty */
2467                                 dirty_line = cy;
2468                                 break;
2469                         case '!':
2470                                 if (!autopick_new_entry(entry, lines_list[cy]))
2471                                         break;
2472                                 string_free(lines_list[cy]);
2473
2474                                 entry->action &= ~DONT_AUTOPICK;
2475                                 if (entry->action & DO_AUTOPICK)
2476                                 {
2477                                         entry->action &= ~DO_AUTOPICK;
2478                                         entry->action |= DO_AUTODESTROY;
2479                                 }
2480                                 else 
2481                                 {
2482                                         entry->action &= ~DO_AUTODESTROY;
2483                                         entry->action |= DO_AUTOPICK;
2484                                 }
2485
2486                                 lines_list[cy] = autopick_line_from_entry(entry);
2487
2488                                 /* Now dirty */
2489                                 dirty_line = cy;
2490                                 break;
2491                         case '(':
2492                                 key = KTRL('g');
2493                                 break;
2494                         case '#':
2495                         case '{':
2496                                 key = KTRL('o');
2497                                 break;
2498                         case 'h': case '4':
2499                                 key = KTRL('b');
2500                                 break;
2501                         case 'l': case '6':
2502                                 key = KTRL('f');
2503                                 break;
2504                         case 'j': case '2':
2505                                 key = KTRL('n');
2506                                 break;
2507                         case 'k': case '8':
2508                                 key = KTRL('p');
2509                                 break;
2510                         case ' ':
2511                                 while (cy < upper + hgt-4 && lines_list[cy + 1])
2512                                         cy++;
2513                                 upper = cy;
2514                                 break;
2515                         case '-': case 'b':
2516                                 while (0 < cy && upper <= cy)
2517                                         cy--;
2518                                 while (0 < upper && cy + 1 < upper + hgt - 4)
2519                                         upper--;
2520                                 break;
2521                         }
2522                 }
2523
2524                 switch(key)
2525                 {
2526                 case KTRL('a'):
2527                         /* Beginning of line */
2528                         cx = 0;
2529                         break;
2530                 case KTRL('b'):
2531                         /* Back */
2532                         if (0 < cx)
2533                         {
2534                                 cx--;
2535                                 len = strlen(lines_list[cy]);
2536                                 if (len < cx) cx = len;
2537                         }
2538                         else if (cy > 0)
2539                         {
2540                                 cy--;
2541                                 cx = strlen(lines_list[cy]);
2542                         }
2543                         break;
2544                 case KTRL('c'):
2545                         /* Insert a conditinal expression line */
2546                         insert_return_code(lines_list, 0, cy);
2547                         lines_list[cy] = string_make(classrace);
2548                         cy++;
2549                         insert_return_code(lines_list, 0, cy);
2550                         lines_list[cy] = string_make("?:1");
2551                         cx = 0;
2552
2553                         /* Now dirty */
2554                         dirty_flags |= DIRTY_ALL;
2555                         break;
2556                 case KTRL('e'):
2557                         /* End of line */
2558                         cx = strlen(lines_list[cy]);
2559                         break;
2560                 case KTRL('f'):
2561                         /* Forward */
2562 #ifdef JP
2563                         if (iskanji(lines_list[cy][cx])) cx++;
2564 #endif
2565                         cx++;
2566                         len = strlen(lines_list[cy]);
2567                         if (len < cx)
2568                         {
2569                                 if (lines_list[cy + 1])
2570                                 {
2571                                         cy++;
2572                                         cx = 0;
2573                                 }
2574                                 else
2575                                         cx = len;
2576                         }
2577                         break;
2578                 case KTRL('g'):
2579                         /* Toggle display in the 'M'ap */
2580                         if (lines_list[cy][0] != '(' && lines_list[cy][1] != '(')
2581                                 insert_string(lines_list, "(", 0, cy);
2582                         else if (lines_list[cy][0] == '(')
2583                                 delete_string(lines_list, 1, 0, cy);
2584                         else if (lines_list[cy][1] == '(')
2585                                 delete_string(lines_list, 1, 1, cy);
2586
2587                         /* Now dirty */
2588                         dirty_line = cy;
2589                         break;
2590                 case KTRL('i'):
2591                         /* Insert choosen item name */
2592                         if (!entry_from_choosed_object(entry))
2593                         {
2594                                 /* Now dirty because of item/equip menu */
2595                                 dirty_flags |= DIRTY_SCREEN;
2596                                 break;
2597                         }
2598                         tmp = autopick_line_from_entry(entry);
2599                         autopick_free_entry(entry);
2600                         if (tmp)
2601                         {
2602                                 insert_return_code(lines_list, 0, cy);
2603                                 lines_list[cy] = tmp;
2604                                 cx = 0;
2605
2606                                 /* Now dirty because of item/equip menu */
2607                                 dirty_flags |= DIRTY_SCREEN;
2608                         }
2609                         break;
2610                 case KTRL('l'):
2611                         /* Insert a name of last destroyed item */
2612                         if (last_destroyed)
2613                         {
2614                                 insert_return_code(lines_list, 0, cy);
2615                                 lines_list[cy] = string_make(last_destroyed);
2616                                 cx = 0;
2617
2618                                 /* Now dirty */
2619                                 dirty_flags |= DIRTY_ALL;
2620                         }
2621                         break;
2622                 case '\n': case '\r':
2623                         /* Split a line or insert end of line */
2624                         insert_return_code(lines_list, cx, cy);
2625                         cy++;
2626                         cx = 0;
2627
2628                         /* Now dirty */
2629                         dirty_flags |= DIRTY_ALL;
2630                         break;
2631                 case KTRL('n'):
2632                         /* Next line */
2633                         if (lines_list[cy + 1]) cy++;
2634                         break;
2635                 case KTRL('o'):
2636                         /* Prepare to write auto-inscription text */
2637                         for (i = 0; lines_list[cy][i]; i++)
2638                                 if (lines_list[cy][i] == '#') break;
2639                         if (!lines_list[cy][i]) insert_string(lines_list, "#", i, cy);
2640                         cx = i + 1;
2641                         edit_mode = TRUE;
2642
2643                         /* Now dirty */
2644                         dirty_line = cy;
2645                         dirty_flags |= DIRTY_MODE;
2646                         break;
2647                 case KTRL('p'):
2648                         /* Previous line */
2649                         if (cy > 0) cy--;
2650                         break;
2651                 case KTRL('q'):
2652                         /* Change mode */
2653                         edit_mode = !edit_mode;
2654                         
2655                         /* Mode line is now dirty */
2656                         dirty_flags |= DIRTY_MODE;
2657                         break;
2658                 case KTRL('r'):
2659                         /* Revert to original */
2660 #ifdef JP
2661                         if (!get_check("Á´¤Æ¤ÎÊѹ¹¤òÇË´þ¤·¤Æ¸µ¤Î¾õÂÖ¤ËÌᤷ¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡© "))
2662 #else
2663                         if (!get_check("Discard all change and revert to original file. Are you sure? "))
2664 #endif
2665                                 break;
2666
2667                         free_text_lines(lines_list);
2668                         lines_list = read_pickpref_text_lines();
2669                         dirty_flags |= DIRTY_ALL | DIRTY_MODE;
2670                         cx = cy = 0;
2671                         edit_mode = FALSE;
2672                         break;
2673                 case KTRL('s'):
2674                         /* Rotate action; pickup/destroy/leave */
2675                         if (!autopick_new_entry(entry, lines_list[cy]))
2676                                 break;
2677                         string_free(lines_list[cy]);
2678
2679                         if (entry->action & DO_AUTOPICK)
2680                         {
2681                                 entry->action &= ~DO_AUTOPICK;
2682                                 entry->action |= DO_AUTODESTROY;
2683                         }
2684                         else if (entry->action & DO_AUTODESTROY)
2685                         {
2686                                 entry->action &= ~DO_AUTODESTROY;
2687                                 entry->action |= DONT_AUTOPICK;
2688                         }
2689                         else if (entry->action & DONT_AUTOPICK)
2690                         {
2691                                 entry->action &= ~DONT_AUTOPICK;
2692                                 entry->action |= DO_AUTOPICK;
2693                         }
2694
2695                         lines_list[cy] = autopick_line_from_entry(entry);
2696                         /* Now dirty */
2697                         dirty_line = cy;
2698
2699                         break;
2700                 case KTRL('t'):
2701                         /* Nothing */
2702                         break;
2703                 case KTRL('u'):
2704                         /* Rotate identify-state; identified/unidentified/... */
2705                         if (!autopick_new_entry(entry, lines_list[cy]))
2706                                 break;
2707                         string_free(lines_list[cy]);
2708
2709                         if (IS_FLG(FLG_UNIDENTIFIED)) 
2710                         {
2711                                 REM_FLG(FLG_UNIDENTIFIED);
2712                                 ADD_FLG(FLG_UNAWARE);
2713                                 REM_FLG(FLG_IDENTIFIED);
2714                                 REM_FLG(FLG_STAR_IDENTIFIED);
2715                         }
2716                         else if (IS_FLG(FLG_UNAWARE)) 
2717                         {
2718                                 REM_FLG(FLG_UNIDENTIFIED);
2719                                 REM_FLG(FLG_UNAWARE);
2720                                 ADD_FLG(FLG_IDENTIFIED);
2721                                 REM_FLG(FLG_STAR_IDENTIFIED);
2722                         }
2723                         else if (IS_FLG(FLG_STAR_IDENTIFIED)) 
2724                         {
2725                                 REM_FLG(FLG_UNIDENTIFIED);
2726                                 REM_FLG(FLG_UNAWARE);
2727                                 REM_FLG(FLG_IDENTIFIED);
2728                                 REM_FLG(FLG_STAR_IDENTIFIED);
2729                         }
2730                         else if (IS_FLG(FLG_IDENTIFIED)) 
2731                         {
2732                                 REM_FLG(FLG_UNIDENTIFIED);
2733                                 REM_FLG(FLG_UNAWARE);
2734                                 REM_FLG(FLG_IDENTIFIED);
2735                                 ADD_FLG(FLG_STAR_IDENTIFIED);
2736                         }
2737                         else
2738                         {
2739                                 ADD_FLG(FLG_UNIDENTIFIED);
2740                                 REM_FLG(FLG_UNAWARE);
2741                                 REM_FLG(FLG_IDENTIFIED);
2742                                 REM_FLG(FLG_STAR_IDENTIFIED);
2743                         }
2744
2745                         lines_list[cy] = autopick_line_from_entry(entry);
2746
2747                         /* Now dirty */
2748                         dirty_line = cy;
2749                         break;
2750                 case KTRL('v'):
2751                         /* Scroll up */
2752                         while (cy < upper + hgt-4 && lines_list[cy + 1])
2753                                 cy++;
2754                         upper = cy;
2755                         break;
2756                 case KTRL('w'):
2757                         /* Toggle 'worthless' */
2758                         toggle_string(lines_list, FLG_WORTHLESS, cy);
2759                         /* Now dirty */
2760                         dirty_line = cy;
2761                         break;
2762                 case KTRL('x'):
2763                         /* Rotate within nameless, ego, artifact */
2764                         if (!autopick_new_entry(entry, lines_list[cy]))
2765                                 break;
2766                         string_free(lines_list[cy]);
2767
2768                         if (IS_FLG(FLG_NAMELESS)) 
2769                         {
2770                                 REM_FLG(FLG_NAMELESS);
2771                                 ADD_FLG(FLG_EGO);
2772                                 REM_FLG(FLG_ARTIFACT);
2773                         }
2774                         else if (IS_FLG(FLG_EGO)) 
2775                         {
2776                                 REM_FLG(FLG_NAMELESS);
2777                                 REM_FLG(FLG_EGO);
2778                                 ADD_FLG(FLG_ARTIFACT);
2779                         }
2780                         else if (IS_FLG(FLG_ARTIFACT)) 
2781                         {
2782                                 REM_FLG(FLG_NAMELESS);
2783                                 REM_FLG(FLG_EGO);
2784                                 REM_FLG(FLG_ARTIFACT);
2785                         }
2786                         else
2787                         {
2788                                 ADD_FLG(FLG_NAMELESS);
2789                                 REM_FLG(FLG_EGO);
2790                                 REM_FLG(FLG_ARTIFACT);
2791                         }
2792
2793                         lines_list[cy] = autopick_line_from_entry(entry);
2794
2795                         /* Now dirty */
2796                         dirty_line = cy;
2797                         break;
2798
2799                 case KTRL('y'):
2800                         /* Paste killed text */
2801                         if (strlen(yank_buf))
2802                         {
2803                                 cx = 0;
2804                                 for (j = 0; yank_buf[j]; j += strlen(yank_buf + j) + 1)
2805                                 {
2806                                         insert_return_code(lines_list, 0, cy);
2807                                         lines_list[cy] = string_make(yank_buf + j);
2808                                         cy++;
2809                                 }
2810
2811                                 /* Now dirty */
2812                                 dirty_flags |= DIRTY_ALL;
2813                         }
2814                         break;
2815                 case KTRL('z'):
2816                         /* Toggle 'collecting' */
2817                         toggle_string(lines_list, FLG_COLLECTING, cy);
2818                         /* Now dirty */
2819                         dirty_line = cy;
2820                         break;
2821
2822                 case KTRL('k'):
2823                         /* Kill rest of line */
2824                         if (lines_list[cy][0] != '\0' && (unsigned int) cx < strlen(lines_list[cy]))
2825                         {
2826                                 for (i = j = 0; lines_list[cy][i] && i < cx; i++)
2827                                 {
2828 #ifdef JP
2829                                         if (iskanji(lines_list[cy][i]))
2830                                                 buf[j++] = lines_list[cy][i++];
2831 #endif
2832                                         buf[j++] = lines_list[cy][i];
2833                                 }
2834                                 buf[j] = '\0';
2835
2836                                 j = 0;
2837                                 if (old_key == KTRL('k'))
2838                                         while (yank_buf[j])
2839                                                 j += strlen(yank_buf + j) + 1;
2840
2841                                 if (j < MAX_YANK - 2)
2842                                 {
2843                                         strncpy(yank_buf + j, lines_list[cy] + i, MAX_YANK-j-2);
2844                                         yank_buf[MAX_YANK-2] = '\0';
2845                                         yank_buf[j + strlen(lines_list[cy] + i) + 1] = '\0';
2846                                 }
2847                                 string_free(lines_list[cy]);
2848                                 lines_list[cy] = string_make(buf);
2849
2850                                 /* Now dirty */
2851                                 dirty_line = cy;
2852                                 break;                  
2853                         }
2854                         /* fall through */
2855                 case KTRL('d'):
2856                 case 0x7F:
2857                         /* DELETE == go forward + BACK SPACE */
2858 #ifdef JP
2859                         if (iskanji(lines_list[cy][cx])) cx++;
2860 #endif
2861                         cx++;
2862                         len = strlen(lines_list[cy]);
2863                         if (len < cx)
2864                         {
2865                                 if (lines_list[cy + 1])
2866                                 {
2867                                         cy++;
2868                                         cx = 0;
2869                                 }
2870                                 else
2871                                 {
2872                                         cx = len;
2873                                         break;
2874                                 }
2875                         }
2876
2877                         /* fall through */
2878
2879                 case '\010':
2880                         /* BACK SPACE */
2881                         if (cx == 0)
2882                         {
2883                                 /* delete a return code and union two lines */
2884                                 if (cy == 0) break;
2885                                 cx = strlen(lines_list[cy-1]);
2886                                 strcpy(buf, lines_list[cy-1]);
2887                                 strcat(buf, lines_list[cy]);
2888                                 string_free(lines_list[cy-1]);
2889                                 string_free(lines_list[cy]);
2890                                 lines_list[cy-1] = string_make(buf);
2891                                 for (i = cy; lines_list[i+1]; i++)
2892                                         lines_list[i] = lines_list[i+1];
2893                                 lines_list[i] = NULL;
2894                                 cy--;
2895
2896                                 /* Now dirty */
2897                                 dirty_flags |= DIRTY_ALL;
2898                                 break;
2899                         }
2900
2901                         for (i = j = 0; lines_list[cy][i] && i < cx; i++)
2902                         {
2903                                 k = j;
2904 #ifdef JP
2905                                 if (iskanji(lines_list[cy][i]))
2906                                         buf[j++] = lines_list[cy][i++];
2907 #endif
2908                                 buf[j++] = lines_list[cy][i];
2909                         }
2910                         while (j > k)
2911                         {
2912                                 cx--;
2913                                 j--;
2914                         }
2915                         for (; lines_list[cy][i]; i++)
2916                                 buf[j++] = lines_list[cy][i];
2917                         buf[j] = '\0';
2918                         string_free(lines_list[cy]);
2919                         lines_list[cy] = string_make(buf);
2920
2921                         /* Now dirty */
2922                         dirty_line = cy;
2923                         break;
2924                 }
2925
2926         } /* while (1) */
2927
2928         /* Restore the screen */
2929         screen_load();
2930
2931 #ifdef JP
2932         sprintf(buf, "picktype-%s.prf", player_name);
2933 #else
2934         sprintf(buf, "pickpref-%s.prf", player_name);
2935 #endif
2936         write_text_lines(buf, lines_list);
2937         free_text_lines(lines_list);
2938
2939         string_free(last_destroyed);
2940
2941         /* Reload autopick pref */
2942         process_pickpref_file(buf);
2943 }