OSDN Git Service

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