OSDN Git Service

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