2012-01-19 23:15:51 -07:00
|
|
|
#include "Core.h"
|
2012-01-20 15:51:51 -07:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
2012-04-20 03:30:37 -06:00
|
|
|
#include "modules/MapCache.h"
|
2012-01-20 15:51:51 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
#include <fstream>
|
2012-01-27 18:32:52 -07:00
|
|
|
#include <google/protobuf/io/coded_stream.h>
|
|
|
|
#include <google/protobuf/io/zero_copy_stream_impl.h>
|
2012-01-29 00:55:42 -07:00
|
|
|
#include <google/protobuf/io/gzip_stream.h>
|
2012-01-27 18:32:52 -07:00
|
|
|
using namespace google::protobuf::io;
|
2012-01-20 15:51:51 -07:00
|
|
|
|
|
|
|
#include "DataDefs.h"
|
|
|
|
#include "df/world.h"
|
2013-01-02 09:26:30 -07:00
|
|
|
#include "df/plant.h"
|
2012-02-04 14:05:41 -07:00
|
|
|
#include "modules/Constructions.h"
|
2012-01-20 12:21:29 -07:00
|
|
|
|
|
|
|
#include "proto/Map.pb.h"
|
2012-01-29 00:55:42 -07:00
|
|
|
#include "proto/Block.pb.h"
|
2012-01-19 23:15:51 -07:00
|
|
|
|
2012-02-04 14:05:41 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
using df::global::world;
|
|
|
|
|
|
|
|
typedef std::vector<df::plant *> PlantList;
|
2012-01-26 21:54:26 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result mapexport (color_ostream &out, std::vector <std::string> & parameters);
|
2012-01-19 23:15:51 -07:00
|
|
|
|
2012-02-21 10:19:17 -07:00
|
|
|
DFHACK_PLUGIN("mapexport");
|
2012-01-19 23:15:51 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
2012-01-19 23:15:51 -07:00
|
|
|
{
|
2012-01-20 10:17:08 -07:00
|
|
|
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
2012-01-20 15:51:51 -07:00
|
|
|
commands.push_back(PluginCommand("mapexport", "Exports the current map to a file.", mapexport, true));
|
2012-01-19 23:15:51 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
2012-01-19 23:15:51 -07:00
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-19 13:02:30 -06:00
|
|
|
static dfproto::Tile::TileMaterialType toProto(df::tiletype_material mat)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This is surely ugly, but casting enums without officially
|
|
|
|
* defined numerical values to protobuf enums is against the
|
|
|
|
* way protobufs are supposed to be used, because it defeats
|
|
|
|
* the backward compatible nature of the protocols.
|
|
|
|
*/
|
|
|
|
switch (mat)
|
|
|
|
{
|
|
|
|
#define CONVERT(name) case tiletype_material::name: return dfproto::Tile::name;
|
2012-04-20 03:30:37 -06:00
|
|
|
case tiletype_material::NONE:
|
2012-04-19 13:02:30 -06:00
|
|
|
CONVERT(AIR)
|
|
|
|
case tiletype_material::PLANT:
|
|
|
|
CONVERT(SOIL)
|
|
|
|
CONVERT(STONE)
|
|
|
|
CONVERT(FEATURE)
|
|
|
|
CONVERT(LAVA_STONE)
|
|
|
|
CONVERT(MINERAL)
|
|
|
|
CONVERT(FROZEN_LIQUID)
|
|
|
|
CONVERT(CONSTRUCTION)
|
|
|
|
CONVERT(GRASS_LIGHT)
|
|
|
|
CONVERT(GRASS_DARK)
|
|
|
|
CONVERT(GRASS_DRY)
|
|
|
|
CONVERT(GRASS_DEAD)
|
|
|
|
CONVERT(HFS)
|
|
|
|
CONVERT(CAMPFIRE)
|
|
|
|
CONVERT(FIRE)
|
|
|
|
CONVERT(ASHES)
|
|
|
|
case tiletype_material::MAGMA:
|
|
|
|
return dfproto::Tile::MAGMA_TYPE;
|
|
|
|
CONVERT(DRIFTWOOD)
|
|
|
|
CONVERT(POOL)
|
|
|
|
CONVERT(BROOK)
|
|
|
|
CONVERT(RIVER)
|
|
|
|
#undef CONVERT
|
|
|
|
}
|
2012-04-20 03:30:37 -06:00
|
|
|
return dfproto::Tile::AIR;
|
2012-04-19 13:02:30 -06:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result mapexport (color_ostream &out, std::vector <std::string> & parameters)
|
2012-01-19 23:15:51 -07:00
|
|
|
{
|
2012-02-04 14:05:41 -07:00
|
|
|
bool showHidden = false;
|
|
|
|
|
|
|
|
int filenameParameter = 1;
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for(size_t i = 0; i < parameters.size();i++)
|
2012-01-19 23:15:51 -07:00
|
|
|
{
|
|
|
|
if(parameters[i] == "help" || parameters[i] == "?")
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("Exports the currently visible map to a file.\n"
|
2012-02-04 14:05:41 -07:00
|
|
|
"Usage: mapexport [options] <filename>\n"
|
|
|
|
"Example: mapexport all embark.dfmap\n"
|
|
|
|
"Options:\n"
|
2012-02-12 19:42:25 -07:00
|
|
|
" all - Export the entire map, not just what's revealed.\n"
|
2012-01-26 22:27:57 -07:00
|
|
|
);
|
2012-01-19 23:15:51 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
2012-02-04 14:05:41 -07:00
|
|
|
if (parameters[i] == "all")
|
|
|
|
{
|
|
|
|
showHidden = true;
|
|
|
|
filenameParameter++;
|
|
|
|
}
|
2012-01-19 23:15:51 -07:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
CoreSuspender suspend;
|
2012-01-20 15:51:51 -07:00
|
|
|
|
|
|
|
uint32_t x_max=0, y_max=0, z_max=0;
|
2012-03-10 04:55:42 -07:00
|
|
|
|
2012-01-20 15:51:51 -07:00
|
|
|
if (!Maps::IsValid())
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Map is not available!\n");
|
2012-01-20 15:51:51 -07:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-02-05 00:51:26 -07:00
|
|
|
if (parameters.size() < filenameParameter)
|
2012-01-29 00:55:42 -07:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Please supply a filename.\n");
|
2012-02-05 00:51:26 -07:00
|
|
|
return CR_FAILURE;
|
2012-01-29 00:55:42 -07:00
|
|
|
}
|
|
|
|
|
2012-02-04 14:05:41 -07:00
|
|
|
std::string filename = parameters[filenameParameter-1];
|
|
|
|
if (filename.rfind(".dfmap") == std::string::npos) filename += ".dfmap";
|
2012-03-10 04:55:42 -07:00
|
|
|
out << "Writing to " << filename << "..." << std::endl;
|
2012-01-29 00:55:42 -07:00
|
|
|
|
2012-05-24 09:31:20 -06:00
|
|
|
std::ofstream output_file(filename.c_str(), std::ios::out | std::ios::trunc | std::ios::binary);
|
2012-01-27 18:32:52 -07:00
|
|
|
if (!output_file.is_open())
|
2012-01-20 15:51:51 -07:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Couldn't open the output file.\n");
|
2012-02-05 00:51:26 -07:00
|
|
|
return CR_FAILURE;
|
2012-01-20 15:51:51 -07:00
|
|
|
}
|
2012-01-27 18:32:52 -07:00
|
|
|
ZeroCopyOutputStream *raw_output = new OstreamOutputStream(&output_file);
|
2012-01-31 09:42:25 -07:00
|
|
|
GzipOutputStream *zip_output = new GzipOutputStream(raw_output);
|
2012-01-29 00:55:42 -07:00
|
|
|
CodedOutputStream *coded_output = new CodedOutputStream(zip_output);
|
2012-01-31 09:42:25 -07:00
|
|
|
|
|
|
|
coded_output->WriteLittleEndian32(0x50414DDF); //Write our file header
|
2012-01-27 18:32:52 -07:00
|
|
|
|
|
|
|
Maps::getSize(x_max, y_max, z_max);
|
|
|
|
MapExtras::MapCache map;
|
2012-03-10 04:55:42 -07:00
|
|
|
DFHack::Materials *mats = Core::getInstance().getMaterials();
|
2012-01-20 15:51:51 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
out << "Writing map info..." << std::endl;
|
2012-02-04 14:05:41 -07:00
|
|
|
|
2012-01-20 17:21:50 -07:00
|
|
|
dfproto::Map protomap;
|
|
|
|
protomap.set_x_size(x_max);
|
|
|
|
protomap.set_y_size(y_max);
|
|
|
|
protomap.set_z_size(z_max);
|
2012-01-20 15:51:51 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
out << "Writing material dictionary..." << std::endl;
|
2012-02-04 14:05:41 -07:00
|
|
|
|
|
|
|
for (size_t i = 0; i < world->raws.inorganics.size(); i++)
|
|
|
|
{
|
|
|
|
dfproto::Material *protomaterial = protomap.add_inorganic_material();
|
2012-02-12 19:39:43 -07:00
|
|
|
protomaterial->set_index(i);
|
2012-02-04 14:05:41 -07:00
|
|
|
protomaterial->set_name(world->raws.inorganics[i]->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < world->raws.plants.all.size(); i++)
|
|
|
|
{
|
|
|
|
dfproto::Material *protomaterial = protomap.add_organic_material();
|
2012-02-12 19:39:43 -07:00
|
|
|
protomaterial->set_index(i);
|
2012-02-04 14:05:41 -07:00
|
|
|
protomaterial->set_name(world->raws.plants.all[i]->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<df::coord,std::pair<uint32_t,uint16_t> > constructionMaterials;
|
|
|
|
if (Constructions::isValid())
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < Constructions::getCount(); i++)
|
|
|
|
{
|
|
|
|
df::construction *construction = Constructions::getConstruction(i);
|
|
|
|
constructionMaterials[construction->pos] = std::make_pair(construction->mat_index, construction->mat_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 00:55:42 -07:00
|
|
|
coded_output->WriteVarint32(protomap.ByteSize());
|
|
|
|
protomap.SerializeToCodedStream(coded_output);
|
2012-02-04 14:05:41 -07:00
|
|
|
|
2012-01-20 15:51:51 -07:00
|
|
|
DFHack::t_feature blockFeatureGlobal;
|
|
|
|
DFHack::t_feature blockFeatureLocal;
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("Writing map block information");
|
2012-02-04 14:05:41 -07:00
|
|
|
|
2012-01-20 15:51:51 -07:00
|
|
|
for(uint32_t z = 0; z < z_max; z++)
|
|
|
|
{
|
|
|
|
for(uint32_t b_y = 0; b_y < y_max; b_y++)
|
|
|
|
{
|
|
|
|
for(uint32_t b_x = 0; b_x < x_max; b_x++)
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
if (b_x == 0 && b_y == 0 && z % 10 == 0) out.print(".");
|
2012-01-20 15:51:51 -07:00
|
|
|
// Get the map block
|
|
|
|
df::coord2d blockCoord(b_x, b_y);
|
|
|
|
MapExtras::Block *b = map.BlockAt(DFHack::DFCoord(b_x, b_y, z));
|
2012-04-10 08:21:19 -06:00
|
|
|
if (!b || !b->is_valid())
|
2012-01-20 15:51:51 -07:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-29 00:55:42 -07:00
|
|
|
dfproto::Block protoblock;
|
|
|
|
protoblock.set_x(b_x);
|
|
|
|
protoblock.set_y(b_y);
|
|
|
|
protoblock.set_z(z);
|
2012-01-20 15:51:51 -07:00
|
|
|
|
2012-04-10 08:21:19 -06:00
|
|
|
// Find features
|
|
|
|
b->GetGlobalFeature(&blockFeatureGlobal);
|
|
|
|
b->GetLocalFeature(&blockFeatureLocal);
|
2012-01-20 15:51:51 -07:00
|
|
|
|
|
|
|
int global_z = df::global::world->map.region_z + z;
|
|
|
|
|
|
|
|
// Iterate over all the tiles in the block
|
|
|
|
for(uint32_t y = 0; y < 16; y++)
|
|
|
|
{
|
|
|
|
for(uint32_t x = 0; x < 16; x++)
|
|
|
|
{
|
|
|
|
df::coord2d coord(x, y);
|
|
|
|
df::tile_designation des = b->DesignationAt(coord);
|
|
|
|
df::tile_occupancy occ = b->OccupancyAt(coord);
|
|
|
|
|
|
|
|
// Skip hidden tiles
|
|
|
|
if (!showHidden && des.bits.hidden)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-29 00:55:42 -07:00
|
|
|
dfproto::Tile *prototile = protoblock.add_tile();
|
2012-01-20 17:21:50 -07:00
|
|
|
prototile->set_x(x);
|
|
|
|
prototile->set_y(y);
|
2012-01-20 15:51:51 -07:00
|
|
|
|
|
|
|
// Check for liquid
|
|
|
|
if (des.bits.flow_size)
|
|
|
|
{
|
2012-01-29 00:55:42 -07:00
|
|
|
prototile->set_liquid_type((dfproto::Tile::LiquidType)des.bits.liquid_type);
|
2012-02-05 00:51:26 -07:00
|
|
|
prototile->set_flow_size(des.bits.flow_size);
|
2012-01-20 15:51:51 -07:00
|
|
|
}
|
|
|
|
|
2012-04-19 09:17:07 -06:00
|
|
|
df::tiletype type = b->tiletypeAt(coord);
|
2012-02-13 15:56:33 -07:00
|
|
|
prototile->set_type((dfproto::Tile::TileType)tileShape(type));
|
2012-04-19 13:02:30 -06:00
|
|
|
prototile->set_tile_material(toProto(tileMaterial(type)));
|
2012-02-04 14:05:41 -07:00
|
|
|
|
|
|
|
df::coord map_pos = df::coord(b_x*16+x,b_y*16+y,z);
|
|
|
|
|
2012-02-13 15:56:33 -07:00
|
|
|
switch (tileMaterial(type))
|
2012-02-05 00:51:26 -07:00
|
|
|
{
|
2012-02-13 18:17:38 -07:00
|
|
|
case tiletype_material::SOIL:
|
|
|
|
case tiletype_material::STONE:
|
2012-02-12 19:39:43 -07:00
|
|
|
prototile->set_material_type(0);
|
2012-04-19 09:17:07 -06:00
|
|
|
prototile->set_material_index(b->layerMaterialAt(coord));
|
2012-02-05 00:51:26 -07:00
|
|
|
break;
|
2012-02-13 18:17:38 -07:00
|
|
|
case tiletype_material::MINERAL:
|
2012-02-12 19:39:43 -07:00
|
|
|
prototile->set_material_type(0);
|
|
|
|
prototile->set_material_index(b->veinMaterialAt(coord));
|
2012-02-05 00:51:26 -07:00
|
|
|
break;
|
2012-02-13 18:17:38 -07:00
|
|
|
case tiletype_material::FEATURE:
|
2012-02-05 00:51:26 -07:00
|
|
|
if (blockFeatureLocal.type != -1 && des.bits.feature_local)
|
2012-01-30 22:12:35 -07:00
|
|
|
{
|
2012-02-13 18:17:38 -07:00
|
|
|
if (blockFeatureLocal.type == feature_type::deep_special_tube
|
2012-01-30 22:12:35 -07:00
|
|
|
&& blockFeatureLocal.main_material == 0) // stone
|
|
|
|
{
|
2012-02-12 19:39:43 -07:00
|
|
|
prototile->set_material_type(0);
|
|
|
|
prototile->set_material_index(blockFeatureLocal.sub_material);
|
2012-01-30 22:12:35 -07:00
|
|
|
}
|
2012-02-05 00:51:26 -07:00
|
|
|
if (blockFeatureGlobal.type != -1 && des.bits.feature_global
|
2012-02-13 18:17:38 -07:00
|
|
|
&& blockFeatureGlobal.type == feature_type::feature_underworld_from_layer
|
2012-02-05 00:51:26 -07:00
|
|
|
&& blockFeatureGlobal.main_material == 0) // stone
|
|
|
|
{
|
2012-02-12 19:39:43 -07:00
|
|
|
prototile->set_material_type(0);
|
|
|
|
prototile->set_material_index(blockFeatureGlobal.sub_material);
|
2012-02-05 00:51:26 -07:00
|
|
|
}
|
2012-01-30 22:12:35 -07:00
|
|
|
}
|
2012-02-04 14:05:41 -07:00
|
|
|
break;
|
2012-02-13 18:17:38 -07:00
|
|
|
case tiletype_material::CONSTRUCTION:
|
2012-02-04 14:05:41 -07:00
|
|
|
if (constructionMaterials.find(map_pos) != constructionMaterials.end())
|
|
|
|
{
|
|
|
|
prototile->set_material_index(constructionMaterials[map_pos].first);
|
2012-02-12 19:39:43 -07:00
|
|
|
prototile->set_material_type(constructionMaterials[map_pos].second);
|
2012-02-04 14:05:41 -07:00
|
|
|
}
|
|
|
|
break;
|
2012-02-20 20:32:58 -07:00
|
|
|
default:
|
|
|
|
break;
|
2012-02-05 00:51:26 -07:00
|
|
|
}
|
2012-01-20 15:51:51 -07:00
|
|
|
}
|
|
|
|
}
|
2012-02-04 14:05:41 -07:00
|
|
|
|
2012-04-11 02:01:27 -06:00
|
|
|
if (b->getRaw())
|
2012-02-04 14:05:41 -07:00
|
|
|
{
|
2012-04-11 02:01:27 -06:00
|
|
|
PlantList *plants = &b->getRaw()->plants;
|
2012-02-04 14:05:41 -07:00
|
|
|
for (PlantList::const_iterator it = plants->begin(); it != plants->end(); it++)
|
|
|
|
{
|
|
|
|
const df::plant & plant = *(*it);
|
|
|
|
df::coord2d loc(plant.pos.x, plant.pos.y);
|
|
|
|
loc = loc % 16;
|
|
|
|
if (showHidden || !b->DesignationAt(loc).bits.hidden)
|
|
|
|
{
|
2012-02-05 14:51:49 -07:00
|
|
|
dfproto::Plant *protoplant = protoblock.add_plant();
|
2012-02-05 00:51:26 -07:00
|
|
|
protoplant->set_x(loc.x);
|
|
|
|
protoplant->set_y(loc.y);
|
2012-02-04 14:05:41 -07:00
|
|
|
protoplant->set_is_shrub(plant.flags.bits.is_shrub);
|
|
|
|
protoplant->set_material(plant.material);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-05 00:51:26 -07:00
|
|
|
coded_output->WriteVarint32(protoblock.ByteSize());
|
|
|
|
protoblock.SerializeToCodedStream(coded_output);
|
2012-01-20 15:51:51 -07:00
|
|
|
} // block x
|
|
|
|
// Clean uneeded memory
|
|
|
|
map.trash();
|
|
|
|
} // block y
|
|
|
|
} // z
|
|
|
|
|
2012-01-27 18:32:52 -07:00
|
|
|
delete coded_output;
|
2012-02-05 00:51:26 -07:00
|
|
|
delete zip_output;
|
2012-01-27 18:32:52 -07:00
|
|
|
delete raw_output;
|
|
|
|
|
2012-02-04 14:05:41 -07:00
|
|
|
mats->Finish();
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("\nMap succesfully exported!\n");
|
2012-01-19 23:15:51 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|