-
Notifications
You must be signed in to change notification settings - Fork 495
Expand file tree
/
Copy pathWorld.cpp
More file actions
353 lines (296 loc) · 10.7 KB
/
World.cpp
File metadata and controls
353 lines (296 loc) · 10.7 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek ([email protected])
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include "Debug.h"
#include "modules/Gui.h"
#include "modules/Translation.h"
#include "modules/Units.h"
#include "modules/World.h"
#include "modules/Translation.h"
#include "df/block_square_event_world_constructionst.h"
#include "df/gamest.h"
#include "df/map_block.h"
#include "df/plotinfost.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/world.h"
#include "df/world_data.h"
#include "df/world_site.h"
using std::string;
using namespace DFHack;
using namespace df::enums;
using df::global::plotinfo;
using df::global::world;
namespace DFHack
{
DBG_DECLARE(core, world, DebugCategory::LINFO);
}
bool World::ReadPauseState()
{
using df::global::game;
using df::global::pause_state;
if (!pause_state || !plotinfo || !game || !world)
return false;
// count single stepping as paused
if (World::isFortressMode()) {
if (auto scr = Gui::getViewscreenByType<df::viewscreen_dwarfmodest>(0); scr && scr->keyRepeat)
return true;
}
return *pause_state ||
(plotinfo->main.mode != df::ui_sidebar_mode::Default &&
(plotinfo->main.mode != df::ui_sidebar_mode::Squads ||
plotinfo->squads.in_kill_order ||
plotinfo->squads.in_move_order
)
) ||
(game->main_interface.squads.open &&
(game->main_interface.squads.giving_kill_order ||
game->main_interface.squads.giving_move_order ||
game->main_interface.squads.giving_patrol_order ||
game->main_interface.squads.giving_burrow_order
)
) ||
game->main_interface.bottom_mode_selected == df::main_bottom_mode_type::BUILDING_PICK_MATERIALS ||
(game->main_interface.view_sheets.open &&
game->main_interface.view_sheets.unit_overview_expelling
) ||
game->main_interface.create_work_order.open ||
game->main_interface.info.open ||
game->main_interface.assign_trade.open ||
game->main_interface.trade.open ||
game->main_interface.announcement_alert.open ||
world->status.popups.size() ||
game->main_interface.stocks.open ||
game->main_interface.petitions.open ||
game->main_interface.diplomacy.open ||
game->main_interface.name_creator.open ||
game->main_interface.image_creator.open ||
game->main_interface.assign_vehicle.open ||
game->main_interface.job_details.open ||
game->main_interface.options.open ||
game->main_interface.assign_display_item.open ||
game->main_interface.custom_stockpile.open;
}
void World::SetPauseState(bool paused)
{
bool dummy;
DF_GLOBAL_VALUE(pause_state, dummy) = paused;
}
uint32_t World::ReadCurrentYear()
{
return DF_GLOBAL_VALUE(cur_year, 0);
}
uint32_t World::ReadCurrentTick()
{
// prevent this from returning anything less than 0,
// to avoid day/month calculations with 0xffffffff
return std::max(0, DF_GLOBAL_VALUE(cur_year_tick, 0));
}
bool World::ReadGameMode(t_gamemodes& rd)
{
if(df::global::gamemode && df::global::gametype)
{
rd.g_mode = (DFHack::GameMode)*df::global::gamemode;
rd.g_type = (DFHack::GameType)*df::global::gametype;
return true;
}
return false;
}
bool World::WriteGameMode(const t_gamemodes & wr)
{
if(df::global::gamemode && df::global::gametype)
{
*df::global::gamemode = wr.g_mode;
*df::global::gametype = wr.g_type;
return true;
}
return false;
}
uint32_t World::ReadCurrentMonth()
{
return ReadCurrentTick() / 1200 / 28;
}
uint32_t World::ReadCurrentDay()
{
return ((ReadCurrentTick() / 1200) % 28) + 1;
}
uint8_t World::ReadCurrentWeather()
{
if (df::global::current_weather)
return (*df::global::current_weather)[2][2];
return 0;
}
void World::SetCurrentWeather(uint8_t weather)
{
if (df::global::current_weather)
memset(df::global::current_weather, weather, 25);
}
string World::ReadWorldFolder()
{
return world->cur_savegame.save_dir;
}
string World::getWorldName(bool in_english)
{
if (!world || !world->world_data)
return "";
return Translation::translateName(&world->world_data->name, in_english);
}
bool World::isFortressMode(df::game_type t)
{
if (t == -1 && df::global::gametype)
t = *df::global::gametype;
return (t == game_type::DWARF_MAIN || t == game_type::DWARF_RECLAIM ||
t == game_type::DWARF_UNRETIRE);
}
bool World::isAdventureMode(df::game_type t)
{
if (t == -1 && df::global::gametype)
t = *df::global::gametype;
return (t == game_type::ADVENTURE_MAIN);
}
bool World::isArena(df::game_type t)
{
if (t == -1 && df::global::gametype)
t = *df::global::gametype;
return (t == game_type::DWARF_ARENA || t == game_type::ADVENTURE_ARENA);
}
bool World::isLegends(df::game_type t)
{
if (t == -1 && df::global::gametype)
t = *df::global::gametype;
return (t == game_type::VIEW_LEGENDS);
}
df::unit * World::getAdventurer() {
if (!isAdventureMode() || !world)
return NULL;
return world->units.adv_unit;
}
int32_t World::GetCurrentSiteId() {
if (!plotinfo)
return -1;
if (isFortressMode())
return plotinfo->site_id;
if (auto adv = getAdventurer(); adv && world->world_data) {
DEBUG(world).print("searching for adventure site\n");
auto & world_map = world->map;
auto adv_pos = Units::getPosition(adv);
DEBUG(world).print("adv_pos: ({}, {}, {})\n", adv_pos.x, adv_pos.y, adv_pos.z);
df::coord2d rgn_pos(world_map.region_x + adv_pos.x/48, world_map.region_y + adv_pos.y/48);
for (auto site : world->world_data->sites) {
DEBUG(world).print("scanning site {}: {}\n", site->id, Translation::translateName(&site->name, true));
DEBUG(world).print(" rgn_pos: ({}, {}); site bounds: ({}, {}), ({}, {}) \n",
rgn_pos.x, rgn_pos.y, site->global_min_x, site->global_min_y, site->global_max_x, site->global_max_y);
if (rgn_pos.x >= site->global_min_x && rgn_pos.x <= site->global_max_x
&& rgn_pos.y >= site->global_min_y && rgn_pos.y <= site->global_max_y)
{
DEBUG(world).print("found site: {}\n", site->id);
return site->id;
}
}
}
return -1;
}
bool World::IsSiteLoaded() {
return GetCurrentSiteId() != -1;
}
PersistentDataItem World::AddPersistentEntityData(int entity_id, const std::string &key) {
return Persistence::addItem(entity_id, key);
}
PersistentDataItem World::AddPersistentWorldData(const std::string &key) {
return AddPersistentEntityData(Persistence::WORLD_ENTITY_ID, key);
}
PersistentDataItem World::AddPersistentSiteData(const std::string &key) {
return AddPersistentEntityData(GetCurrentSiteId(), key);
}
PersistentDataItem World::GetPersistentEntityData(int entity_id, const std::string &key, bool create) {
bool added = false;
return create ? Persistence::getByKey(entity_id, key, &added) : Persistence::getByKey(entity_id, key);
}
PersistentDataItem World::GetPersistentWorldData(const std::string &key, bool create) {
return GetPersistentEntityData(Persistence::WORLD_ENTITY_ID, key, create);
}
PersistentDataItem World::GetPersistentSiteData(const std::string &key, bool create) {
return GetPersistentEntityData(GetCurrentSiteId(), key, create);
}
void World::GetPersistentEntityData(std::vector<PersistentDataItem> *vec, int entity_id, const std::string &key, bool prefix) {
if (prefix && key.empty())
Persistence::getAll(*vec, entity_id);
else if (prefix) {
std::string min = key;
if (min.back() != '/')
min.push_back('/');
std::string max = min;
++max.back();
Persistence::getAllByKeyRange(*vec, entity_id, min, max);
}
else
Persistence::getAllByKey(*vec, entity_id, key);
}
void World::GetPersistentWorldData(std::vector<PersistentDataItem> *vec, const std::string &key, bool prefix) {
GetPersistentEntityData(vec, Persistence::WORLD_ENTITY_ID, key, prefix);
}
void World::GetPersistentSiteData(std::vector<PersistentDataItem> *vec, const std::string &key, bool prefix) {
GetPersistentEntityData(vec, GetCurrentSiteId(), key, prefix);
}
bool World::DeletePersistentData(const PersistentDataItem &item) {
return Persistence::deleteItem(item);
}
df::tile_bitmask *World::getPersistentTilemask(PersistentDataItem &item, df::map_block *block, bool create) {
if (!block)
return NULL;
int id = item.fake_df_id();
if (id > -100)
return NULL;
for (auto ev : block->block_events) {
if (ev->getType() != block_square_event_type::world_construction)
continue;
auto wcsev = strict_virtual_cast<df::block_square_event_world_constructionst>(ev);
if (!wcsev || wcsev->construction_id != id)
continue;
return &wcsev->tile_bitmask;
}
if (!create)
return NULL;
auto ev = df::allocate<df::block_square_event_world_constructionst>();
if (!ev)
return NULL;
ev->construction_id = id;
ev->tile_bitmask.clear();
vector_insert_at(block->block_events, 0, (df::block_square_event*)ev);
return &ev->tile_bitmask;
}
bool World::deletePersistentTilemask(PersistentDataItem &item, df::map_block *block) {
if (!block)
return false;
int id = item.fake_df_id();
if (id > -100)
return false;
bool found = false;
for (int i = block->block_events.size()-1; i >= 0; i--) {
auto ev = block->block_events[i];
if (ev->getType() != block_square_event_type::world_construction)
continue;
auto wcsev = strict_virtual_cast<df::block_square_event_world_constructionst>(ev);
if (!wcsev || wcsev->construction_id != id)
continue;
delete wcsev;
vector_erase_at(block->block_events, i);
found = true;
}
return found;
}