OSDN Git Service

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