From a6ffb58fdb9a9cbf8050b221080b21b13e4c0489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Wed, 18 Nov 2009 21:27:13 +0000 Subject: [PATCH] optimized map initialization, more sanity checks --- library/DFHackAPI.cpp | 51 ++++++++++++++++++++++--------------------- tools/expbench.cpp | 5 +++-- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/library/DFHackAPI.cpp b/library/DFHackAPI.cpp index 94162128b..5b757da3b 100644 --- a/library/DFHackAPI.cpp +++ b/library/DFHackAPI.cpp @@ -142,7 +142,7 @@ API::~API() *-----------------------------------*/ bool API::InitMap() { - uint32_t map_loc, // location of the X array + uint32_t x_array_loc, // location of the X array temp_loc, // block location temp_locx, // iterator for the X array temp_locy, // iterator for the Y array @@ -158,41 +158,42 @@ bool API::InitMap() d->occupancy_offset = d->offset_descriptor->getOffset("occupancy"); // get the map pointer - map_loc = MreadDWord(map_offset); + x_array_loc = MreadDWord(map_offset); //FIXME: very inadequate - if (!map_loc) + if (!x_array_loc) { // bad stuffz happend return false; } - + uint32_t mx, my, mz; + // get the size - d->x_block_count = MreadDWord(x_count_offset); - d->y_block_count = MreadDWord(y_count_offset); - d->z_block_count = MreadDWord(z_count_offset); + mx = d->x_block_count = MreadDWord(x_count_offset); + my = d->y_block_count = MreadDWord(y_count_offset); + mz = d->z_block_count = MreadDWord(z_count_offset); + // test for wrong map dimensions + if(mx == 0 || mx > 48 || my == 0 || my > 48 || mz == 0) + { + return false; + } + // alloc array for pinters to all blocks - d->block = new uint32_t[d->x_block_count*d->y_block_count*d->z_block_count]; - - //read the memory from the map blocks - x -> map slice - for(uint32_t x = 0; x < d->x_block_count; x++) + d->block = new uint32_t[mx*my*mz]; + uint32_t temp_x[mx]; + uint32_t temp_y[my]; + uint32_t temp_z[mz]; + + Mread(x_array_loc,mx * sizeof(uint32_t),(uint8_t *)temp_x); + for(uint32_t x = 0; x < mx; x++) { - temp_locx = map_loc + ( 4 * x ); - temp_locy = MreadDWord(temp_locx); - + Mread(temp_x[x],my * sizeof(uint32_t),(uint8_t *)temp_y); // y -> map column - for(uint32_t y = 0; y < d->y_block_count; y++) + for(uint32_t y = 0; y < my; y++) { - temp_locz = MreadDWord(temp_locy); - temp_locy += 4; - - // z -> map block (16x16) - for(uint32_t z = 0; z < d->z_block_count; z++) - { - temp_loc = MreadDWord(temp_locz); - temp_locz += 4; - d->block[x*d->y_block_count*d->z_block_count + y*d->z_block_count + z] = temp_loc; - } + Mread(temp_y[y], + mz * sizeof(uint32_t), + (uint8_t *)(d->block + x*my*mz + y*mz)); } } return true; diff --git a/tools/expbench.cpp b/tools/expbench.cpp index 9edebdb36..41e82c7dc 100644 --- a/tools/expbench.cpp +++ b/tools/expbench.cpp @@ -27,12 +27,12 @@ int main (void) cerr << "DF not found" << endl; return 1; } - DF.InitMap(); - DF.getSize(x_max,y_max,z_max); time(&start); for(uint32_t i = 0; i< 1000;i++) { + DF.InitMap(); + DF.getSize(x_max,y_max,z_max); if((i % 10) == 0) { int percentage = i / 10; @@ -55,6 +55,7 @@ int main (void) } } } + DF.DestroyMap(); } DF.Detach(); time(&end);