forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautodump.cpp
More file actions
295 lines (251 loc) · 8.13 KB
/
autodump.cpp
File metadata and controls
295 lines (251 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Quick Dumper : Moves items marked as "dump" to cursor
// FIXME: local item cache in map blocks needs to be fixed after teleporting items
#include "Core.h"
#include "Console.h"
#include "DataDefs.h"
#include "Export.h"
#include "PluginManager.h"
#include "modules/Maps.h"
#include "modules/Gui.h"
#include "modules/Items.h"
#include "modules/Materials.h"
#include "modules/MapCache.h"
#include "df/item.h"
#include "df/world.h"
#include "df/general_ref.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/building_stockpilest.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <climits>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
using namespace DFHack;
using namespace df::enums;
using MapExtras::Block;
using MapExtras::MapCache;
using df::building_stockpilest;
DFHACK_PLUGIN("autodump");
REQUIRE_GLOBAL(gps);
REQUIRE_GLOBAL(world);
command_result df_autodump(color_ostream &out, vector <string> & parameters);
command_result df_autodump_destroy_here(color_ostream &out, vector <string> & parameters);
command_result df_autodump_destroy_item(color_ostream &out, vector <string> & parameters);
DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCommand> &commands)
{
commands.push_back(PluginCommand(
"autodump",
"Teleport items marked for dumping to the keyboard cursor.",
df_autodump));
commands.push_back(PluginCommand(
"autodump-destroy-here",
"Destroy items marked for dumping under the keyboard cursor.",
df_autodump_destroy_here,
Gui::cursor_hotkey));
commands.push_back(PluginCommand(
"autodump-destroy-item",
"Destroy the selected item.",
df_autodump_destroy_item,
Gui::any_item_hotkey));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
typedef map <DFCoord, uint32_t> coordmap;
static command_result autodump_main(color_ostream &out, vector <string> & parameters)
{
// Command line options
bool destroy = false;
bool here = false;
bool need_visible = false;
bool need_hidden = false;
bool need_forbidden = false;
for (size_t i = 0; i < parameters.size(); i++)
{
string & p = parameters[i];
if(p == "destroy")
destroy = true;
else if (p == "destroy-here")
destroy = here = true;
else if (p == "visible")
need_visible = true;
else if (p == "hidden")
need_hidden = true;
else if (p == "forbidden")
need_forbidden = true;
else
return CR_WRONG_USAGE;
}
if (need_visible && need_hidden)
{
out.printerr("An item can't be both hidden and visible.\n");
return CR_WRONG_USAGE;
}
if (!Maps::IsValid())
{
out.printerr("Map is not available!\n");
return CR_FAILURE;
}
size_t numItems = world->items.all.size();
MapCache MC;
int dumped_total = 0;
df::coord pos_cursor;
if(!destroy || here)
{
pos_cursor = Gui::getCursorPos();
if (!pos_cursor.isValid()) {
out.printerr("Keyboard cursor must be over a suitable map tile.\n");
return CR_FAILURE;
}
}
if (!destroy)
{
{
Block * b = MC.BlockAt(pos_cursor / 16);
if(!b)
{
out.printerr("Cursor is in an invalid/uninitialized area. Place it over a floor.\n");
return CR_FAILURE;
}
df::tiletype ttype = MC.tiletypeAt(pos_cursor);
if(!DFHack::isWalkable(ttype))
{
out.printerr("Cursor should be placed over a floor.\n");
return CR_FAILURE;
}
}
}
// proceed with the dumpification operation
for(size_t i=0; i< numItems; i++)
{
df::item * itm = world->items.all[i];
DFCoord pos_item(itm->pos.x, itm->pos.y, itm->pos.z);
// only dump the stuff marked for dumping and laying on the ground
if ( !itm->flags.bits.dump
// || !itm->flags.bits.on_ground
|| itm->flags.bits.construction
|| itm->flags.bits.in_building
|| itm->flags.bits.in_chest
// || itm->flags.bits.in_inventory
|| itm->flags.bits.artifact
)
continue;
if (need_visible && itm->flags.bits.hidden)
continue;
if (need_hidden && !itm->flags.bits.hidden)
continue;
if (need_forbidden && !itm->flags.bits.forbid)
continue;
if (!need_forbidden && itm->flags.bits.forbid)
continue;
if (!destroy) // move to cursor
{
// Don't move items if they're already at the cursor
if (pos_cursor != pos_item)
{
if (Items::moveToGround(MC, itm, pos_cursor))
{
// Change flags to indicate the dump was completed, as if by super-dwarfs
itm->flags.bits.dump = false;
itm->flags.bits.forbid = true;
}
else
{
out.print("Could not move item: %s\n",
Items::getDescription(itm, 0, true).c_str());
}
}
}
else // destroy
{
if (here && pos_item != pos_cursor)
continue;
itm->flags.bits.garbage_collect = true;
// Cosmetic changes: make them disappear from view instantly
itm->flags.bits.forbid = true;
itm->flags.bits.hidden = true;
}
dumped_total++;
}
// write map changes back to DF.
if(!destroy)
MC.WriteAll();
out.print("Done. %d items %s.\n", dumped_total, destroy ? "marked for destruction" : "quickdumped");
return CR_OK;
}
command_result df_autodump(color_ostream &out, vector <string> & parameters)
{
CoreSuspender suspend;
return autodump_main(out, parameters);
}
command_result df_autodump_destroy_here(color_ostream &out, vector <string> & parameters)
{
if (!parameters.empty())
return CR_WRONG_USAGE;
vector<string> args;
args.push_back("destroy-here");
CoreSuspender suspend;
return autodump_main(out, args);
}
static map<int, df::item_flags> pending_destroy;
static int last_frame = 0;
command_result df_autodump_destroy_item(color_ostream &out, vector <string> & parameters)
{
if (!parameters.empty())
return CR_WRONG_USAGE;
CoreSuspender suspend;
df::item *item = Gui::getSelectedItem(out);
if (!item)
return CR_FAILURE;
// Allow undoing the destroy
if (world->frame_counter != last_frame)
{
last_frame = world->frame_counter;
pending_destroy.clear();
}
if (pending_destroy.count(item->id))
{
df::item_flags old_flags = pending_destroy[item->id];
pending_destroy.erase(item->id);
item->flags.bits.garbage_collect = false;
item->flags.bits.hidden = old_flags.bits.hidden;
item->flags.bits.dump = old_flags.bits.dump;
item->flags.bits.forbid = old_flags.bits.forbid;
return CR_OK;
}
// Check the item is good to destroy
if (item->flags.bits.garbage_collect)
{
out.printerr("Item is already marked for destroy.\n");
return CR_FAILURE;
}
if (item->flags.bits.construction ||
item->flags.bits.in_building ||
item->flags.bits.artifact)
{
out.printerr("Choosing not to destroy buildings, constructions and artifacts.\n");
return CR_FAILURE;
}
for (size_t i = 0; i < item->general_refs.size(); i++)
{
df::general_ref *ref = item->general_refs[i];
if (ref->getType() == general_ref_type::UNIT_HOLDER)
{
out.printerr("Choosing not to destroy items in unit inventory.\n");
return CR_FAILURE;
}
}
// Set the flags
pending_destroy[item->id] = item->flags;
item->flags.bits.garbage_collect = true;
item->flags.bits.hidden = true;
item->flags.bits.dump = true;
item->flags.bits.forbid = true;
return CR_OK;
}