OSDN Git Service

e9bf264db139b9496c2cd4fb5e1c78058d7bc285
[hengband/hengband.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         /* Protected by auto-picker */
922         if (autopick_idx >= 0 &&
923             !(autopick_list[autopick_idx].action & DO_AUTODESTROY))
924                 destroy = FALSE;
925
926         if (!always_pickup)
927         {
928                 /* Auto-picker/destroyer */
929                 if (autopick_idx >= 0 &&
930                     (autopick_list[autopick_idx].action & DO_AUTODESTROY))
931                         destroy = TRUE;
932         }
933
934         /* Not to be destroyed */
935         if (!destroy) return FALSE;
936
937         /* Now decided to destroy */
938
939         disturb(0,0);
940
941         /* Describe the object (with {terrible/special}) */
942         object_desc(o_name, o_ptr, TRUE, 3);
943
944         /* Artifact? */
945         if (!can_player_destroy_object(o_ptr))
946         {
947                 /* Message */
948 #ifdef JP
949                 msg_format("%s¤ÏÇ˲õÉÔǽ¤À¡£", o_name);
950 #else
951                 msg_format("You cannot auto-destroy %s.", o_name);
952 #endif
953
954                 /* Done */
955                 return TRUE;
956         }
957
958         /* Record name of destroyed item */
959         autopick_free_entry(&autopick_entry_last_destroyed);
960         autopick_entry_from_object(&autopick_entry_last_destroyed, o_ptr);
961
962         /* Eliminate the item (from the pack) */
963         if (item >= 0)
964         {
965                 inven_item_increase(item, -(o_ptr->number));
966                 inven_item_optimize(item);
967         }
968
969         /* Eliminate the item (from the floor) */
970         else
971         {
972                 delete_object_idx(0 - item);
973         }
974
975         /* Print a message */
976 #ifdef JP
977         msg_format("%s¤ò¼«Æ°Ç˲õ¤·¤Þ¤¹¡£", o_name);
978 #else
979         msg_format("Auto-destroying %s.", o_name);
980 #endif
981                         
982         return TRUE;
983 }
984
985
986 /*
987  * Automatically pickup/destroy items in this grid.
988  */
989 void auto_pickup_items(cave_type *c_ptr)
990 {
991         s16b this_o_idx, next_o_idx = 0;
992         
993         /* Scan the pile of objects */
994         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
995         {
996                 int idx;
997         
998                 /* Acquire object */
999                 object_type *o_ptr = &o_list[this_o_idx];
1000                 
1001                 /* Acquire next object */
1002                 next_o_idx = o_ptr->next_o_idx;
1003
1004                 idx = is_autopick(o_ptr);
1005
1006                 /* Item index for floor -1,-2,-3,...  */
1007                 auto_inscribe_item((-this_o_idx), idx);
1008
1009                 if (idx >= 0 && (autopick_list[idx].action & DO_AUTOPICK))
1010                 {
1011                         disturb(0,0);
1012
1013                         if (!inven_carry_okay(o_ptr))
1014                         {
1015                                 char o_name[MAX_NLEN];
1016
1017                                 /* Describe the object */
1018                                 object_desc(o_name, o_ptr, TRUE, 3);
1019
1020                                 /* Message */
1021 #ifdef JP
1022                                 msg_format("¥¶¥Ã¥¯¤Ë¤Ï%s¤òÆþ¤ì¤ë·ä´Ö¤¬¤Ê¤¤¡£", o_name);
1023 #else
1024                                 msg_format("You have no room for %s.", o_name);
1025 #endif
1026                                 continue;
1027                         }
1028                         py_pickup_aux(this_o_idx);
1029
1030                         continue;
1031                 }
1032                 
1033                 /*
1034                  * Do auto-destroy;
1035                  * When always_pickup is 'yes', we disable
1036                  * auto-destroyer from autopick function, and do only
1037                  * easy-auto-destroyer.
1038                  */
1039                 else
1040                 {
1041                         if (auto_destroy_item((-this_o_idx), idx))
1042                                 continue;
1043                 }
1044         }
1045 }
1046
1047
1048 /*
1049  * Describe which kind of object is Auto-picked/destroyed
1050  */
1051 static void describe_autopick(char *buff, autopick_type *entry)
1052 {
1053         cptr str = entry->name;
1054         byte act = entry->action;
1055         cptr insc = entry->insc;
1056         int i;
1057
1058         bool top = FALSE;
1059
1060 #ifdef JP
1061         cptr before_str[100], body_str;
1062         int before_n = 0;
1063
1064         body_str = "¥¢¥¤¥Æ¥à";
1065
1066         /*** Collecting items ***/
1067         /*** Which can be absorbed into a slot as a bundle ***/
1068         if (IS_FLG(FLG_COLLECTING))
1069                 before_str[before_n++] = "¼ý½¸Ãæ¤Ç´û¤Ë»ý¤Ã¤Æ¤¤¤ë¥¹¥í¥Ã¥È¤Ë¤Þ¤È¤á¤é¤ì¤ë";
1070         
1071         /*** Unidentified ***/
1072         if (IS_FLG(FLG_UNIDENTIFIED))
1073                 before_str[before_n++] = "̤´ÕÄê¤Î";
1074
1075         /*** Identified ***/
1076         if (IS_FLG(FLG_IDENTIFIED))
1077                 before_str[before_n++] = "´ÕÄêºÑ¤ß¤Î";
1078
1079         /*** *Identified* ***/
1080         if (IS_FLG(FLG_STAR_IDENTIFIED))
1081                 before_str[before_n++] = "´°Á´¤Ë´ÕÄêºÑ¤ß¤Î";
1082
1083         /*** Dice boosted (weapon of slaying) ***/
1084         if (IS_FLG(FLG_BOOSTED))
1085         {
1086                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤¬Ä̾ï¤è¤êÂ礭¤¤";
1087                 body_str = "Éð´ï";
1088         }
1089
1090         /*** Weapons whose dd*ds is more than nn ***/
1091         if (IS_FLG(FLG_MORE_THAN))
1092         {
1093                 static char more_than_desc_str[] = "___";
1094                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤ÎºÇÂçÃͤ¬";
1095                 body_str = "Éð´ï";
1096                         
1097                 sprintf(more_than_desc_str,"%d", entry->dice);
1098                 before_str[before_n++] = more_than_desc_str;
1099                 before_str[before_n++] = "°Ê¾å¤Î";
1100         }
1101
1102         /*** Items whose magical bonus is more than nn ***/
1103         if (IS_FLG(FLG_MORE_BONUS))
1104         {
1105                 static char more_bonus_desc_str[] = "___";
1106                 before_str[before_n++] = "½¤ÀµÃͤ¬(+";
1107                         
1108                 sprintf(more_bonus_desc_str,"%d", entry->bonus);
1109                 before_str[before_n++] = more_bonus_desc_str;
1110                 before_str[before_n++] = ")°Ê¾å¤Î";
1111         }
1112
1113         /*** Worthless items ***/
1114         if (IS_FLG(FLG_WORTHLESS))
1115                 before_str[before_n++] = "Ź¤Ç̵²ÁÃͤÈȽÄꤵ¤ì¤ë";
1116
1117         /*** Artifact ***/
1118         if (IS_FLG(FLG_ARTIFACT))
1119         {
1120                 before_str[before_n++] = "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Î";
1121                 body_str = "ÁõÈ÷";
1122         }
1123
1124         /*** Ego ***/
1125         if (IS_FLG(FLG_EGO))
1126         {
1127                 before_str[before_n++] = "¥¨¥´¥¢¥¤¥Æ¥à¤Î";
1128                 body_str = "ÁõÈ÷";
1129         }
1130
1131         /*** Nameless ***/
1132         if (IS_FLG(FLG_NAMELESS))
1133         {
1134                 before_str[before_n++] = "¥¨¥´¤Ç¤â¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Ç¤â¤Ê¤¤";
1135                 body_str = "ÁõÈ÷";
1136         }
1137
1138         /*** Unaware items ***/
1139         if (IS_FLG(FLG_UNAWARE))
1140                 before_str[before_n++] = "̤´ÕÄê¤Ç¤½¤Î¸ú²Ì¤âȽÌÀ¤·¤Æ¤¤¤Ê¤¤";
1141
1142         /*** Wanted monster's corpse/skeletons ***/
1143         if (IS_FLG(FLG_WANTED))
1144         {
1145                 before_str[before_n++] = "¥Ï¥ó¥¿¡¼»ö̳½ê¤Ç¾Þ¶â¼ó¤È¤µ¤ì¤Æ¤¤¤ë";
1146                 body_str = "»àÂΤä¹ü";
1147         }
1148
1149         /*** Human corpse/skeletons (for Daemon magic) ***/
1150         if (IS_FLG(FLG_HUMAN))
1151         {
1152                 before_str[before_n++] = "°­ËâËâË¡¤Ç»È¤¦¤¿¤á¤Î¿Í´Ö¤ä¥Ò¥å¡¼¥Þ¥Î¥¤¥É¤Î";
1153                 body_str = "»àÂΤä¹ü";
1154         }
1155
1156         /*** Unique monster's corpse/skeletons/statues ***/
1157         if (IS_FLG(FLG_UNIQUE))
1158         {
1159                 before_str[before_n++] = "¥æ¥Ë¡¼¥¯¥â¥ó¥¹¥¿¡¼¤Î";
1160                 body_str = "»àÂΤä¹ü";
1161         }
1162
1163         /*** Unreadable spellbooks ***/
1164         if (IS_FLG(FLG_UNREADABLE))
1165         {
1166                 before_str[before_n++] = "¤¢¤Ê¤¿¤¬Æɤá¤Ê¤¤Îΰè¤Î";
1167                 body_str = "ËâË¡½ñ";
1168         }
1169
1170         /*** First realm spellbooks ***/
1171         if (IS_FLG(FLG_REALM1))
1172         {
1173                 before_str[before_n++] = "Âè°ìÎΰè¤Î";
1174                 body_str = "ËâË¡½ñ";
1175         }
1176
1177         /*** Second realm spellbooks ***/
1178         if (IS_FLG(FLG_REALM2))
1179         {
1180                 before_str[before_n++] = "ÂèÆóÎΰè¤Î";
1181                 body_str = "ËâË¡½ñ";
1182         }
1183
1184         /*** First rank spellbooks ***/
1185         if (IS_FLG(FLG_FIRST))
1186         {
1187                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î1ºýÌܤÎ";
1188                 body_str = "ËâË¡½ñ";
1189         }
1190
1191         /*** Second rank spellbooks ***/
1192         if (IS_FLG(FLG_SECOND))
1193         {
1194                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î2ºýÌܤÎ";
1195                 body_str = "ËâË¡½ñ";
1196         }
1197
1198         /*** Third rank spellbooks ***/
1199         if (IS_FLG(FLG_THIRD))
1200         {
1201                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î3ºýÌܤÎ";
1202                 body_str = "ËâË¡½ñ";
1203         }
1204
1205         /*** Fourth rank spellbooks ***/
1206         if (IS_FLG(FLG_FOURTH))
1207         {
1208                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î4ºýÌܤÎ";
1209                 body_str = "ËâË¡½ñ";
1210         }
1211
1212         /*** Items ***/
1213         if (IS_FLG(FLG_ITEMS))
1214                 ; /* Nothing to do */
1215         else if (IS_FLG(FLG_WEAPONS))
1216                 body_str = "Éð´ï";
1217         else if (IS_FLG(FLG_ARMORS))
1218                 body_str = "Ëɶñ";
1219         else if (IS_FLG(FLG_MISSILES))
1220                 body_str = "ÃƤäÌð¤ä¥¯¥í¥¹¥Ü¥¦¤ÎÌð";
1221         else if (IS_FLG(FLG_DEVICES))
1222                 body_str = "´¬Êª¤äËâË¡ËÀ¤ä¾ó¤ä¥í¥Ã¥É";
1223         else if (IS_FLG(FLG_LIGHTS))
1224                 body_str = "¸÷¸»ÍѤΥ¢¥¤¥Æ¥à";
1225         else if (IS_FLG(FLG_JUNKS))
1226                 body_str = "Àޤ줿ËÀÅù¤Î¥¬¥é¥¯¥¿";
1227         else if (IS_FLG(FLG_SPELLBOOKS))
1228                 body_str = "ËâË¡½ñ";
1229         else if (IS_FLG(FLG_HAFTED))
1230                 body_str = "Æß´ï";
1231         else if (IS_FLG(FLG_SHIELDS))
1232                 body_str = "½â";
1233         else if (IS_FLG(FLG_BOWS))
1234                 body_str = "¥¹¥ê¥ó¥°¤äµÝ¤ä¥¯¥í¥¹¥Ü¥¦";
1235         else if (IS_FLG(FLG_RINGS))
1236                 body_str = "»ØÎØ";
1237         else if (IS_FLG(FLG_AMULETS))
1238                 body_str = "¥¢¥ß¥å¥ì¥Ã¥È";
1239         else if (IS_FLG(FLG_SUITS))
1240                 body_str = "³»";
1241         else if (IS_FLG(FLG_CLOAKS))
1242                 body_str = "¥¯¥í¡¼¥¯";
1243         else if (IS_FLG(FLG_HELMS))
1244                 body_str = "¥Ø¥ë¥á¥Ã¥È¤ä´§";
1245         else if (IS_FLG(FLG_GLOVES))
1246                 body_str = "¾®¼ê";
1247         else if (IS_FLG(FLG_BOOTS))
1248                 body_str = "¥Ö¡¼¥Ä";
1249
1250         *buff = '\0';
1251         if (!before_n) 
1252                 strcat(buff, "Á´¤Æ¤Î");
1253         else for (i = 0; i < before_n && before_str[i]; i++)
1254                 strcat(buff, before_str[i]);
1255
1256         strcat(buff, body_str);
1257
1258         if (*str)
1259         {
1260                 if (*str == '^')
1261                 {
1262                         str++;
1263                         top = TRUE;
1264                 }
1265
1266                 strcat(buff, "¤Ç¡¢Ì¾Á°¤¬¡Ö");
1267                 strncat(buff, str, 80);
1268                 if (top)
1269                         strcat(buff, "¡×¤Ç»Ï¤Þ¤ë¤â¤Î");
1270                 else
1271                         strcat(buff, "¡×¤ò´Þ¤à¤â¤Î");
1272         }
1273
1274         if (insc)
1275                 strncat(buff, format("¤Ë¡Ö%s¡×¤È¹ï¤ó¤Ç", insc), 80);
1276         else
1277                 strcat(buff, "¤ò");
1278
1279         if (act & DONT_AUTOPICK)
1280                 strcat(buff, "ÊüÃÖ¤¹¤ë¡£");
1281         else if (act & DO_AUTODESTROY)
1282                 strcat(buff, "Ç˲õ¤¹¤ë¡£");
1283         else
1284                 strcat(buff, "½¦¤¦¡£");
1285
1286         if (act & DO_DISPLAY)
1287         {
1288                 if (act & DONT_AUTOPICK)
1289                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'N'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1290                 else if (act & DO_AUTODESTROY)
1291                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'K'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1292                 else
1293                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'M'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1294         }
1295         else
1296                 strcat(buff, "Á´ÂΥޥåפˤÏɽ¼¨¤·¤Ê¤¤");
1297
1298 #else /* JP */
1299
1300         cptr before_str[20], after_str[20], which_str[20], whose_str[20], body_str;
1301         int before_n = 0, after_n = 0, which_n = 0, whose_n = 0;
1302
1303         body_str = "items";
1304
1305         /*** Collecting items ***/
1306         /*** Which can be absorbed into a slot as a bundle ***/
1307         if (IS_FLG(FLG_COLLECTING))
1308                 which_str[which_n++] = "can be absorbed into an existing inventory slot";
1309         
1310         /*** Unidentified ***/
1311         if (IS_FLG(FLG_UNIDENTIFIED))
1312                 before_str[before_n++] = "unidentified";
1313
1314         /*** Identified ***/
1315         if (IS_FLG(FLG_IDENTIFIED))
1316                 before_str[before_n++] = "identified";
1317
1318         /*** *Identified* ***/
1319         if (IS_FLG(FLG_STAR_IDENTIFIED))
1320                 before_str[before_n++] = "fully identified";
1321
1322         /*** Worthless items ***/
1323         if (IS_FLG(FLG_WORTHLESS))
1324         {
1325                 before_str[before_n++] = "worthless";
1326                 which_str[which_n++] = "can not be sold at stores";
1327         }
1328
1329         /*** Artifacto ***/
1330         if (IS_FLG(FLG_ARTIFACT))
1331         {
1332                 before_str[before_n++] = "artifact";
1333         }
1334
1335         /*** Ego ***/
1336         if (IS_FLG(FLG_EGO))
1337         {
1338                 before_str[before_n++] = "ego";
1339         }
1340
1341         /*** Nameless ***/
1342         if (IS_FLG(FLG_NAMELESS))
1343         {
1344                 body_str = "equipment";
1345                 which_str[which_n++] = "is neither ego-item nor artifact";
1346         }
1347
1348         /*** Unaware items ***/
1349         if (IS_FLG(FLG_UNAWARE))
1350         {
1351                 before_str[before_n++] = "unidentified";
1352                 whose_str[whose_n++] = "basic abilities are not known";
1353         }
1354
1355         /*** Dice boosted (weapon of slaying) ***/
1356         if (IS_FLG(FLG_BOOSTED))
1357         {
1358                 body_str = "weapons";
1359                 whose_str[whose_n++] = "damage dice is bigger than normal";
1360         }
1361
1362         /*** Weapons whose dd*ds is more than nn ***/
1363         if (IS_FLG(FLG_MORE_THAN))
1364         {
1365                 static char more_than_desc_str[] =
1366                         "maximum damage from dice is bigger than __";
1367                 body_str = "weapons";
1368                         
1369                 sprintf(more_than_desc_str + sizeof(more_than_desc_str) - 3,
1370                         "%d", entry->dice);
1371                 whose_str[whose_n++] = more_than_desc_str;
1372         }
1373
1374         /*** Items whose magical bonus is more than nn ***/
1375         if (IS_FLG(FLG_MORE_BONUS))
1376         {
1377                 static char more_bonus_desc_str[] =
1378                         "magical bonus is bigger than (+__)";
1379                         
1380                 sprintf(more_bonus_desc_str + sizeof(more_bonus_desc_str) - 4,
1381                         "%d)", entry->bonus);
1382                 whose_str[whose_n++] = more_bonus_desc_str;
1383         }
1384
1385         /*** Wanted monster's corpse/skeletons ***/
1386         if (IS_FLG(FLG_WANTED))
1387         {
1388                 body_str = "corpse or skeletons";
1389                 which_str[which_n++] = "is wanted at the Hunter's Office";
1390         }
1391
1392         /*** Human corpse/skeletons (for Daemon magic) ***/
1393         if (IS_FLG(FLG_HUMAN))
1394         {
1395                 before_str[before_n++] = "humanoid";
1396                 body_str = "corpse or skeletons";
1397                 which_str[which_n++] = "can be used for Daemon magic";
1398         }
1399
1400         /*** Unique monster's corpse/skeletons/statues ***/
1401         if (IS_FLG(FLG_UNIQUE))
1402         {
1403                 before_str[before_n++] = "unique monster's";
1404                 body_str = "corpse or skeletons";
1405         }
1406
1407         /*** Unreadable spellbooks ***/
1408         if (IS_FLG(FLG_UNREADABLE))
1409         {
1410                 body_str = "spellbooks";
1411                 after_str[after_n++] = "of different realms from yours";
1412         }
1413
1414         /*** First realm spellbooks ***/
1415         if (IS_FLG(FLG_REALM1))
1416         {
1417                 body_str = "spellbooks";
1418                 after_str[after_n++] = "of your first realm";
1419         }
1420
1421         /*** Second realm spellbooks ***/
1422         if (IS_FLG(FLG_REALM2))
1423         {
1424                 body_str = "spellbooks";
1425                 after_str[after_n++] = "of your second realm";
1426         }
1427
1428         /*** First rank spellbooks ***/
1429         if (IS_FLG(FLG_FIRST))
1430         {
1431                 before_str[before_n++] = "first one of four";
1432                 body_str = "spellbooks";
1433         }
1434
1435         /*** Second rank spellbooks ***/
1436         if (IS_FLG(FLG_SECOND))
1437         {
1438                 before_str[before_n++] = "second one of four";
1439                 body_str = "spellbooks";
1440         }
1441
1442         /*** Third rank spellbooks ***/
1443         if (IS_FLG(FLG_THIRD))
1444         {
1445                 before_str[before_n++] = "third one of four";
1446                 body_str = "spellbooks";
1447         }
1448
1449         /*** Fourth rank spellbooks ***/
1450         if (IS_FLG(FLG_FOURTH))
1451         {
1452                 before_str[before_n++] = "fourth one of four";
1453                 body_str = "spellbooks";
1454         }
1455
1456         /*** Items ***/
1457         if (IS_FLG(FLG_ITEMS))
1458                 ; /* Nothing to do */
1459         else if (IS_FLG(FLG_WEAPONS))
1460                 body_str = "weapons";
1461         else if (IS_FLG(FLG_ARMORS))
1462                 body_str = "armors";
1463         else if (IS_FLG(FLG_MISSILES))
1464                 body_str = "shots, arrows or crossbow bolts";
1465         else if (IS_FLG(FLG_DEVICES))
1466                 body_str = "scrolls, wands, staves or rods";
1467         else if (IS_FLG(FLG_LIGHTS))
1468                 body_str = "light sources";
1469         else if (IS_FLG(FLG_JUNKS))
1470                 body_str = "junk such as broken sticks";
1471         else if (IS_FLG(FLG_SPELLBOOKS))
1472                 body_str = "spellbooks";
1473         else if (IS_FLG(FLG_HAFTED))
1474                 body_str = "hafted weapons";
1475         else if (IS_FLG(FLG_SHIELDS))
1476                 body_str = "shields";
1477         else if (IS_FLG(FLG_BOWS))
1478                 body_str = "slings, bows or crossbows";
1479         else if (IS_FLG(FLG_RINGS))
1480                 body_str = "rings";
1481         else if (IS_FLG(FLG_AMULETS))
1482                 body_str = "amulets";
1483         else if (IS_FLG(FLG_SUITS))
1484                 body_str = "body armors";
1485         else if (IS_FLG(FLG_CLOAKS))
1486                 body_str = "cloaks";
1487         else if (IS_FLG(FLG_HELMS))
1488                 body_str = "helms or crowns";
1489         else if (IS_FLG(FLG_GLOVES))
1490                 body_str = "gloves";
1491         else if (IS_FLG(FLG_BOOTS))
1492                 body_str = "boots";
1493
1494         /* Prepare a string for item name */
1495         if (*str)
1496         {
1497                 if (*str == '^')
1498                 {
1499                         str++;
1500                         top = TRUE;
1501                         whose_str[whose_n++] = "name is beginning with \"";
1502                 }
1503                 else
1504                         which_str[which_n++] = "have \"";
1505         }
1506
1507
1508         /* Describe action flag */
1509         if (act & DONT_AUTOPICK)
1510                 strcpy(buff, "Leave on floor ");
1511         else if (act & DO_AUTODESTROY)
1512                 strcpy(buff, "Destroy ");
1513         else
1514                 strcpy(buff, "Pickup ");
1515
1516         /* Auto-insctiption */
1517         if (insc)
1518                 strncat(buff, format("and inscribe \"%s\" on ", insc), 80);
1519
1520         /* Adjective */
1521         if (!before_n) 
1522                 strcat(buff, "all ");
1523         else for (i = 0; i < before_n && before_str[i]; i++)
1524         {
1525                 strcat(buff, before_str[i]);
1526                 strcat(buff, " ");
1527         }
1528
1529         /* Item class */
1530         strcat(buff, body_str);
1531
1532         /* of ... */
1533         for (i = 0; i < after_n && after_str[i]; i++)
1534         {
1535                 strcat(buff, " ");
1536                 strcat(buff, after_str[i]);
1537         }
1538
1539         /* which ... */
1540         for (i = 0; i < whose_n && whose_str[i]; i++)
1541         {
1542                 if (i == 0)
1543                         strcat(buff, " whose ");
1544                 else
1545                         strcat(buff, ", and ");
1546
1547                 strcat(buff, whose_str[i]);
1548         }
1549
1550         /* Item name ; whose name is beginning with "str" */
1551         if (*str && top)
1552         {
1553                 strcat(buff, str);
1554                 strcat(buff, "\"");
1555         }
1556
1557         /* whose ..., and which .... */
1558         if (whose_n && which_n)
1559                 strcat(buff, ", and ");
1560
1561         /* which ... */
1562         for (i = 0; i < which_n && which_str[i]; i++)
1563         {
1564                 if (i == 0)
1565                         strcat(buff, " which ");
1566                 else
1567                         strcat(buff, ", and ");
1568
1569                 strcat(buff, which_str[i]);
1570         }
1571
1572         /* Item name ; which have "str" as part of its name */
1573         if (*str && !top)
1574         {
1575                 strncat(buff, str, 80);
1576                 strcat(buff, "\" as part of its name");
1577         }
1578         strcat(buff, ".");
1579
1580         /* Describe whether it will be displayed on the full map or not */
1581         if (act & DO_DISPLAY)
1582         {
1583                 if (act & DONT_AUTOPICK)
1584                         strcat(buff, "  Display these items when you press 'N' in the full map('M').");
1585                 else if (act & DO_AUTODESTROY)
1586                         strcat(buff, "  Display these items when you press 'K' in the full map('M').");
1587                 else
1588                         strcat(buff, "  Display these items when you press 'M' in the full map('M').");
1589         }
1590         else
1591                 strcat(buff, " Not displayed in the full map.");
1592 #endif /* JP */
1593
1594 }
1595
1596
1597 #define MAX_LINES 3000
1598
1599 /*
1600  * Read whole lines of a file to memory
1601  */
1602 static cptr *read_text_lines(cptr filename, bool user)
1603 {
1604         cptr *lines_list = NULL;
1605         FILE *fff;
1606
1607         int lines = 0;
1608         char buf[1024];
1609
1610         if (user)
1611         {
1612                 /* Hack -- drop permissions */
1613                 safe_setuid_drop();
1614                 path_build(buf, 1024, ANGBAND_DIR_USER, filename);
1615         }
1616         else
1617         {
1618                 path_build(buf, 1024, ANGBAND_DIR_PREF, filename);
1619         }
1620         
1621         /* Open the file */
1622         fff = my_fopen(buf, "r");
1623
1624         if (fff)
1625         {
1626                 /* Allocate list of pointers */
1627                 C_MAKE(lines_list, MAX_LINES, cptr);
1628
1629                 /* Parse it */
1630                 while (0 == my_fgets(fff, buf, 1024))
1631                 {
1632                         lines_list[lines++] = string_make(buf);
1633                         if (lines >= MAX_LINES - 1) break;
1634                 }
1635                 if (lines == 0)
1636                         lines_list[0] = string_make("");
1637
1638                 my_fclose(fff);
1639         }
1640
1641         /* Grab priv's */
1642         safe_setuid_grab();
1643
1644         if (!fff) return NULL;
1645         return lines_list;
1646 }
1647
1648 static cptr *read_pickpref_text_lines(void)
1649 {
1650         char buf[1024];
1651         cptr *lines_list;
1652
1653 #ifdef JP
1654         sprintf(buf, "picktype-%s.prf", player_name);
1655 #else
1656         sprintf(buf, "pickpref-%s.prf", player_name);
1657 #endif
1658         lines_list = read_text_lines(buf, TRUE);
1659
1660         if (!lines_list)
1661         {
1662 #ifdef JP
1663                 lines_list = read_text_lines("picktype.prf", TRUE);
1664 #else
1665                 lines_list = read_text_lines("pickpref.prf", TRUE);
1666 #endif
1667         }
1668
1669         if (!lines_list)
1670         {
1671 #ifdef JP
1672                 lines_list = read_text_lines("picktype.prf", FALSE);
1673 #else
1674                 lines_list = read_text_lines("pickpref.prf", FALSE);
1675 #endif
1676         }
1677
1678         if (!lines_list)
1679         {
1680                 /* Allocate list of pointers */
1681                 C_MAKE(lines_list, MAX_LINES, cptr);
1682                 lines_list[0] = string_make("");
1683         }
1684         return lines_list;
1685 }
1686
1687 /*
1688  * Write whole lines of memory to a file.
1689  */
1690 static bool write_text_lines(cptr filename, cptr *lines_list)
1691 {
1692         FILE *fff;
1693
1694         int lines = 0;
1695         char buf[1024];
1696
1697         /* Hack -- drop permissions */
1698         safe_setuid_drop();
1699
1700         /* Build the filename */
1701         path_build(buf, 1024, ANGBAND_DIR_USER, filename);
1702         
1703         /* Open the file */
1704         fff = my_fopen(buf, "w");
1705         if (fff)
1706         {
1707                 for (lines = 0; lines_list[lines]; lines++)
1708                         my_fputs(fff, lines_list[lines], 1024);
1709
1710                 my_fclose(fff);
1711         }
1712
1713         /* Grab priv's */
1714         safe_setuid_grab();
1715
1716         if (!fff) return FALSE;
1717         return TRUE;
1718 }
1719
1720
1721 /*
1722  * Free memory of lines_list.
1723  */
1724 static void free_text_lines(cptr *lines_list)
1725 {
1726         int lines;
1727
1728         for (lines = 0; lines_list[lines]; lines++)
1729                 string_free(lines_list[lines]);
1730
1731         /* free list of pointers */
1732         C_FREE((char **)lines_list, MAX_LINES, char *);
1733 }
1734
1735
1736 /*
1737  * Delete or insert string
1738  */
1739 static void toggle_string(cptr *lines_list, int flg, int y)
1740 {
1741         autopick_type an_entry, *entry = &an_entry;
1742
1743         if (!autopick_new_entry(entry, lines_list[y]))
1744                 return;
1745
1746         string_free(lines_list[y]);
1747         if (IS_FLG(flg)) 
1748                 REM_FLG(flg);
1749         else
1750                 ADD_FLG(flg);
1751
1752         lines_list[y] = autopick_line_from_entry(entry);
1753 }
1754
1755 /*
1756  * Insert return code and split the line
1757  */
1758 static bool insert_return_code(cptr *lines_list, int cx, int cy)
1759 {
1760         char buf[1024];
1761         int i, j, k;
1762
1763         for (k = 0; lines_list[k]; k++)
1764                 /* count number of lines */ ;
1765
1766         if (k >= MAX_LINES - 2) return FALSE;
1767         k--;
1768
1769         /* Move down lines */
1770         for (; cy < k; k--)
1771                 lines_list[k+1] = lines_list[k];
1772
1773         /* Split current line */
1774         for (i = j = 0; lines_list[cy][i] && i < cx; i++)
1775         {
1776 #ifdef JP
1777                 if (iskanji(lines_list[cy][i]))
1778                         buf[j++] = lines_list[cy][i++];
1779 #endif
1780                 buf[j++] = lines_list[cy][i];
1781         }
1782         buf[j] = '\0';
1783         lines_list[cy+1] = string_make(&lines_list[cy][i]);
1784         string_free(lines_list[cy]);
1785         lines_list[cy] = string_make(buf);
1786         return TRUE;
1787 }
1788
1789
1790 /*
1791  * Get auto-picker entry from o_ptr.
1792  */
1793 void autopick_entry_from_object(autopick_type *entry, object_type *o_ptr)
1794 {
1795         char o_name[MAX_NLEN];
1796         object_desc(o_name, o_ptr, FALSE, 0);
1797
1798         entry->name = string_make(o_name);
1799         entry->insc = string_make(quark_str(o_ptr->inscription));
1800         entry->action = DO_AUTOPICK | DO_DISPLAY;
1801         entry->flag[0] = entry->flag[1] = 0L;
1802         entry->dice = 0;
1803
1804         if (!object_aware_p(o_ptr))
1805                 ADD_FLG(FLG_UNAWARE);
1806         if (object_value(o_ptr) <= 0)
1807                 ADD_FLG(FLG_WORTHLESS);
1808
1809         if (object_known_p(o_ptr))
1810         {
1811                 if (o_ptr->name2)
1812                         ADD_FLG(FLG_EGO);
1813                 else if (o_ptr->name1 || o_ptr->art_name)
1814                         ADD_FLG(FLG_ARTIFACT);
1815         }
1816
1817         switch(o_ptr->tval)
1818         {
1819                 object_kind *k_ptr; 
1820         case TV_HAFTED: case TV_POLEARM: case TV_SWORD: case TV_DIGGING:
1821                 k_ptr = &k_info[o_ptr->k_idx];
1822                 if ((o_ptr->dd != k_ptr->dd) || (o_ptr->ds != k_ptr->ds))
1823                         ADD_FLG(FLG_BOOSTED);
1824         }
1825
1826         if (o_ptr->tval == TV_CORPSE && object_is_shoukinkubi(o_ptr))
1827         {
1828                 REM_FLG(FLG_WORTHLESS);
1829                 ADD_FLG(FLG_WANTED);
1830         }
1831
1832         if ((o_ptr->tval == TV_CORPSE || o_ptr->tval == TV_STATUE)
1833             && (r_info[o_ptr->pval].flags1 & RF1_UNIQUE))
1834         {
1835                 REM_FLG(FLG_WORTHLESS);
1836                 ADD_FLG(FLG_UNIQUE);
1837         }
1838
1839         if (o_ptr->tval == TV_CORPSE && strchr("pht", r_info[o_ptr->pval].d_char))
1840         {
1841                 REM_FLG(FLG_WORTHLESS);
1842                 ADD_FLG(FLG_HUMAN);
1843         }
1844
1845         if (o_ptr->tval >= TV_LIFE_BOOK &&
1846             !check_book_realm(o_ptr->tval, o_ptr->sval))
1847                 ADD_FLG(FLG_UNREADABLE);
1848
1849         if (REALM1_BOOK == o_ptr->tval &&
1850             p_ptr->pclass != CLASS_SORCERER &&
1851             p_ptr->pclass != CLASS_RED_MAGE)
1852                 ADD_FLG(FLG_REALM1);
1853
1854         if (REALM2_BOOK == o_ptr->tval &&
1855             p_ptr->pclass != CLASS_SORCERER &&
1856             p_ptr->pclass != CLASS_RED_MAGE)
1857                 ADD_FLG(FLG_REALM2);
1858
1859         if (o_ptr->tval >= TV_LIFE_BOOK && 0 == o_ptr->sval)
1860                 ADD_FLG(FLG_FIRST);
1861         if (o_ptr->tval >= TV_LIFE_BOOK && 1 == o_ptr->sval)
1862                 ADD_FLG(FLG_SECOND);
1863         if (o_ptr->tval >= TV_LIFE_BOOK && 2 == o_ptr->sval)
1864                 ADD_FLG(FLG_THIRD);
1865         if (o_ptr->tval >= TV_LIFE_BOOK && 3 == o_ptr->sval)
1866                 ADD_FLG(FLG_FOURTH);
1867
1868         if (o_ptr->tval == TV_SHOT || o_ptr->tval == TV_BOLT
1869                  || o_ptr->tval == TV_ARROW)
1870                 ADD_FLG(FLG_MISSILES);
1871         else if (o_ptr->tval == TV_SCROLL || o_ptr->tval == TV_STAFF
1872                  || o_ptr->tval == TV_WAND || o_ptr->tval == TV_ROD)
1873                 ADD_FLG(FLG_DEVICES);
1874         else if (o_ptr->tval == TV_LITE)
1875                 ADD_FLG(FLG_LIGHTS);
1876         else if (o_ptr->tval == TV_SKELETON || o_ptr->tval == TV_BOTTLE
1877                  || o_ptr->tval == TV_JUNK || o_ptr->tval == TV_STATUE)
1878                 ADD_FLG(FLG_JUNKS);
1879         else if (o_ptr->tval >= TV_LIFE_BOOK)
1880                 ADD_FLG(FLG_SPELLBOOKS);
1881         else if (o_ptr->tval == TV_HAFTED)
1882                 ADD_FLG(FLG_HAFTED);
1883         else if (o_ptr->tval == TV_POLEARM || o_ptr->tval == TV_SWORD
1884                  || o_ptr->tval == TV_DIGGING)
1885                 ADD_FLG(FLG_WEAPONS);
1886         else if (o_ptr->tval == TV_SHIELD)
1887                 ADD_FLG(FLG_SHIELDS);
1888         else if (o_ptr->tval == TV_BOW)
1889                 ADD_FLG(FLG_BOWS);
1890         else if (o_ptr->tval == TV_RING)
1891                 ADD_FLG(FLG_RINGS);
1892         else if (o_ptr->tval == TV_AMULET)
1893                 ADD_FLG(FLG_AMULETS);
1894         else if (o_ptr->tval == TV_DRAG_ARMOR || o_ptr->tval == TV_HARD_ARMOR ||
1895                  o_ptr->tval == TV_SOFT_ARMOR)
1896                 ADD_FLG(FLG_SUITS);
1897         else if (o_ptr->tval == TV_CLOAK)
1898                 ADD_FLG(FLG_CLOAKS);
1899         else if (o_ptr->tval == TV_HELM)
1900                 ADD_FLG(FLG_HELMS);
1901         else if (o_ptr->tval == TV_GLOVES)
1902                 ADD_FLG(FLG_GLOVES);
1903         else if (o_ptr->tval == TV_BOOTS)
1904                 ADD_FLG(FLG_BOOTS);
1905
1906         return;
1907 }
1908
1909 /*
1910  * Choose an item and get auto-picker entry from it.
1911  */
1912 static bool entry_from_choosed_object(autopick_type *entry)
1913 {
1914         int item;
1915         object_type *o_ptr;
1916         cptr q, s;
1917
1918         /* Get an item */
1919 #ifdef JP
1920         q = "¤É¤Î¥¢¥¤¥Æ¥à¤òÅÐÏ¿¤·¤Þ¤¹¤«? ";
1921         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
1922 #else
1923         q = "Entry which item? ";
1924         s = "You have nothing to entry.";
1925 #endif
1926         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR | USE_EQUIP))) return FALSE;
1927
1928         /* Get the item (in the pack) */
1929         if (item >= 0)
1930         {
1931                 o_ptr = &inventory[item];
1932         }
1933
1934         /* Get the item (on the floor) */
1935         else
1936         {
1937                 o_ptr = &o_list[0 - item];
1938         }
1939
1940         autopick_entry_from_object(entry, o_ptr);
1941         return TRUE;
1942 }
1943
1944
1945 /*
1946  * Initialize auto-picker preference
1947  */
1948 void init_autopicker(void)
1949 {
1950         static const char easy_autopick_inscription[] = "(:=g";
1951         autopick_type entry;
1952         int i;
1953
1954         /* Clear old entries */
1955         for( i = 0; i < max_autopick; i++)
1956                 autopick_free_entry(&autopick_list[i]);
1957
1958         max_autopick = 0;
1959
1960         /* There is always one entry "=g" */
1961         autopick_new_entry(&entry, easy_autopick_inscription);
1962         autopick_list[max_autopick++] = entry;
1963 }
1964
1965
1966 /*
1967  * Description of control commands
1968  */
1969
1970 #define WID_DESC 31
1971
1972 static cptr ctrl_command_desc[] =
1973 {
1974 #ifdef JP
1975 #define LAST_DESTROYED 6
1976         "^P ^N ^B ^F ¾å²¼º¸±¦¤Ë°ÜÆ°",
1977         "^A ^E ¹Ô¤ÎÀèƬ¡¢½ªÃ¼",
1978         "^Q ÆþÎÏ/¥³¥Þ¥ó¥É¥â¡¼¥ÉÀÚ¤êÂؤ¨",
1979         "^R Êѹ¹¤òÁ´¤Æ¼è¤ê¾Ã¤·¤Æ¸µ¤ËÌ᤹",
1980         "------------------------------------",
1981         "^I »ý¤Áʪ/ÁõÈ÷¤«¤éÁªÂò",
1982         "^L",
1983         "^K ¥«¡¼¥½¥ë¤«¤é½ªÃ¼¤Þ¤Çºï½ü",
1984         "^Y ºï½ü(^K)¤·¤¿¹Ô¤òÁÞÆþ",
1985         "^C ¼ï²¡¢¿¦¶È¤Î¾ò·ï¼°¤òÁÞÆþ",
1986         "------------------------------------",
1987         "^S Êѹ¹ (!Ç˲õ/~ÊüÃÖ/½¦¤¦)",
1988         "^G \"(\" Á´ÂΥޥåפÇɽ¼¨¤·¤Ê¤¤",
1989         "^O \"#\" ¼«Æ°¹ï¤ß",
1990         "------------------------------------",
1991         "^U Ì¤´ÕÄê/̤ȽÌÀ/´ÕÄê/*´ÕÄê*",
1992         "^W \"̵²ÁÃͤÎ\"",
1993         "^X ÌµÌÃ/¥¨¥´/¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È",
1994         "^Z \"¼ý½¸Ãæ¤Î\"",
1995         NULL
1996 #else
1997 #define LAST_DESTROYED 6
1998         "^P ^N ^B ^F Move Cursor",
1999         "^A ^E Beginning and End of Line",
2000         "^Q Toggle Insert/Command mode",
2001         "^R Revert to Original File",
2002         "------------------------------------",
2003         "^I Object in Inventry/Equipment",
2004         "^L",
2005         "^K Kill Rest of Line",
2006         "^Y Insert killed(^K) text",
2007         "^C Insert conditional expression",
2008         "------------------------------------",
2009         "^S Toggle(!Destroy/~Leave/Pick)",
2010         "^G \"(\" No display in the 'M'ap",
2011         "^O \"#\" Auto-Inscribe",
2012         "------------------------------------",
2013         "^U Toggle 'identified' state",
2014         "^W \"worthless\"",
2015         "^X Toggle nameless/ego/artifact",
2016         "^Z \"collecting\"",
2017         NULL
2018 #endif
2019 };
2020
2021
2022 #define MAX_YANK 1024
2023 #define MAX_LINELEN 1024
2024 #define DIRTY_ALL 0x01
2025 #define DIRTY_COMMAND 0x02
2026 #define DIRTY_MODE 0x04
2027 #define DIRTY_SCREEN 0x04
2028
2029 /*
2030  * In-game editor of Object Auto-picker/Destoryer
2031  */
2032 void do_cmd_edit_autopick(void)
2033 {
2034         static int cx = 0, cy = 0;
2035         static int upper = 0, left = 0;
2036
2037         cptr last_destroyed;
2038         char last_destroyed_command[WID_DESC+3];
2039         char yank_buf[MAX_YANK];
2040         char classrace[80];
2041         autopick_type an_entry, *entry = &an_entry;
2042         char buf[MAX_LINELEN];
2043         cptr *lines_list;
2044
2045         int i, j, k, len;
2046         cptr tmp;
2047
2048         int old_upper = -1, old_left = -1;
2049         int old_cy = -1;
2050         int key = -1, old_key;
2051
2052         bool edit_mode = FALSE;
2053
2054         byte dirty_flags = DIRTY_ALL | DIRTY_COMMAND | DIRTY_MODE;
2055         int dirty_line = -1;
2056
2057         int wid, hgt, old_wid = -1, old_hgt = -1;
2058
2059         /* Free old entries */
2060         init_autopicker();
2061
2062         /* Name of the Last Destroyed Item */
2063         last_destroyed = autopick_line_from_entry(&autopick_entry_last_destroyed);
2064
2065         /* Command Description of the Last Destroyed Item */
2066         if (last_destroyed)
2067         {
2068                 strcpy(last_destroyed_command, "^L \"");
2069                 strncpy(last_destroyed_command + 4, last_destroyed, WID_DESC-4);
2070                 last_destroyed_command[WID_DESC+2] = '\0';
2071         }
2072         else
2073         {
2074 #ifdef JP
2075                 strcpy(last_destroyed_command, "^L ºÇ¸å¤Ë¼«Æ°Ç˲õ¤·¤¿¥¢¥¤¥Æ¥à̾");
2076 #else
2077                 strcpy(last_destroyed_command, "^L Last destroyed object");
2078 #endif
2079         }
2080         ctrl_command_desc[LAST_DESTROYED] = last_destroyed_command;
2081
2082         /* Conditional Expression for Class and Race */
2083         sprintf(classrace, "?:[AND [EQU $RACE %s] [EQU $CLASS %s]]", 
2084 #ifdef JP
2085                 rp_ptr->E_title, cp_ptr->E_title
2086 #else
2087                 rp_ptr->title, cp_ptr->title
2088 #endif
2089                 );
2090
2091         /* Clear yank buffer */
2092         yank_buf[0] = '\0';
2093
2094         /* Read or initialize whole text */
2095         lines_list = read_pickpref_text_lines();
2096
2097         /* Reset cursor position if needed */
2098         for (i = 0; i < cy; i++)
2099         {
2100                 if (!lines_list[i])
2101                 {
2102                         cy = cx = 0;
2103                         break;
2104                 }
2105         }
2106
2107         /* Save the screen */
2108         screen_save();
2109
2110         /* Process requests until done */
2111         while (1)
2112         {
2113                 /* Get size */
2114                 Term_get_size(&wid, &hgt);
2115
2116 #ifdef JP
2117                 /* Don't let cursor at second byte of kanji */
2118                 for (i = 0; lines_list[cy][i]; i++)
2119                         if (iskanji(lines_list[cy][i]))
2120                         {
2121                                 i++;
2122                                 if (i == cx)
2123                                 {
2124                                         cx--;
2125                                         break;
2126                                 }
2127                         }
2128 #endif
2129
2130                 /* Scroll if necessary */
2131                 if (cy < upper || upper + hgt - 4 <= cy)
2132                         upper = cy - (hgt-4)/2;
2133                 if (upper < 0)
2134                         upper = 0;
2135                 if ((cx < left + 10 && left > 0) || left + wid - WID_DESC - 5 <= cx)
2136                         left = cx - (wid - WID_DESC)*2/3;
2137                 if (left < 0)
2138                         left = 0;
2139
2140                 /* Redraw whole window after resize */
2141                 if (old_wid != wid || old_hgt != hgt)
2142                         dirty_flags |= DIRTY_SCREEN;
2143
2144                 /* Redraw all text after scroll */
2145                 else if (old_upper != upper || old_left != left)
2146                         dirty_flags |= DIRTY_ALL;
2147
2148
2149                 if (dirty_flags & DIRTY_SCREEN)
2150                 {
2151                         dirty_flags = DIRTY_ALL | DIRTY_COMMAND | DIRTY_MODE;
2152
2153                         /* Clear screen */
2154                         Term_clear();
2155                 }
2156
2157                 if (dirty_flags & DIRTY_COMMAND)
2158                 {
2159                         /* Display control command */
2160                         for (i = 0; ctrl_command_desc[i]; i++)
2161                                 Term_putstr(wid - WID_DESC, i + 1, WID_DESC, TERM_WHITE, ctrl_command_desc[i]);
2162                 }
2163
2164                 /* Redraw mode line */
2165                 if (dirty_flags & DIRTY_MODE)
2166                 {
2167                         int sepa_length = wid - WID_DESC;
2168
2169                         /* Separator */
2170                         for (i = 0; i < sepa_length; i++)
2171                                 buf[i] = '-';
2172                         buf[i] = '\0';
2173
2174                         /* Mode line */
2175                         if (edit_mode)
2176                                 strncpy(buf + sepa_length - 21, " (INSERT MODE)  ", 16);
2177                         else
2178                                 strncpy(buf + sepa_length - 21, " (COMMAND MODE) ", 16);
2179
2180                         Term_putstr(0, hgt - 3, sepa_length, (byte) (edit_mode ? TERM_YELLOW : TERM_WHITE), buf);
2181                 }
2182                 
2183                 /* Dump up to 20, or hgt-4, lines of messages */
2184                 for (i = 0; i < hgt - 4; i++)
2185                 {
2186                         int leftcol = 0;
2187                         cptr msg;
2188
2189                         /* clean or dirty? */
2190                         if (!(dirty_flags & DIRTY_ALL) && (dirty_line != upper+i))
2191                                 continue;
2192
2193                         msg = lines_list[upper+i];
2194                         if (!msg) break;
2195
2196                         /* Apply horizontal scroll */
2197                         for (j = 0; *msg; msg++, j++)
2198                         {
2199                                 if (j == left) break;
2200 #ifdef JP
2201                                 if (j > left)
2202                                 {
2203                                         leftcol = 1;
2204                                         break;
2205                                 }
2206                                 if (iskanji(*msg))
2207                                 {
2208                                         msg++;
2209                                         j++;
2210                                 }
2211 #endif
2212                         }
2213
2214                         /* Erase line */
2215                         Term_erase(0, i + 1, wid - WID_DESC);
2216
2217                         /* Dump the messages, bottom to top */
2218                         Term_putstr(leftcol, i + 1, wid - WID_DESC - 1, TERM_WHITE, msg);
2219                 }
2220
2221                 for (; i < hgt - 4; i++)
2222                 {
2223                         /* Erase line */
2224                         Term_erase(0, i + 1, wid - WID_DESC);
2225                 }
2226
2227                 /* Display header line */
2228 #ifdef JP
2229                 if (edit_mode)
2230                         prt("^Q ESC ¤Ç¥³¥Þ¥ó¥É¥â¡¼¥É¤Ø°Ü¹Ô¡¢Ä̾ï¤Îʸ»ú¤Ï¤½¤Î¤Þ¤ÞÆþÎÏ", 0, 0);
2231                 else
2232                         prt("q _ ¤Ç½ªÎ»¡¢hjkl2468 ¤Ç°ÜÆ°¡¢^Q a i ¤ÇÆþÎϥ⡼¥É", 0, 0);
2233 #else   
2234                 if (edit_mode)
2235                         prt("Press ^Q ESC to command mode, any letters to insert", 0, 0);
2236                 else
2237                         prt("Press q _ to quit, hjkl2468 to move, ^Q a i to insert mode", 0, 0);
2238 #endif
2239                 /* Display current position */
2240                 prt (format("(%d,%d)", cx, cy), 0, 70);
2241
2242                 /* Display information when updated */
2243                 if (old_cy != cy || (dirty_flags & DIRTY_ALL) || dirty_line == cy)
2244                 {
2245                         /* Clear information line */
2246                         Term_erase(0, hgt - 3 + 1, wid);
2247                         Term_erase(0, hgt - 3 + 2, wid);
2248
2249                         /* Display information */
2250                         if (lines_list[cy][0] == '#')
2251                         {
2252 #ifdef JP
2253                                 prt("¤³¤Î¹Ô¤Ï¥³¥á¥ó¥È¤Ç¤¹¡£", hgt - 3 + 1, 0);
2254 #else
2255                                 prt("This line is comment.", hgt - 3 + 1, 0);
2256 #endif
2257                         }
2258                         else if (lines_list[cy][1] == ':')
2259                         {
2260                                 switch(lines_list[cy][0])
2261                                 {
2262                                 case '?':
2263 #ifdef JP
2264                                         prt("¤³¤Î¹Ô¤Ï¾ò·ïʬ´ô¼°¤Ç¤¹¡£", hgt - 3 + 1, 0);
2265 #else
2266                                         prt("This line is Conditional Expression.", hgt - 3 + 1, 0);
2267 #endif
2268                                         break;
2269                                 case 'A':
2270 #ifdef JP
2271                                         prt("¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¼Â¹ÔÆâÍƤòÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2272 #else
2273                                         prt("This line defines Macro action.", hgt - 3 + 1, 0);
2274 #endif
2275                                         break;
2276                                 case 'P':
2277 #ifdef JP
2278                                         prt("¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¥È¥ê¥¬¡¼¡¦¥­¡¼¤òÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2279 #else
2280                                         prt("This line defines Macro trigger key.", hgt - 3 + 1, 0);
2281 #endif
2282                                         break;
2283                                 case 'C':
2284 #ifdef JP
2285                                         prt("¤³¤Î¹Ô¤Ï¥­¡¼ÇÛÃÖ¤òÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2286 #else
2287                                         prt("This line defines Keymap.", hgt - 3 + 1, 0);
2288 #endif
2289                                         break;
2290                                 }
2291                         }
2292
2293                         /* Get description of an autopicker preference line */
2294                         else if (autopick_new_entry(entry, lines_list[cy]))
2295                         {
2296                                 char temp[1024];
2297                                 cptr t;
2298
2299                                 describe_autopick(buf, entry);
2300
2301                                 roff_to_buf(buf, 81, temp);
2302                                 t = temp;
2303                                 for (i = 0; i< 2; i++)
2304                                 {
2305                                         if(t[0] == 0)
2306                                                 break; 
2307                                         else
2308                                         {
2309                                                 prt(t, hgt - 3 + 1 + i, 0);
2310                                                 t += strlen(t) + 1;
2311                                         }
2312                                 }
2313                                 autopick_free_entry(entry);
2314                         }
2315                 }
2316
2317                 /* Place cursor */
2318                 Term_gotoxy(cx - left, cy - upper + 1);
2319
2320                 /* Now clean */
2321                 dirty_flags = 0;
2322                 dirty_line = -1;
2323
2324                 /* Save old key and location */
2325                 old_cy = cy;
2326                 old_key = key;
2327                 old_upper = upper;
2328                 old_left = left;
2329                 old_wid = wid;
2330                 old_hgt = hgt;
2331
2332                 /* Do not process macros except special keys */
2333                 inkey_special = TRUE;
2334
2335                 /* Get a command */
2336                 key = inkey();
2337
2338                 if (edit_mode)
2339                 {
2340                         if (key == ESCAPE)
2341                         {
2342                                 edit_mode = FALSE;
2343
2344                                 /* Mode line is now dirty */
2345                                 dirty_flags |= DIRTY_MODE;
2346                         }
2347
2348                         /* Insert a character */
2349                         else if (!iscntrl(key&0xff))
2350                         {
2351                                 int next;
2352
2353                                 /* Save preceding string */
2354                                 for (i = j = 0; lines_list[cy][i] && i < cx; i++)
2355                                         buf[j++] = lines_list[cy][i];
2356
2357                                 /* Add a character */
2358 #ifdef JP
2359                                 if (iskanji(key))
2360                                 {
2361                                         inkey_base = TRUE;
2362                                         next = inkey();
2363                                         if (j+2 < MAX_LINELEN)
2364                                         {
2365                                                 buf[j++] = key;
2366                                                 buf[j++] = next;
2367                                                 cx += 2;
2368                                         }
2369                                         else
2370                                                 bell();
2371                                 }
2372                                 else
2373 #endif
2374                                 {
2375                                         if (j+1 < MAX_LINELEN)
2376                                                 buf[j++] = key;
2377                                         cx++;
2378                                 }
2379
2380                                 /* Add following */
2381                                 for (; lines_list[cy][i] && j + 1 < MAX_LINELEN; i++)
2382                                         buf[j++] = lines_list[cy][i];
2383                                 buf[j] = '\0';
2384
2385                                 /* Replace current line with new line */
2386                                 string_free(lines_list[cy]);
2387                                 lines_list[cy] = string_make(buf);
2388
2389                                 /* Move to correct collumn */
2390                                 len = strlen(lines_list[cy]);
2391                                 if (len < cx) cx = len;
2392
2393                                 /* Now dirty */
2394                                 dirty_line = cy;
2395                         }
2396                 }
2397                 else
2398                 {
2399                         /* Exit on 'q' */
2400                         if (key == 'q' || key == '_') break;
2401
2402                         switch(key)
2403                         {
2404                         case 'a': case 'i':
2405                                 edit_mode = TRUE;
2406
2407                                 /* Mode line is now dirty */
2408                                 dirty_flags |= DIRTY_MODE;
2409                                 break;
2410                         case '~':
2411                                 if (!autopick_new_entry(entry, lines_list[cy]))
2412                                         break;
2413                                 string_free(lines_list[cy]);
2414
2415                                 entry->action &= ~DO_AUTODESTROY;
2416                                 if (entry->action & DO_AUTOPICK)
2417                                 {
2418                                         entry->action &= ~DO_AUTOPICK;
2419                                         entry->action |= DONT_AUTOPICK;
2420                                 }
2421                                 else 
2422                                 {
2423                                         entry->action &= ~DONT_AUTOPICK;
2424                                         entry->action |= DO_AUTOPICK;
2425                                 }
2426
2427                                 lines_list[cy] = autopick_line_from_entry(entry);
2428
2429                                 /* Now dirty */
2430                                 dirty_line = cy;
2431                                 break;
2432                         case '!':
2433                                 if (!autopick_new_entry(entry, lines_list[cy]))
2434                                         break;
2435                                 string_free(lines_list[cy]);
2436
2437                                 entry->action &= ~DONT_AUTOPICK;
2438                                 if (entry->action & DO_AUTOPICK)
2439                                 {
2440                                         entry->action &= ~DO_AUTOPICK;
2441                                         entry->action |= DO_AUTODESTROY;
2442                                 }
2443                                 else 
2444                                 {
2445                                         entry->action &= ~DO_AUTODESTROY;
2446                                         entry->action |= DO_AUTOPICK;
2447                                 }
2448
2449                                 lines_list[cy] = autopick_line_from_entry(entry);
2450
2451                                 /* Now dirty */
2452                                 dirty_line = cy;
2453                                 break;
2454                         case '(':
2455                                 key = KTRL('g');
2456                                 break;
2457                         case '#':
2458                         case '{':
2459                                 key = KTRL('o');
2460                                 break;
2461                         case 'h': case '4':
2462                                 key = KTRL('b');
2463                                 break;
2464                         case 'l': case '6':
2465                                 key = KTRL('f');
2466                                 break;
2467                         case 'j': case '2':
2468                                 key = KTRL('n');
2469                                 break;
2470                         case 'k': case '8':
2471                                 key = KTRL('p');
2472                                 break;
2473                         case ' ':
2474                                 while (cy < upper + hgt-4 && lines_list[cy + 1])
2475                                         cy++;
2476                                 upper = cy;
2477                                 break;
2478                         case '-': case 'b':
2479                                 while (0 < cy && upper <= cy)
2480                                         cy--;
2481                                 while (0 < upper && cy + 1 < upper + hgt - 4)
2482                                         upper--;
2483                                 break;
2484                         }
2485                 }
2486
2487                 switch(key)
2488                 {
2489                 case KTRL('a'):
2490                         /* Beginning of line */
2491                         cx = 0;
2492                         break;
2493                 case KTRL('b'):
2494                         /* Back */
2495                         if (0 < cx)
2496                         {
2497                                 cx--;
2498                                 len = strlen(lines_list[cy]);
2499                                 if (len < cx) cx = len;
2500                         }
2501                         else if (cy > 0)
2502                         {
2503                                 cy--;
2504                                 cx = strlen(lines_list[cy]);
2505                         }
2506                         break;
2507                 case KTRL('c'):
2508                         /* Insert a conditinal expression line */
2509                         insert_return_code(lines_list, 0, cy);
2510                         lines_list[cy] = string_make(classrace);
2511                         cy++;
2512                         insert_return_code(lines_list, 0, cy);
2513                         lines_list[cy] = string_make("?:1");
2514                         cx = 0;
2515
2516                         /* Now dirty */
2517                         dirty_flags |= DIRTY_ALL;
2518                         break;
2519                 case KTRL('e'):
2520                         /* End of line */
2521                         cx = strlen(lines_list[cy]);
2522                         break;
2523                 case KTRL('f'):
2524                         /* Forward */
2525 #ifdef JP
2526                         if (iskanji(lines_list[cy][cx])) cx++;
2527 #endif
2528                         cx++;
2529                         len = strlen(lines_list[cy]);
2530                         if (len < cx)
2531                         {
2532                                 if (lines_list[cy + 1])
2533                                 {
2534                                         cy++;
2535                                         cx = 0;
2536                                 }
2537                                 else
2538                                         cx = len;
2539                         }
2540                         break;
2541                 case KTRL('g'):
2542                         /* Toggle display on the 'M'ap */
2543                         if (!autopick_new_entry(entry, lines_list[cy]))
2544                                 break;
2545                         string_free(lines_list[cy]);
2546
2547                         if (entry->action & DO_DISPLAY)
2548                         {
2549                                 entry->action &= ~DO_DISPLAY;
2550                                 cx++;
2551                         }
2552                         else
2553                         {
2554                                 entry->action |= DO_DISPLAY;
2555                                 if (cx > 0) cx--;
2556                         }
2557
2558                         lines_list[cy] = autopick_line_from_entry(entry);
2559
2560                         /* Now dirty */
2561                         dirty_line = cy;
2562                         break;
2563                 case KTRL('i'):
2564                         /* Insert choosen item name */
2565                         if (!entry_from_choosed_object(entry))
2566                         {
2567                                 /* Now dirty because of item/equip menu */
2568                                 dirty_flags |= DIRTY_SCREEN;
2569                                 break;
2570                         }
2571                         tmp = autopick_line_from_entry(entry);
2572                         autopick_free_entry(entry);
2573                         if (tmp)
2574                         {
2575                                 insert_return_code(lines_list, 0, cy);
2576                                 lines_list[cy] = tmp;
2577                                 cx = 0;
2578
2579                                 /* Now dirty because of item/equip menu */
2580                                 dirty_flags |= DIRTY_SCREEN;
2581                         }
2582                         break;
2583                 case KTRL('l'):
2584                         /* Insert a name of last destroyed item */
2585                         if (last_destroyed)
2586                         {
2587                                 insert_return_code(lines_list, 0, cy);
2588                                 lines_list[cy] = string_make(last_destroyed);
2589                                 cx = 0;
2590
2591                                 /* Now dirty */
2592                                 dirty_flags |= DIRTY_ALL;
2593                         }
2594                         break;
2595                 case '\n': case '\r':
2596                         /* Split a line or insert end of line */
2597                         insert_return_code(lines_list, cx, cy);
2598                         cy++;
2599                         cx = 0;
2600
2601                         /* Now dirty */
2602                         dirty_flags |= DIRTY_ALL;
2603                         break;
2604                 case KTRL('n'):
2605                         /* Next line */
2606                         if (lines_list[cy + 1]) cy++;
2607                         break;
2608                 case KTRL('o'):
2609                         /* Prepare to write auto-inscription text */
2610                         if (!autopick_new_entry(entry, lines_list[cy]))
2611                                 break;
2612                         string_free(lines_list[cy]);
2613
2614                         if (!entry->insc) entry->insc = string_make("");
2615
2616                         lines_list[cy] = autopick_line_from_entry(entry);
2617
2618                         /* Move to collumn for auto inscription */
2619                         for (cx = 0; lines_list[cy][cx]; cx++)
2620                                 if (lines_list[cy][cx] == '#') break;
2621                         cx++;
2622                         edit_mode = TRUE;
2623
2624                         /* Now dirty */
2625                         dirty_line = cy;
2626                         dirty_flags |= DIRTY_MODE;
2627                         break;
2628                 case KTRL('p'):
2629                         /* Previous line */
2630                         if (cy > 0) cy--;
2631                         break;
2632                 case KTRL('q'):
2633                         /* Change mode */
2634                         edit_mode = !edit_mode;
2635                         
2636                         /* Mode line is now dirty */
2637                         dirty_flags |= DIRTY_MODE;
2638                         break;
2639                 case KTRL('r'):
2640                         /* Revert to original */
2641 #ifdef JP
2642                         if (!get_check("Á´¤Æ¤ÎÊѹ¹¤òÇË´þ¤·¤Æ¸µ¤Î¾õÂÖ¤ËÌᤷ¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡© "))
2643 #else
2644                         if (!get_check("Discard all change and revert to original file. Are you sure? "))
2645 #endif
2646                                 break;
2647
2648                         free_text_lines(lines_list);
2649                         lines_list = read_pickpref_text_lines();
2650                         dirty_flags |= DIRTY_ALL | DIRTY_MODE;
2651                         cx = cy = 0;
2652                         edit_mode = FALSE;
2653                         break;
2654                 case KTRL('s'):
2655                         /* Rotate action; pickup/destroy/leave */
2656                         if (!autopick_new_entry(entry, lines_list[cy]))
2657                                 break;
2658                         string_free(lines_list[cy]);
2659
2660                         if (entry->action & DO_AUTOPICK)
2661                         {
2662                                 entry->action &= ~DO_AUTOPICK;
2663                                 entry->action |= DO_AUTODESTROY;
2664                         }
2665                         else if (entry->action & DO_AUTODESTROY)
2666                         {
2667                                 entry->action &= ~DO_AUTODESTROY;
2668                                 entry->action |= DONT_AUTOPICK;
2669                         }
2670                         else if (entry->action & DONT_AUTOPICK)
2671                         {
2672                                 entry->action &= ~DONT_AUTOPICK;
2673                                 entry->action |= DO_AUTOPICK;
2674                         }
2675
2676                         lines_list[cy] = autopick_line_from_entry(entry);
2677                         /* Now dirty */
2678                         dirty_line = cy;
2679
2680                         break;
2681                 case KTRL('t'):
2682                         /* Nothing */
2683                         break;
2684                 case KTRL('u'):
2685                         /* Rotate identify-state; identified/unidentified/... */
2686                         if (!autopick_new_entry(entry, lines_list[cy]))
2687                                 break;
2688                         string_free(lines_list[cy]);
2689
2690                         if (IS_FLG(FLG_UNIDENTIFIED)) 
2691                         {
2692                                 REM_FLG(FLG_UNIDENTIFIED);
2693                                 ADD_FLG(FLG_UNAWARE);
2694                                 REM_FLG(FLG_IDENTIFIED);
2695                                 REM_FLG(FLG_STAR_IDENTIFIED);
2696                         }
2697                         else if (IS_FLG(FLG_UNAWARE)) 
2698                         {
2699                                 REM_FLG(FLG_UNIDENTIFIED);
2700                                 REM_FLG(FLG_UNAWARE);
2701                                 ADD_FLG(FLG_IDENTIFIED);
2702                                 REM_FLG(FLG_STAR_IDENTIFIED);
2703                         }
2704                         else if (IS_FLG(FLG_STAR_IDENTIFIED)) 
2705                         {
2706                                 REM_FLG(FLG_UNIDENTIFIED);
2707                                 REM_FLG(FLG_UNAWARE);
2708                                 REM_FLG(FLG_IDENTIFIED);
2709                                 REM_FLG(FLG_STAR_IDENTIFIED);
2710                         }
2711                         else if (IS_FLG(FLG_IDENTIFIED)) 
2712                         {
2713                                 REM_FLG(FLG_UNIDENTIFIED);
2714                                 REM_FLG(FLG_UNAWARE);
2715                                 REM_FLG(FLG_IDENTIFIED);
2716                                 ADD_FLG(FLG_STAR_IDENTIFIED);
2717                         }
2718                         else
2719                         {
2720                                 ADD_FLG(FLG_UNIDENTIFIED);
2721                                 REM_FLG(FLG_UNAWARE);
2722                                 REM_FLG(FLG_IDENTIFIED);
2723                                 REM_FLG(FLG_STAR_IDENTIFIED);
2724                         }
2725
2726                         lines_list[cy] = autopick_line_from_entry(entry);
2727
2728                         /* Now dirty */
2729                         dirty_line = cy;
2730                         break;
2731                 case KTRL('v'):
2732                         /* Scroll up */
2733                         while (cy < upper + hgt-4 && lines_list[cy + 1])
2734                                 cy++;
2735                         upper = cy;
2736                         break;
2737                 case KTRL('w'):
2738                         /* Toggle 'worthless' */
2739                         toggle_string(lines_list, FLG_WORTHLESS, cy);
2740                         /* Now dirty */
2741                         dirty_line = cy;
2742                         break;
2743                 case KTRL('x'):
2744                         /* Rotate within nameless, ego, artifact */
2745                         if (!autopick_new_entry(entry, lines_list[cy]))
2746                                 break;
2747                         string_free(lines_list[cy]);
2748
2749                         if (IS_FLG(FLG_NAMELESS)) 
2750                         {
2751                                 REM_FLG(FLG_NAMELESS);
2752                                 ADD_FLG(FLG_EGO);
2753                                 REM_FLG(FLG_ARTIFACT);
2754                         }
2755                         else if (IS_FLG(FLG_EGO)) 
2756                         {
2757                                 REM_FLG(FLG_NAMELESS);
2758                                 REM_FLG(FLG_EGO);
2759                                 ADD_FLG(FLG_ARTIFACT);
2760                         }
2761                         else if (IS_FLG(FLG_ARTIFACT)) 
2762                         {
2763                                 REM_FLG(FLG_NAMELESS);
2764                                 REM_FLG(FLG_EGO);
2765                                 REM_FLG(FLG_ARTIFACT);
2766                         }
2767                         else
2768                         {
2769                                 ADD_FLG(FLG_NAMELESS);
2770                                 REM_FLG(FLG_EGO);
2771                                 REM_FLG(FLG_ARTIFACT);
2772                         }
2773
2774                         lines_list[cy] = autopick_line_from_entry(entry);
2775
2776                         /* Now dirty */
2777                         dirty_line = cy;
2778                         break;
2779
2780                 case KTRL('y'):
2781                         /* Paste killed text */
2782                         if (strlen(yank_buf))
2783                         {
2784                                 for (j = 0; yank_buf[j]; j += strlen(yank_buf + j) + 1)
2785                                 {
2786                                         int k = j;
2787
2788                                         /* Split current line */
2789                                         insert_return_code(lines_list, cx, cy);
2790
2791                                         /* Paste yank buffer */
2792                                         for(i = 0; lines_list[cy][i]; i++)
2793                                                 buf[i] = lines_list[cy][i];
2794                                         while (yank_buf[k] && i < MAX_LINELEN)
2795                                                 buf[i++] = yank_buf[k++];
2796                                         buf[i] = '\0';
2797
2798                                         string_free(lines_list[cy]);
2799                                         lines_list[cy] = string_make(buf);
2800
2801                                         /* Move to the beggining of next line */
2802                                         cx = 0;
2803                                         cy++;
2804                                 }
2805
2806                                 /* Now dirty */
2807                                 dirty_flags |= DIRTY_ALL;
2808                         }
2809                         break;
2810                 case KTRL('z'):
2811                         /* Toggle 'collecting' */
2812                         toggle_string(lines_list, FLG_COLLECTING, cy);
2813                         /* Now dirty */
2814                         dirty_line = cy;
2815                         break;
2816
2817                 case KTRL('k'):
2818                         /* Kill rest of line */
2819                         if (lines_list[cy][0] != '\0' && (uint)cx < strlen(lines_list[cy]))
2820                         {
2821                                 /* Save preceding string */
2822                                 for (i = 0; lines_list[cy][i] && i < cx; i++)
2823                                 {
2824 #ifdef JP
2825                                         if (iskanji(lines_list[cy][i]))
2826                                         {
2827                                                 buf[i] = lines_list[cy][i];
2828                                                 i++;
2829                                         }
2830 #endif
2831                                         buf[i] = lines_list[cy][i];
2832                                 }
2833                                 buf[i] = '\0';
2834
2835                                 j = 0;
2836                                 if (old_key == KTRL('k'))
2837                                         while (yank_buf[j])
2838                                                 j += strlen(yank_buf + j) + 1;
2839
2840                                 /* Copy following to yank buffer */
2841                                 while (lines_list[cy][i] && j < MAX_YANK - 2)
2842                                         yank_buf[j++] = lines_list[cy][i++];
2843                                 yank_buf[j++] = '\0';
2844                                 yank_buf[j] = '\0';
2845
2846                                 /* Replace current line with 'preceding string' */
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
2863                         /* fall through */
2864
2865                 case '\010':
2866                         /* BACK SPACE */
2867                         len = strlen(lines_list[cy]);
2868                         if (len < cx)
2869                         {
2870                                 if (lines_list[cy + 1])
2871                                 {
2872                                         cy++;
2873                                         cx = 0;
2874                                 }
2875                                 else
2876                                 {
2877                                         cx = len;
2878                                         break;
2879                                 }
2880                         }
2881
2882                         if (cx == 0)
2883                         {
2884                                 /* delete a return code and union two lines */
2885                                 if (cy == 0) break;
2886                                 cx = strlen(lines_list[cy-1]);
2887                                 strcpy(buf, lines_list[cy-1]);
2888                                 strcat(buf, lines_list[cy]);
2889                                 string_free(lines_list[cy-1]);
2890                                 string_free(lines_list[cy]);
2891                                 lines_list[cy-1] = string_make(buf);
2892                                 for (i = cy; lines_list[i+1]; i++)
2893                                         lines_list[i] = lines_list[i+1];
2894                                 lines_list[i] = NULL;
2895                                 cy--;
2896
2897                                 /* Now dirty */
2898                                 dirty_flags |= DIRTY_ALL;
2899                                 break;
2900                         }
2901
2902                         for (i = j = 0; lines_list[cy][i] && i < cx; i++)
2903                         {
2904                                 k = j;
2905 #ifdef JP
2906                                 if (iskanji(lines_list[cy][i]))
2907                                         buf[j++] = lines_list[cy][i++];
2908 #endif
2909                                 buf[j++] = lines_list[cy][i];
2910                         }
2911                         while (j > k)
2912                         {
2913                                 cx--;
2914                                 j--;
2915                         }
2916                         for (; lines_list[cy][i]; i++)
2917                                 buf[j++] = lines_list[cy][i];
2918                         buf[j] = '\0';
2919                         string_free(lines_list[cy]);
2920                         lines_list[cy] = string_make(buf);
2921
2922                         /* Now dirty */
2923                         dirty_line = cy;
2924                         break;
2925                 }
2926
2927         } /* while (1) */
2928
2929         /* Restore the screen */
2930         screen_load();
2931
2932 #ifdef JP
2933         sprintf(buf, "picktype-%s.prf", player_name);
2934 #else
2935         sprintf(buf, "pickpref-%s.prf", player_name);
2936 #endif
2937         write_text_lines(buf, lines_list);
2938         free_text_lines(lines_list);
2939
2940         string_free(last_destroyed);
2941
2942         /* Reload autopick pref */
2943         process_pickpref_file(buf);
2944 }