DFhackCExport command_result df_flows (Core * c, vector <string> & parameters) { uint32_t x_max,y_max,z_max; DFHack::designations40d designations; DFHack::Maps *Maps; c->Suspend(); Maps = c->getMaps(); // init the map if(!Maps->Start()) { c->con.printerr("Can't init map.\n"); c->Resume(); return CR_FAILURE; } DFHack::t_blockflags bflags; Maps->getSize(x_max,y_max,z_max); // walk the map, count flowing tiles, magma, water uint32_t flow1=0, flow2=0, flowboth=0, water=0, magma=0; c->con.print("Counting flows and liquids ...\n"); for(uint32_t x = 0; x< x_max;x++) { for(uint32_t y = 0; y< y_max;y++) { for(uint32_t z = 0; z< z_max;z++) { if(Maps->getBlock(x,y,z)) { Maps->ReadBlockFlags(x, y, z, bflags); Maps->ReadDesignations(x, y, z, &designations); if (bflags.bits.liquid_1) flow1++; if (bflags.bits.liquid_2) flow2++; if (bflags.bits.liquid_1 && bflags.bits.liquid_2) flowboth++; for (uint32_t i = 0; i < 16;i++) for (uint32_t j = 0; j < 16;j++) { if (designations[i][j].bits.liquid_type == DFHack::liquid_magma) magma++; if (designations[i][j].bits.liquid_type == DFHack::liquid_water) water++; } } } } } c->con.print("Blocks with liquid_1=true: %d\n" "Blocks with liquid_2=true: %d\n" "Blocks with both: %d\n" "Water tiles: %d\n" "Magma tiles: %d\n" ,flow1, flow2, flowboth, water, magma ); c->Resume(); return CR_OK; }
int main(int argc, char *argv[]) { /* initialize your non-curses data structures here */ signal(SIGINT, finish); /* arrange interrupts to terminate */ setlocale(LC_ALL,""); initscr(); /* initialize the curses library */ keypad(stdscr, TRUE); /* enable keyboard mapping */ nonl(); /* tell curses not to do NL->CR/NL on output */ cbreak(); /* take input chars one at a time, no wait for \n */ noecho(); /* don't echo input */ //nodelay(stdscr, true); keypad(stdscr, TRUE); scrollok(stdscr, TRUE); if (has_colors()) { start_color(); /* * Simple color assignment, often all we need. */ init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK); init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK); init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK); init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK); init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK); init_color(COLOR_CYAN, 700, 700, 700); // lt grey init_color(COLOR_MAGENTA, 500, 500, 500); // dk grey init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK); init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK); init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); } int x_max,y_max,z_max; uint32_t x_max_a,y_max_a,z_max_a; /* uint16_t tiletypes[16][16]; DFHack::t_designation designations[16][16]; uint8_t regionoffsets[16]; */ map <int16_t, uint32_t> materials; materials.clear(); mapblock40d blocks[3][3]; vector<DFHack::t_effect_df40d> effects; vector< vector <uint16_t> > layerassign; vector<t_vein> veinVector; vector<t_frozenliquidvein> IceVeinVector; vector<t_spattervein> splatter; vector<t_grassvein> grass; vector<t_worldconstruction> wconstructs; t_temperatures b_temp1; t_temperatures b_temp2; DFHack::Materials * Mats = 0; DFHack::Maps * Maps = 0; DFHack::ContextManager DFMgr("Memory.xml"); DFHack::Context* DF; try { pDF = DF = DFMgr.getSingleContext(); DF->Attach(); Maps = DF->getMaps(); } catch (exception& e) { cerr << e.what() << endl; #ifndef LINUX_BUILD cin.ignore(); #endif finish(0); } bool hasmats = true; try { Mats = DF->getMaterials(); } catch (exception& e) { hasmats = false; } // init the map if(!Maps->Start()) { error = "Can't find a map to look at."; finish(0); } Maps->getSize(x_max_a,y_max_a,z_max_a); x_max = x_max_a; y_max = y_max_a; z_max = z_max_a; bool hasInorgMats = false; bool hasPlantMats = false; bool hasCreatureMats = false; if(hasmats) { // get stone matgloss mapping if(Mats->ReadInorganicMaterials()) { hasInorgMats = true; } if(Mats->ReadCreatureTypes()) { hasCreatureMats = true; } if(Mats->ReadOrganicMaterials()) { hasPlantMats = true; } } /* // get region geology if(!DF.ReadGeology( layerassign )) { error = "Can't read local geology."; pDF = 0; finish(0); } */ // FIXME: could fail on small forts int cursorX = x_max/2 - 1; int cursorY = y_max/2 - 1; int cursorZ = z_max/2 - 1; bool dig = false; bool dump = false; bool digbit = false; bool dotwiddle; unsigned char twiddle = 0; int vein = 0; int filenum = 0; bool dirtybit = false; uint32_t blockaddr = 0; uint32_t blockaddr2 = 0; t_blockflags bflags; bflags.whole = 0; enum e_tempmode { TEMP_NO, TEMP_1, TEMP_2, WATER_SALT, WATER_STAGNANT }; e_tempmode temperature = TEMP_NO; // resume so we don't block DF while we wait for input DF->Resume(); for (;;) { dig = false; dump = false; dotwiddle = false; digbit = false; int c = getch(); /* refresh, accept single keystroke of input */ flushinp(); clrscr(); /* process the command keystroke */ switch(c) { case KEY_DOWN: cursorY ++; break; case KEY_UP: cursorY --; break; case KEY_LEFT: cursorX --; break; case KEY_RIGHT: cursorX ++; break; case KEY_NPAGE: cursorZ --; break; case KEY_PPAGE: cursorZ ++; break; case '+': vein ++; break; case 'd': dig = true; break; case 'o': dump = true; break; case '-': vein --; break; case 'z': digbit = true; break; case '/': if(twiddle != 0) twiddle--; break; case '*': twiddle++; break; case 't': dotwiddle = true; break; case 'b': temperature = TEMP_NO; break; case 'n': temperature = TEMP_1; break; case 'm': temperature = TEMP_2; break; case 'c': temperature = WATER_SALT; break; case 'v': temperature = WATER_STAGNANT; break; case 27: // escape key DF->Detach(); return 0; break; default: break; } cursorX = max(cursorX, 0); cursorY = max(cursorY, 0); cursorZ = max(cursorZ, 0); cursorX = min(cursorX, x_max - 1); cursorY = min(cursorY, y_max - 1); cursorZ = min(cursorZ, z_max - 1); if(twiddle > 31) twiddle = 31; // clear data before we suspend memset(blocks,0,sizeof(blocks)); veinVector.clear(); IceVeinVector.clear(); effects.clear(); splatter.clear(); grass.clear(); dirtybit = 0; // Supend, read/write data DF->Suspend(); // restart cleared modules Maps->Start(); if(hasmats) { Mats->Start(); if(hasInorgMats) { Mats->ReadInorganicMaterials(); } if(hasPlantMats) { Mats->ReadOrganicMaterials(); } if(hasCreatureMats) { Mats->ReadCreatureTypes(); } } /* if(DF.InitReadEffects(effectnum)) { for(uint32_t i = 0; i < effectnum;i++) { t_effect_df40d effect; DF.ReadEffect(i,effect); effects.push_back(effect); } } */ for(int i = -1; i <= 1; i++) for(int j = -1; j <= 1; j++) { mapblock40d * Block = &blocks[i+1][j+1]; if(Maps->isValidBlock(cursorX+i,cursorY+j,cursorZ)) { Maps->ReadBlock40d(cursorX+i,cursorY+j,cursorZ, Block); // extra processing of the block in the middle if(i == 0 && j == 0) { if(hasInorgMats) do_features(DF, Block, cursorX, cursorY, 50,10, Mats->inorganic); // read veins Maps->ReadVeins(cursorX+i,cursorY+j,cursorZ,&veinVector,&IceVeinVector,&splatter,&grass, &wconstructs); // get pointer to block blockaddr = Maps->getBlockPtr(cursorX+i,cursorY+j,cursorZ); blockaddr2 = Block->origin; // dig all veins and trees if(dig) { for(int x = 0; x < 16; x++) for(int y = 0; y < 16; y++) { int16_t tiletype = Block->tiletypes[x][y]; TileShape tc = tileShape(tiletype); TileMaterial tm = tileMaterial(tiletype); if( tc == WALL && tm == VEIN || tc == TREE_OK || tc == TREE_DEAD) { Block->designation[x][y].bits.dig = designation_default; } } Maps->WriteDesignations(cursorX+i,cursorY+j,cursorZ, &(Block->designation)); } // read temperature data Maps->ReadTemperatures(cursorX+i,cursorY+j,cursorZ,&b_temp1, &b_temp2 ); if(dotwiddle) { bitset<32> bs = Block->designation[0][0].whole; bs.flip(twiddle); Block->designation[0][0].whole = bs.to_ulong(); Maps->WriteDesignations(cursorX+i,cursorY+j,cursorZ, &(Block->designation)); dotwiddle = false; } // do a dump of the block data if(dump) { hexdump(DF,blockaddr,0x1E00,filenum); filenum++; } // read/write dirty bit of the block Maps->ReadDirtyBit(cursorX+i,cursorY+j,cursorZ,dirtybit); Maps->ReadBlockFlags(cursorX+i,cursorY+j,cursorZ,bflags); if(digbit) { dirtybit = !dirtybit; Maps->WriteDirtyBit(cursorX+i,cursorY+j,cursorZ,dirtybit); } } } } // Resume, print stuff to the terminal DF->Resume(); for(int i = -1; i <= 1; i++) for(int j = -1; j <= 1; j++) { mapblock40d * Block = &blocks[i+1][j+1]; for(int x = 0; x < 16; x++) for(int y = 0; y < 16; y++) { int color = COLOR_BLACK; color = pickColor(Block->tiletypes[x][y]); /* if(!Block->designation[x][y].bits.hidden) { puttile(x+(i+1)*16,y+(j+1)*16,Block->tiletypes[x][y], color); } else*/ { attron(A_STANDOUT); puttile(x+(i+1)*16,y+(j+1)*16,Block->tiletypes[x][y], color); attroff(A_STANDOUT); } } // print effects for the center tile /* if(i == 0 && j == 0) { for(uint zz = 0; zz < effects.size();zz++) { if(effects[zz].z == cursorZ && !effects[zz].isHidden) { // block coords to tile coords uint16_t x = effects[zz].x - (cursorX * 16); uint16_t y = effects[zz].y - (cursorY * 16); if(x < 16 && y < 16) { putch(x + 16,y + 16,'@',COLOR_WHITE); } } } } */ } gotoxy(50,0); cprintf("arrow keys, PGUP, PGDN = navigate"); gotoxy(50,1); cprintf("+,- = switch vein"); gotoxy(50,2); uint32_t mineralsize = veinVector.size(); uint32_t icesize = IceVeinVector.size(); uint32_t splattersize = splatter.size(); uint32_t grasssize = grass.size(); uint32_t wconstrsize = wconstructs.size(); uint32_t totalVeinSize = mineralsize+ icesize + splattersize + grasssize + wconstrsize; if(vein == totalVeinSize) vein = totalVeinSize - 1; if(vein < -1) vein = -1; cprintf("X %d/%d, Y %d/%d, Z %d/%d. Vein %d of %d",cursorX+1,x_max,cursorY+1,y_max,cursorZ,z_max,vein+1,totalVeinSize); if(!veinVector.empty() || !IceVeinVector.empty() || !splatter.empty() || !grass.empty() || !wconstructs.empty()) { if(vein != -1 && vein < totalVeinSize) { uint32_t realvein = 0; if(vein < mineralsize) { realvein = vein; //iterate through vein rows for(uint32_t j = 0;j<16;j++) { //iterate through the bits for (uint32_t k = 0; k< 16;k++) { // and the bit array with a one-bit mask, check if the bit is set bool set = !!(((1 << k) & veinVector[realvein].assignment[j]) >> k); if(set) { putch(k+16,j+16,'$',COLOR_RED); } } } if(hasInorgMats) { gotoxy(50,3); cprintf("Mineral: %s",Mats->inorganic[veinVector[vein].type].id); } } else if (vein < mineralsize + icesize) { realvein = vein - mineralsize; t_frozenliquidvein &frozen = IceVeinVector[realvein]; for(uint32_t i = 0;i<16;i++) { for (uint32_t j = 0; j< 16;j++) { int color = COLOR_BLACK; int tile = frozen.tiles[i][j]; color = pickColor(tile); attron(A_STANDOUT); puttile(i+16,j+16,tile, color); attroff(A_STANDOUT); } } gotoxy(50,3); cprintf("ICE"); } else if(vein < mineralsize + icesize + splattersize) { realvein = vein - mineralsize - icesize; for(uint32_t yyy = 0; yyy < 16; yyy++) { for(uint32_t xxx = 0; xxx < 16; xxx++) { uint8_t intensity = splatter[realvein].intensity[xxx][yyy]; if(intensity) { attron(A_STANDOUT); putch(xxx+16,yyy+16,'*', COLOR_RED); attroff(A_STANDOUT); } } } if(hasCreatureMats) { gotoxy(50,3); cprintf("Spatter: %s",PrintSplatterType(splatter[realvein].mat1,splatter[realvein].mat2,Mats->race).c_str()); } } else if(vein < mineralsize + icesize + splattersize + grasssize) { realvein = vein - mineralsize - icesize - splattersize; t_grassvein & grassy =grass[realvein]; for(uint32_t yyy = 0; yyy < 16; yyy++) { for(uint32_t xxx = 0; xxx < 16; xxx++) { uint8_t intensity = grassy.intensity[xxx][yyy]; if(intensity) { attron(A_STANDOUT); putch(xxx+16,yyy+16,'X', COLOR_RED); attroff(A_STANDOUT); } } } if(hasPlantMats) { gotoxy(50,3); cprintf("Grass: 0x%x, %s",grassy.address_of, Mats->organic[grassy.material].id); } } else { realvein = vein - mineralsize - icesize - splattersize - grasssize; t_worldconstruction & wconstr=wconstructs[realvein]; for(uint32_t j = 0; j < 16; j++) { for(uint32_t k = 0; k < 16; k++) { bool set = !!(((1 << k) & wconstr.assignment[j]) >> k); if(set) { putch(k+16,j+16,'$',COLOR_RED); } } } if(hasInorgMats) { gotoxy(50,3); cprintf("Road: 0x%x, %d - %s", wconstr.address_of, wconstr.material,Mats->inorganic[wconstr.material].id); } } }