OSDN Git Service

0f8e214de73af1d2414a824e7508fb5d4ffaa0e1
[hengband/hengband.git] / src / cmd / cmd-autopick.c
1 #include "angband.h"
2 #include "cmd/cmd-autopick.h"
3 #include "cmd/cmd-save.h"
4 #include "autopick/autopick-util.h"
5 #include "autopick/autopick-commands-table.h"
6 #include "autopick/autopick-dirty-flags.h"
7 #include "autopick/autopick-initializer.h"
8 #include "autopick/autopick-entry.h"
9 #include "autopick/autopick-drawer.h"
10 #include "autopick/autopick-reader-writer.h"
11 #include "autopick/autopick-command-menu.h"
12 #include "autopick/autopick-editor-command.h"
13 #include "autopick/autopick-editor-util.h"
14 #include "autopick/autopick-inserter-killer.h"
15 #include "io/read-pref-file.h"
16 #include "world.h"
17 #include "view/display-main-window.h" // 暫定。後で消す.
18
19 /*
20  * Check special key code and get a movement command id
21  */
22 static int analyze_move_key(text_body_type *tb, int skey)
23 {
24         int com_id;
25         if (!(skey & SKEY_MASK)) return 0;
26
27         switch (skey & ~SKEY_MOD_MASK)
28         {
29         case SKEY_DOWN:   com_id = EC_DOWN;   break;
30         case SKEY_LEFT:   com_id = EC_LEFT;   break;
31         case SKEY_RIGHT:  com_id = EC_RIGHT;  break;
32         case SKEY_UP:     com_id = EC_UP;     break;
33         case SKEY_PGUP:   com_id = EC_PGUP;   break;
34         case SKEY_PGDOWN: com_id = EC_PGDOWN; break;
35         case SKEY_TOP:    com_id = EC_TOP;    break;
36         case SKEY_BOTTOM: com_id = EC_BOTTOM; break;
37         default:
38                 return 0;
39         }
40
41         if (!(skey & SKEY_MOD_SHIFT))
42         {
43                 /*
44                  * Un-shifted cursor keys cancells
45                  * selection created by shift+cursor.
46                  */
47                 if (tb->mark & MARK_BY_SHIFT)
48                 {
49                         tb->mark = 0;
50                         tb->dirty_flags |= DIRTY_ALL;
51                 }
52
53                 return com_id;
54         }
55
56         if (tb->mark) return com_id;
57
58         int len = strlen(tb->lines_list[tb->cy]);
59         tb->mark = MARK_MARK | MARK_BY_SHIFT;
60         tb->my = tb->cy;
61         tb->mx = tb->cx;
62         if (tb->cx > len) tb->mx = len;
63
64         if (com_id == EC_UP || com_id == EC_DOWN)
65         {
66                 tb->dirty_flags |= DIRTY_ALL;
67         }
68         else
69         {
70                 tb->dirty_line = tb->cy;
71         }
72
73         return com_id;
74 }
75
76 /*
77  * In-game editor of Object Auto-picker/Destoryer
78  * @param player_ptr プレーヤーへの参照ポインタ
79  */
80 void do_cmd_edit_autopick(player_type *player_ptr)
81 {
82         static int cx_save = 0;
83         static int cy_save = 0;
84         autopick_type an_entry, *entry = &an_entry;
85         char buf[MAX_LINELEN];
86         int i;
87         int key = -1;
88         static s32b old_autosave_turn = 0L;
89         byte quit = 0;
90
91         text_body_type text_body;
92         text_body_type *tb = &text_body;
93         tb->changed = FALSE;
94         tb->cx = cx_save;
95         tb->cy = cy_save;
96         tb->upper = tb->left = 0;
97         tb->mark = 0;
98         tb->mx = tb->my = 0;
99         tb->old_cy = tb->old_upper = tb->old_left = -1;
100         tb->old_wid = tb->old_hgt = -1;
101         tb->old_com_id = 0;
102
103         tb->yank = NULL;
104         tb->search_o_ptr = NULL;
105         tb->search_str = NULL;
106         tb->last_destroyed = NULL;
107         tb->dirty_flags = DIRTY_ALL | DIRTY_MODE | DIRTY_EXPRESSION;
108         tb->dirty_line = -1;
109         tb->filename_mode = PT_DEFAULT;
110
111         if (current_world_ptr->game_turn < old_autosave_turn)
112         {
113                 while (old_autosave_turn > current_world_ptr->game_turn) old_autosave_turn -= TURNS_PER_TICK * TOWN_DAWN;
114         }
115
116         if (current_world_ptr->game_turn > old_autosave_turn + 100L)
117         {
118                 do_cmd_save_game(player_ptr, TRUE);
119                 old_autosave_turn = current_world_ptr->game_turn;
120         }
121
122         update_playtime();
123         init_autopick();
124         if (autopick_last_destroyed_object.k_idx)
125         {
126                 autopick_entry_from_object(player_ptr, entry, &autopick_last_destroyed_object);
127                 tb->last_destroyed = autopick_line_from_entry_kill(entry);
128         }
129
130         tb->lines_list = read_pickpref_text_lines(player_ptr, &tb->filename_mode);
131         for (i = 0; i < tb->cy; i++)
132         {
133                 if (!tb->lines_list[i])
134                 {
135                         tb->cy = tb->cx = 0;
136                         break;
137                 }
138         }
139
140         screen_save();
141         while (!quit)
142         {
143                 int com_id = 0;
144                 draw_text_editor(player_ptr, tb);
145                 prt(_("(^Q:終了 ^W:セーブして終了, ESC:メニュー, その他:入力)",
146                         "(^Q:Quit, ^W:Save&Quit, ESC:Menu, Other:Input text)"), 0, 0);
147                 if (!tb->mark)
148                 {
149                         prt(format("(%d,%d)", tb->cx, tb->cy), 0, 60);
150                 }
151                 else
152                 {
153                         prt(format("(%d,%d)-(%d,%d)", tb->mx, tb->my, tb->cx, tb->cy), 0, 60);
154                 }
155
156                 Term_gotoxy(tb->cx - tb->left, tb->cy - tb->upper + 1);
157                 tb->dirty_flags = 0;
158                 tb->dirty_line = -1;
159                 tb->old_cy = tb->cy;
160                 tb->old_upper = tb->upper;
161                 tb->old_left = tb->left;
162                 tb->old_wid = tb->wid;
163                 tb->old_hgt = tb->hgt;
164
165                 key = inkey_special(TRUE);
166
167                 if (key & SKEY_MASK)
168                 {
169                         com_id = analyze_move_key(tb, key);
170                 }
171                 else if (key == ESCAPE)
172                 {
173                         com_id = do_command_menu(0, 0);
174                         tb->dirty_flags |= DIRTY_SCREEN;
175                 }
176                 else if (!iscntrl((unsigned char)key))
177                 {
178                         if (tb->mark)
179                         {
180                                 tb->mark = 0;
181                                 tb->dirty_flags |= DIRTY_ALL;
182                         }
183
184                         insert_single_letter(tb, key);
185                         continue;
186                 }
187                 else
188                 {
189                         com_id = get_com_id((char)key);
190                 }
191
192                 if (com_id) quit = do_editor_command(player_ptr, tb, com_id);
193         }
194
195         screen_load();
196         strcpy(buf, pickpref_filename(player_ptr, tb->filename_mode));
197
198         if (quit == QUIT_AND_SAVE)
199                 write_text_lines(buf, tb->lines_list);
200
201         free_text_lines(tb->lines_list);
202         string_free(tb->search_str);
203         string_free(tb->last_destroyed);
204         kill_yank_chain(tb);
205
206         process_autopick_file(player_ptr, buf);
207         current_world_ptr->start_time = (u32b)time(NULL);
208         cx_save = tb->cx;
209         cy_save = tb->cy;
210 }