Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60bb486

Browse files
committed
Add api for enabling liquid and temperature updates for blocks.
Now updates also have to be enabled for the z level to work.
1 parent af3e389 commit 60bb486

10 files changed

Lines changed: 127 additions & 66 deletions

File tree

LUA_API.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,14 @@ Job module
666666

667667
Returns the unit performing the job.
668668

669+
* ``dfhack.job.checkBuildingsNow()``
670+
671+
Instructs the game to check buildings for jobs next frame and assign workers.
672+
673+
* ``dfhack.job.checkDesignationsNow()``
674+
675+
Instructs the game to check designations for jobs next frame and assign workers.
676+
669677
* ``dfhack.job.is_equal(job1,job2)``
670678

671679
Compares important fields in the job and nested item structures.
@@ -803,6 +811,10 @@ Maps module
803811

804812
Returns the biome info struct for the given global map region.
805813

814+
* ``dfhack.maps.enableBlockUpdates(block[,flow,temperature])``
815+
816+
Enables updates for liquid flow or temperature, unless already active.
817+
806818
* ``dfhack.maps.getGlobalInitFeature(index)``
807819

808820
Returns the global feature object with the given index.

Lua API.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,12 @@ <h3><a class="toc-backref" href="#id15">Job module</a></h3>
912912
<li><p class="first"><tt class="docutils literal">dfhack.job.getWorker(job)</tt></p>
913913
<p>Returns the unit performing the job.</p>
914914
</li>
915+
<li><p class="first"><tt class="docutils literal">dfhack.job.checkBuildingsNow()</tt></p>
916+
<p>Instructs the game to check buildings for jobs next frame and assign workers.</p>
917+
</li>
918+
<li><p class="first"><tt class="docutils literal">dfhack.job.checkDesignationsNow()</tt></p>
919+
<p>Instructs the game to check designations for jobs next frame and assign workers.</p>
920+
</li>
915921
<li><p class="first"><tt class="docutils literal">dfhack.job.is_equal(job1,job2)</tt></p>
916922
<p>Compares important fields in the job and nested item structures.</p>
917923
</li>
@@ -1023,6 +1029,9 @@ <h3><a class="toc-backref" href="#id18">Maps module</a></h3>
10231029
<li><p class="first"><tt class="docutils literal">dfhack.maps.getRegionBiome(region_coord2d)</tt></p>
10241030
<p>Returns the biome info struct for the given global map region.</p>
10251031
</li>
1032+
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.maps.enableBlockUpdates(block[,flow,temperature])</span></tt></p>
1033+
<p>Enables updates for liquid flow or temperature, unless already active.</p>
1034+
</li>
10261035
<li><p class="first"><tt class="docutils literal">dfhack.maps.getGlobalInitFeature(index)</tt></p>
10271036
<p>Returns the global feature object with the given index.</p>
10281037
</li>

library/LuaApi.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ static const LuaWrapper::FunctionReg dfhack_job_module[] = {
637637
WRAPM(Job,printJobDetails),
638638
WRAPM(Job,getHolder),
639639
WRAPM(Job,getWorker),
640+
WRAPM(Job,checkBuildingsNow),
641+
WRAPM(Job,checkDesignationsNow),
640642
WRAPN(is_equal, jobEqual),
641643
WRAPN(is_item_equal, jobItemEqual),
642644
{ NULL, NULL }
@@ -753,6 +755,7 @@ static const luaL_Reg dfhack_items_funcs[] = {
753755
static const LuaWrapper::FunctionReg dfhack_maps_module[] = {
754756
WRAPN(getBlock, (df::map_block* (*)(int32_t,int32_t,int32_t))Maps::getBlock),
755757
WRAPM(Maps, getRegionBiome),
758+
WRAPM(Maps, enableBlockUpdates),
756759
WRAPM(Maps, getGlobalInitFeature),
757760
WRAPM(Maps, getLocalInitFeature),
758761
WRAPM(Maps, canWalkBetween),

library/include/modules/Job.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ namespace DFHack
5757
DFHACK_EXPORT df::building *getHolder(df::job *job);
5858
DFHACK_EXPORT df::unit *getWorker(df::job *job);
5959

60+
// Instruct the game to check and assign workers
61+
DFHACK_EXPORT void checkBuildingsNow();
62+
DFHACK_EXPORT void checkDesignationsNow();
63+
6064
DFHACK_EXPORT bool linkIntoWorld(df::job *job, bool new_id = true);
6165

6266
// lists jobs with ids >= *id_var, and sets *id_var = *job_next_id;

library/include/modules/MapCache.h

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ class DFHACK_EXPORT Block
9393
Block(MapCache *parent, DFCoord _bcoord);
9494
~Block();
9595

96+
DFCoord getCoord() { return bcoord; }
97+
98+
void enableBlockUpdates(bool flow = false, bool temp = false) {
99+
Maps::enableBlockUpdates(block, flow, temp);
100+
}
101+
96102
/*
97103
* All coordinates are taken mod 16.
98104
*/
@@ -208,11 +214,8 @@ class DFHACK_EXPORT Block
208214
dirty_designations = true;
209215
//printf("setting block %d/%d/%d , %d %d\n",x,y,z, p.x, p.y);
210216
index_tile<df::tile_designation&>(designation,p) = des;
211-
if(des.bits.dig)
212-
{
213-
dirty_blockflags = true;
214-
blockflags.bits.designated = true;
215-
}
217+
if(des.bits.dig && block)
218+
block->flags.bits.designated = true;
216219
return true;
217220
}
218221

@@ -236,15 +239,7 @@ class DFHACK_EXPORT Block
236239

237240
t_blockflags BlockFlags()
238241
{
239-
return blockflags;
240-
}
241-
bool setBlockFlags(t_blockflags des)
242-
{
243-
if(!valid) return false;
244-
dirty_blockflags = true;
245-
//printf("setting block %d/%d/%d , %d %d\n",x,y,z, p.x, p.y);
246-
blockflags = des;
247-
return true;
242+
return block ? block->flags : t_blockflags();
248243
}
249244

250245
bool Write();
@@ -273,7 +268,6 @@ class DFHACK_EXPORT Block
273268
bool dirty_designations:1;
274269
bool dirty_tiles:1;
275270
bool dirty_temperatures:1;
276-
bool dirty_blockflags:1;
277271
bool dirty_occupancies:1;
278272

279273
DFCoord bcoord;
@@ -328,7 +322,6 @@ class DFHACK_EXPORT Block
328322

329323
designations40d designation;
330324
occupancies40d occupancy;
331-
t_blockflags blockflags;
332325

333326
t_temperatures temp1;
334327
t_temperatures temp2;

library/include/modules/Maps.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ inline df::tile_occupancy *getTileOccupancy(df::coord pos) {
257257

258258
DFHACK_EXPORT df::world_data::T_region_map *getRegionBiome(df::coord2d rgn_pos);
259259

260+
// Enables per-frame updates for liquid flow and/or temperature.
261+
DFHACK_EXPORT void enableBlockUpdates(df::map_block *blk, bool flow = false, bool temperature = false);
262+
260263
/// sorts the block event vector into multiple vectors by type
261264
/// mineral veins, what's under ice, blood smears and mud
262265
extern DFHACK_EXPORT bool SortBlockEvents(df::map_block *block,

library/modules/Buildings.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,7 @@ static void linkBuilding(df::building *bld)
709709

710710
linkRooms(bld);
711711

712-
if (process_jobs)
713-
*process_jobs = true;
712+
Job::checkBuildingsNow();
714713
}
715714

716715
static void createDesign(df::building *bld, bool rough)
@@ -900,8 +899,6 @@ bool Buildings::deconstruct(df::building *bld)
900899
{
901900
using df::global::ui;
902901
using df::global::world;
903-
using df::global::process_jobs;
904-
using df::global::process_dig;
905902
using df::global::ui_look_list;
906903

907904
CHECK_NULL_POINTER(bld);
@@ -952,8 +949,8 @@ bool Buildings::deconstruct(df::building *bld)
952949
}
953950
}
954951

955-
if (process_dig) *process_dig = true;
956-
if (process_jobs) *process_jobs = true;
952+
Job::checkBuildingsNow();
953+
Job::checkDesignationsNow();
957954

958955
return true;
959956
}

library/modules/Job.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,18 @@ df::unit *DFHack::Job::getWorker(df::job *job)
256256
return NULL;
257257
}
258258

259+
void DFHack::Job::checkBuildingsNow()
260+
{
261+
if (df::global::process_jobs)
262+
*df::global::process_jobs = true;
263+
}
264+
265+
void DFHack::Job::checkDesignationsNow()
266+
{
267+
if (df::global::process_dig)
268+
*df::global::process_dig = true;
269+
}
270+
259271
bool DFHack::Job::linkIntoWorld(df::job *job, bool new_id)
260272
{
261273
using df::global::world;

library/modules/Maps.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ using namespace std;
5454
#include "df/world_region_details.h"
5555
#include "df/builtin_mats.h"
5656
#include "df/block_square_event_grassst.h"
57+
#include "df/z_level_flags.h"
5758

5859
using namespace DFHack;
5960
using namespace df::enums;
@@ -176,6 +177,30 @@ df::world_data::T_region_map *Maps::getRegionBiome(df::coord2d rgn_pos)
176177
return &data->region_map[rgn_pos.x][rgn_pos.y];
177178
}
178179

180+
void Maps::enableBlockUpdates(df::map_block *blk, bool flow, bool temperature)
181+
{
182+
if (!blk || !(flow || temperature)) return;
183+
184+
if (temperature)
185+
blk->flags.bits.update_temperature = true;
186+
187+
if (flow)
188+
{
189+
blk->flags.bits.update_liquid = true;
190+
blk->flags.bits.update_liquid_twice = true;
191+
}
192+
193+
auto z_flags = world->map.z_level_flags;
194+
int z_level = blk->map_pos.z;
195+
196+
if (z_flags && z_level >= 0 && z_level < world->map.z_count_block)
197+
{
198+
z_flags += z_level;
199+
z_flags->bits.update = true;
200+
z_flags->bits.update_twice = true;
201+
}
202+
}
203+
179204
df::feature_init *Maps::getGlobalInitFeature(int32_t index)
180205
{
181206
auto data = world->world_data;
@@ -426,7 +451,6 @@ MapExtras::Block::Block(MapCache *parent, DFCoord _bcoord) : parent(parent)
426451
dirty_designations = false;
427452
dirty_tiles = false;
428453
dirty_temperatures = false;
429-
dirty_blockflags = false;
430454
dirty_occupancies = false;
431455
valid = false;
432456
bcoord = _bcoord;
@@ -440,7 +464,6 @@ MapExtras::Block::Block(MapCache *parent, DFCoord _bcoord) : parent(parent)
440464
{
441465
COPY(designation, block->designation);
442466
COPY(occupancy, block->occupancy);
443-
blockflags = block->flags;
444467

445468
COPY(temp1, block->temperature_1);
446469
COPY(temp2, block->temperature_2);
@@ -449,7 +472,6 @@ MapExtras::Block::Block(MapCache *parent, DFCoord _bcoord) : parent(parent)
449472
}
450473
else
451474
{
452-
blockflags.whole = 0;
453475
memset(designation,0,sizeof(designation));
454476
memset(occupancy,0,sizeof(occupancy));
455477
memset(temp1,0,sizeof(temp1));
@@ -634,11 +656,6 @@ bool MapExtras::Block::Write ()
634656
{
635657
if(!valid) return false;
636658

637-
if(dirty_blockflags)
638-
{
639-
block->flags = blockflags;
640-
dirty_blockflags = false;
641-
}
642659
if(dirty_designations)
643660
{
644661
COPY(block->designation, designation);

0 commit comments

Comments
 (0)