Disconnected the RemoteReader values from the DF enums.

develop
JapaMala 2014-07-17 17:46:00 +05:30
parent 48e714b65b
commit 216019e5af
2 changed files with 396 additions and 141 deletions

@ -20,10 +20,13 @@ enum TiletypeShape
RAMP_TOP = 10;
BROOK_BED = 11;
BROOK_TOP = 12;
TREE = 13;
TREE_SHAPE = 13;
SAPLING = 14;
SHRUB = 15;
ENDLESS_PIT = 16;
BRANCH = 17;
TRUNK_BRANCH = 18;
TWIG = 19;
}
enum TiletypeSpecial
@ -40,6 +43,7 @@ enum TiletypeSpecial
WORN_2 = 8;
WORN_3 = 9;
TRACK = 10;
SMOOTH_DEAD = 11;
};
enum TiletypeMaterial
{
@ -66,14 +70,18 @@ enum TiletypeMaterial
POOL = 19;
BROOK = 20;
RIVER = 21;
ROOT = 22;
TREE_MATERIAL = 23;
MUSHROOM = 24;
UNDERWORLD_GATE = 25;
}
enum TiletypeVariant
{
NO_VARIANT = -1;
VAR_1 = 0;
VAR_2 = 1;
VAR_3 = 3;
VAR_4 = 4;
VAR_3 = 2;
VAR_4 = 3;
};
message Tiletype

@ -1,4 +1,7 @@
// This is a generic plugin that does nothing useful apart from acting as an example... of a plugin that does nothing :D
//define which version of DF this is being built for.
#define DF_VER_040
//#define DF_VER_034
// some headers required for a plugin. Nothing special, just the basics.
#include "Core.h"
@ -66,7 +69,7 @@ void FindChangedBlocks();
DFHACK_PLUGIN("RemoteFortressReader");
// Mandatory init function. If you have some global state, create it here.
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands)
{
//// Fill the command list with your commands.
//commands.push_back(PluginCommand(
@ -84,15 +87,15 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <Plug
DFhackCExport RPCService *plugin_rpcconnect(color_ostream &)
{
RPCService *svc = new RPCService();
svc->addFunction("GetMaterialList", GetMaterialList);
svc->addFunction("GetBlockList", GetBlockList);
svc->addFunction("CheckHashes", CheckHashes);
svc->addFunction("GetMaterialList", GetMaterialList);
svc->addFunction("GetBlockList", GetBlockList);
svc->addFunction("CheckHashes", CheckHashes);
svc->addFunction("GetTiletypeList", GetTiletypeList);
return svc;
}
// This is called right before the plugin library is removed from memory.
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
DFhackCExport command_result plugin_shutdown(color_ostream &out)
{
// You *MUST* kill all threads you created before this returns.
// If everything fails, just return CR_FAILURE. Your plugin will be
@ -102,116 +105,360 @@ DFhackCExport command_result plugin_shutdown ( color_ostream &out )
uint16_t fletcher16(uint8_t const *data, size_t bytes)
{
uint16_t sum1 = 0xff, sum2 = 0xff;
while (bytes) {
size_t tlen = bytes > 20 ? 20 : bytes;
bytes -= tlen;
do {
sum2 += sum1 += *data++;
} while (--tlen);
sum1 = (sum1 & 0xff) + (sum1 >> 8);
sum2 = (sum2 & 0xff) + (sum2 >> 8);
}
/* Second reduction step to reduce sums to 8 bits */
sum1 = (sum1 & 0xff) + (sum1 >> 8);
sum2 = (sum2 & 0xff) + (sum2 >> 8);
return sum2 << 8 | sum1;
uint16_t sum1 = 0xff, sum2 = 0xff;
while (bytes) {
size_t tlen = bytes > 20 ? 20 : bytes;
bytes -= tlen;
do {
sum2 += sum1 += *data++;
} while (--tlen);
sum1 = (sum1 & 0xff) + (sum1 >> 8);
sum2 = (sum2 & 0xff) + (sum2 >> 8);
}
/* Second reduction step to reduce sums to 8 bits */
sum1 = (sum1 & 0xff) + (sum1 >> 8);
sum2 = (sum2 & 0xff) + (sum2 >> 8);
return sum2 << 8 | sum1;
}
RemoteFortressReader::TiletypeMaterial TranslateMaterial(df::tiletype_material material)
{
switch (material)
{
case df::enums::tiletype_material::NONE:
return RemoteFortressReader::NO_MATERIAL;
break;
case df::enums::tiletype_material::AIR:
return RemoteFortressReader::AIR;
break;
case df::enums::tiletype_material::SOIL:
return RemoteFortressReader::SOIL;
break;
case df::enums::tiletype_material::STONE:
return RemoteFortressReader::STONE;
break;
case df::enums::tiletype_material::FEATURE:
return RemoteFortressReader::FEATURE;
break;
case df::enums::tiletype_material::LAVA_STONE:
return RemoteFortressReader::LAVA_STONE;
break;
case df::enums::tiletype_material::MINERAL:
return RemoteFortressReader::MINERAL;
break;
case df::enums::tiletype_material::FROZEN_LIQUID:
return RemoteFortressReader::FROZEN_LIQUID;
break;
case df::enums::tiletype_material::CONSTRUCTION:
return RemoteFortressReader::CONSTRUCTION;
break;
case df::enums::tiletype_material::GRASS_LIGHT:
return RemoteFortressReader::GRASS_LIGHT;
break;
case df::enums::tiletype_material::GRASS_DARK:
return RemoteFortressReader::GRASS_DARK;
break;
case df::enums::tiletype_material::GRASS_DRY:
return RemoteFortressReader::GRASS_DRY;
break;
case df::enums::tiletype_material::GRASS_DEAD:
return RemoteFortressReader::GRASS_DEAD;
break;
case df::enums::tiletype_material::PLANT:
return RemoteFortressReader::PLANT;
break;
case df::enums::tiletype_material::HFS:
return RemoteFortressReader::HFS;
break;
case df::enums::tiletype_material::CAMPFIRE:
return RemoteFortressReader::CAMPFIRE;
break;
case df::enums::tiletype_material::FIRE:
return RemoteFortressReader::FIRE;
break;
case df::enums::tiletype_material::ASHES:
return RemoteFortressReader::ASHES;
break;
case df::enums::tiletype_material::MAGMA:
return RemoteFortressReader::MAGMA;
break;
case df::enums::tiletype_material::DRIFTWOOD:
return RemoteFortressReader::DRIFTWOOD;
break;
case df::enums::tiletype_material::POOL:
return RemoteFortressReader::POOL;
break;
case df::enums::tiletype_material::BROOK:
return RemoteFortressReader::BROOK;
break;
case df::enums::tiletype_material::RIVER:
return RemoteFortressReader::RIVER;
break;
case df::enums::tiletype_material::ROOT:
return RemoteFortressReader::ROOT;
break;
case df::enums::tiletype_material::TREE:
return RemoteFortressReader::TREE_MATERIAL;
break;
case df::enums::tiletype_material::MUSHROOM:
return RemoteFortressReader::MUSHROOM;
break;
case df::enums::tiletype_material::UNDERWORLD_GATE:
return RemoteFortressReader::UNDERWORLD_GATE;
break;
default:
return RemoteFortressReader::NO_MATERIAL;
break;
}
return RemoteFortressReader::NO_MATERIAL;
}
RemoteFortressReader::TiletypeSpecial TranslateSpecial(df::tiletype_special special)
{
switch (special)
{
case df::enums::tiletype_special::NONE:
return RemoteFortressReader::NO_SPECIAL;
break;
case df::enums::tiletype_special::NORMAL:
return RemoteFortressReader::NORMAL;
break;
case df::enums::tiletype_special::RIVER_SOURCE:
return RemoteFortressReader::RIVER_SOURCE;
break;
case df::enums::tiletype_special::WATERFALL:
return RemoteFortressReader::WATERFALL;
break;
case df::enums::tiletype_special::SMOOTH:
return RemoteFortressReader::SMOOTH;
break;
case df::enums::tiletype_special::FURROWED:
return RemoteFortressReader::FURROWED;
break;
case df::enums::tiletype_special::WET:
return RemoteFortressReader::WET;
break;
case df::enums::tiletype_special::DEAD:
return RemoteFortressReader::DEAD;
break;
case df::enums::tiletype_special::WORN_1:
return RemoteFortressReader::WORN_1;
break;
case df::enums::tiletype_special::WORN_2:
return RemoteFortressReader::WORN_2;
break;
case df::enums::tiletype_special::WORN_3:
return RemoteFortressReader::WORN_3;
break;
case df::enums::tiletype_special::TRACK:
return RemoteFortressReader::TRACK;
break;
case df::enums::tiletype_special::SMOOTH_DEAD:
return RemoteFortressReader::SMOOTH_DEAD;
break;
default:
return RemoteFortressReader::NO_SPECIAL;
break;
}
return RemoteFortressReader::NO_SPECIAL;
}
RemoteFortressReader::TiletypeShape TranslateShape(df::tiletype_shape shape)
{
switch (shape)
{
case df::enums::tiletype_shape::NONE:
return RemoteFortressReader::NO_SHAPE;
break;
case df::enums::tiletype_shape::EMPTY:
return RemoteFortressReader::EMPTY;
break;
case df::enums::tiletype_shape::FLOOR:
return RemoteFortressReader::FLOOR;
break;
case df::enums::tiletype_shape::BOULDER:
return RemoteFortressReader::BOULDER;
break;
case df::enums::tiletype_shape::PEBBLES:
return RemoteFortressReader::PEBBLES;
break;
case df::enums::tiletype_shape::WALL:
return RemoteFortressReader::WALL;
break;
case df::enums::tiletype_shape::FORTIFICATION:
return RemoteFortressReader::FORTIFICATION;
break;
case df::enums::tiletype_shape::STAIR_UP:
return RemoteFortressReader::STAIR_UP;
break;
case df::enums::tiletype_shape::STAIR_DOWN:
return RemoteFortressReader::STAIR_DOWN;
break;
case df::enums::tiletype_shape::STAIR_UPDOWN:
return RemoteFortressReader::STAIR_UPDOWN;
break;
case df::enums::tiletype_shape::RAMP:
return RemoteFortressReader::RAMP;
break;
case df::enums::tiletype_shape::RAMP_TOP:
return RemoteFortressReader::RAMP_TOP;
break;
case df::enums::tiletype_shape::BROOK_BED:
return RemoteFortressReader::BROOK_BED;
break;
case df::enums::tiletype_shape::BROOK_TOP:
return RemoteFortressReader::BROOK_TOP;
break;
case df::enums::tiletype_shape::BRANCH:
return RemoteFortressReader::BRANCH;
break;
#ifdef DF_VER_034
case df::enums::tiletype_shape::TREE:
return RemoteFortressReader::TREE;
break;
#endif
case df::enums::tiletype_shape::TRUNK_BRANCH:
return RemoteFortressReader::TRUNK_BRANCH;
break;
case df::enums::tiletype_shape::TWIG:
return RemoteFortressReader::TWIG;
break;
case df::enums::tiletype_shape::SAPLING:
return RemoteFortressReader::SAPLING;
break;
case df::enums::tiletype_shape::SHRUB:
return RemoteFortressReader::SHRUB;
break;
case df::enums::tiletype_shape::ENDLESS_PIT:
return RemoteFortressReader::EMPTY;
break;
default:
return RemoteFortressReader::NO_SHAPE;
break;
}
return RemoteFortressReader::NO_SHAPE;
}
RemoteFortressReader::TiletypeVariant TranslateVariant(df::tiletype_variant variant)
{
switch (variant)
{
case df::enums::tiletype_variant::NONE:
return RemoteFortressReader::NO_VARIANT;
break;
case df::enums::tiletype_variant::VAR_1:
return RemoteFortressReader::VAR_1;
break;
case df::enums::tiletype_variant::VAR_2:
return RemoteFortressReader::VAR_2;
break;
case df::enums::tiletype_variant::VAR_3:
return RemoteFortressReader::VAR_3;
break;
case df::enums::tiletype_variant::VAR_4:
return RemoteFortressReader::VAR_4;
break;
default:
return RemoteFortressReader::NO_VARIANT;
break;
}
return RemoteFortressReader::NO_VARIANT;
}
static command_result CheckHashes(color_ostream &stream, const EmptyMessage *in)
{
clock_t start = clock();
for (int i = 0; i < df::global::world->map.map_blocks.size(); i++)
{
df::map_block * block = df::global::world->map.map_blocks[i];
fletcher16((uint8_t*)(block->tiletype), 16 * 16 * sizeof(df::enums::tiletype::tiletype));
}
clock_t end = clock();
double elapsed_secs = double(end - start) / CLOCKS_PER_SEC;
stream.print("Checking all hashes took %f seconds.", elapsed_secs);
return CR_OK;
clock_t start = clock();
for (int i = 0; i < df::global::world->map.map_blocks.size(); i++)
{
df::map_block * block = df::global::world->map.map_blocks[i];
fletcher16((uint8_t*)(block->tiletype), 16 * 16 * sizeof(df::enums::tiletype::tiletype));
}
clock_t end = clock();
double elapsed_secs = double(end - start) / CLOCKS_PER_SEC;
stream.print("Checking all hashes took %f seconds.", elapsed_secs);
return CR_OK;
}
df::matter_state GetState(df::material * mat, uint16_t temp = 10015)
{
df::matter_state state = matter_state::Solid;
if (temp >= mat->heat.melting_point)
state = df::matter_state::Liquid;
if (temp >= mat->heat.boiling_point)
state = matter_state::Gas;
return state;
df::matter_state state = matter_state::Solid;
if (temp >= mat->heat.melting_point)
state = df::matter_state::Liquid;
if (temp >= mat->heat.boiling_point)
state = matter_state::Gas;
return state;
}
static command_result GetMaterialList(color_ostream &stream, const EmptyMessage *in, MaterialList *out)
{
if (!Core::getInstance().isWorldLoaded()) {
//out->set_available(false);
return CR_OK;
}
df::world_raws *raws = &df::global::world->raws;
MaterialInfo mat;
for (int i = 0; i < raws->inorganics.size(); i++)
{
mat.decode(0, i);
MaterialDefinition *mat_def = out->add_material_list();
mat_def->mutable_mat_pair()->set_mat_index(0);
mat_def->mutable_mat_pair()->set_mat_type(i);
mat_def->set_id(mat.getToken());
mat_def->set_name(mat.toString()); //find the name at cave temperature;
if (raws->inorganics[i]->material.state_color[GetState(&raws->inorganics[i]->material)] < raws->language.colors.size())
{
df::descriptor_color *color = raws->language.colors[raws->inorganics[i]->material.state_color[GetState(&raws->inorganics[i]->material)]];
mat_def->mutable_state_color()->set_red(color->red);
mat_def->mutable_state_color()->set_green(color->green);
mat_def->mutable_state_color()->set_blue(color->blue);
}
}
for (int i = 1; i < 19; i++)
{
int k = 0;
if (i == 7)
k = 1;// for coal.
for (int j = 0; j <= k; j++)
{
mat.decode(i, j);
MaterialDefinition *mat_def = out->add_material_list();
mat_def->mutable_mat_pair()->set_mat_index(i);
mat_def->mutable_mat_pair()->set_mat_type(j);
mat_def->set_id(mat.getToken());
mat_def->set_name(mat.toString()); //find the name at cave temperature;
if (raws->mat_table.builtin[i]->state_color[GetState(raws->mat_table.builtin[i])] < raws->language.colors.size())
{
df::descriptor_color *color = raws->language.colors[raws->mat_table.builtin[i]->state_color[GetState(raws->mat_table.builtin[i])]];
mat_def->mutable_state_color()->set_red(color->red*255);
mat_def->mutable_state_color()->set_green(color->green*255);
mat_def->mutable_state_color()->set_blue(color->blue*255);
}
}
}
for (int i = 0; i < raws->creatures.all.size(); i++)
{
df::creature_raw * creature = raws->creatures.all[i];
for (int j = 0; j < creature->material.size(); j++)
{
mat.decode(j + 19, i);
MaterialDefinition *mat_def = out->add_material_list();
mat_def->mutable_mat_pair()->set_mat_index(j+19);
mat_def->mutable_mat_pair()->set_mat_type(i);
mat_def->set_id(mat.getToken());
mat_def->set_name(mat.toString()); //find the name at cave temperature;
if (creature->material[j]->state_color[GetState(creature->material[j])] < raws->language.colors.size())
{
df::descriptor_color *color = raws->language.colors[creature->material[j]->state_color[GetState(creature->material[j])]];
mat_def->mutable_state_color()->set_red(color->red);
mat_def->mutable_state_color()->set_green(color->green);
mat_def->mutable_state_color()->set_blue(color->blue);
}
}
}
if (!Core::getInstance().isWorldLoaded()) {
//out->set_available(false);
return CR_OK;
}
df::world_raws *raws = &df::global::world->raws;
MaterialInfo mat;
for (int i = 0; i < raws->inorganics.size(); i++)
{
mat.decode(0, i);
MaterialDefinition *mat_def = out->add_material_list();
mat_def->mutable_mat_pair()->set_mat_index(0);
mat_def->mutable_mat_pair()->set_mat_type(i);
mat_def->set_id(mat.getToken());
mat_def->set_name(mat.toString()); //find the name at cave temperature;
if (raws->inorganics[i]->material.state_color[GetState(&raws->inorganics[i]->material)] < raws->language.colors.size())
{
df::descriptor_color *color = raws->language.colors[raws->inorganics[i]->material.state_color[GetState(&raws->inorganics[i]->material)]];
mat_def->mutable_state_color()->set_red(color->red * 255);
mat_def->mutable_state_color()->set_green(color->green * 255);
mat_def->mutable_state_color()->set_blue(color->blue * 255);
}
}
for (int i = 1; i < 19; i++)
{
int k = 0;
if (i == 7)
k = 1;// for coal.
for (int j = 0; j <= k; j++)
{
mat.decode(i, j);
MaterialDefinition *mat_def = out->add_material_list();
mat_def->mutable_mat_pair()->set_mat_index(i);
mat_def->mutable_mat_pair()->set_mat_type(j);
mat_def->set_id(mat.getToken());
mat_def->set_name(mat.toString()); //find the name at cave temperature;
if (raws->mat_table.builtin[i]->state_color[GetState(raws->mat_table.builtin[i])] < raws->language.colors.size())
{
df::descriptor_color *color = raws->language.colors[raws->mat_table.builtin[i]->state_color[GetState(raws->mat_table.builtin[i])]];
mat_def->mutable_state_color()->set_red(color->red * 255);
mat_def->mutable_state_color()->set_green(color->green * 255);
mat_def->mutable_state_color()->set_blue(color->blue * 255);
}
}
}
for (int i = 0; i < raws->creatures.all.size(); i++)
{
df::creature_raw * creature = raws->creatures.all[i];
for (int j = 0; j < creature->material.size(); j++)
{
mat.decode(j + 19, i);
MaterialDefinition *mat_def = out->add_material_list();
mat_def->mutable_mat_pair()->set_mat_index(j + 19);
mat_def->mutable_mat_pair()->set_mat_type(i);
mat_def->set_id(mat.getToken());
mat_def->set_name(mat.toString()); //find the name at cave temperature;
if (creature->material[j]->state_color[GetState(creature->material[j])] < raws->language.colors.size())
{
df::descriptor_color *color = raws->language.colors[creature->material[j]->state_color[GetState(creature->material[j])]];
mat_def->mutable_state_color()->set_red(color->red * 255);
mat_def->mutable_state_color()->set_green(color->green * 255);
mat_def->mutable_state_color()->set_blue(color->blue * 255);
}
}
}
for (int i = 0; i < raws->plants.all.size(); i++)
{
df::plant_raw * plant = raws->plants.all[i];
@ -226,48 +473,48 @@ static command_result GetMaterialList(color_ostream &stream, const EmptyMessage
if (plant->material[j]->state_color[GetState(plant->material[j])] < raws->language.colors.size())
{
df::descriptor_color *color = raws->language.colors[plant->material[j]->state_color[GetState(plant->material[j])]];
mat_def->mutable_state_color()->set_red(color->red);
mat_def->mutable_state_color()->set_green(color->green);
mat_def->mutable_state_color()->set_blue(color->blue);
mat_def->mutable_state_color()->set_red(color->red * 255);
mat_def->mutable_state_color()->set_green(color->green * 255);
mat_def->mutable_state_color()->set_blue(color->blue * 255);
}
}
}
return CR_OK;
return CR_OK;
}
void CopyBlock(df::map_block * DfBlock, RemoteFortressReader::MapBlock * NetBlock)
{
NetBlock->set_map_x(DfBlock->map_pos.x);
NetBlock->set_map_y(DfBlock->map_pos.y);
NetBlock->set_map_z(DfBlock->map_pos.z);
for (int yy = 0; yy < 16; yy++)
{
for (int xx = 0; xx < 16; xx++)
{
df::tiletype tile = DfBlock->tiletype[xx][yy];
NetBlock->set_map_x(DfBlock->map_pos.x);
NetBlock->set_map_y(DfBlock->map_pos.y);
NetBlock->set_map_z(DfBlock->map_pos.z);
for (int yy = 0; yy < 16; yy++)
{
for (int xx = 0; xx < 16; xx++)
{
df::tiletype tile = DfBlock->tiletype[xx][yy];
NetBlock->add_tiles(tile);
}
}
}
}
}
static command_result GetBlockList(color_ostream &stream, const BlockRequest *in, BlockList *out)
{
//stream.print("Got request for blocks from (%d, %d, %d) to (%d, %d, %d).\n", in->min_x(), in->min_y(), in->min_z(), in->max_x(), in->max_y(), in->max_z());
for (int zz = in->min_z(); zz < in->max_z(); zz++)
{
for (int yy = in->min_y(); yy < in->max_y(); yy++)
{
for (int xx = in->min_x(); xx < in->max_x(); xx++)
{
df::map_block * block = DFHack::Maps::getBlock(xx, yy, zz);
if (block == NULL)
continue;
RemoteFortressReader::MapBlock *net_block = out->add_map_blocks();
CopyBlock(block, net_block);
}
}
}
return CR_OK;
//stream.print("Got request for blocks from (%d, %d, %d) to (%d, %d, %d).\n", in->min_x(), in->min_y(), in->min_z(), in->max_x(), in->max_y(), in->max_z());
for (int zz = in->min_z(); zz < in->max_z(); zz++)
{
for (int yy = in->min_y(); yy < in->max_y(); yy++)
{
for (int xx = in->min_x(); xx < in->max_x(); xx++)
{
df::map_block * block = DFHack::Maps::getBlock(xx, yy, zz);
if (block == NULL)
continue;
RemoteFortressReader::MapBlock *net_block = out->add_map_blocks();
CopyBlock(block, net_block);
}
}
}
return CR_OK;
}
static command_result GetTiletypeList(color_ostream &stream, const EmptyMessage *in, TiletypeList *out)
@ -281,10 +528,10 @@ static command_result GetTiletypeList(color_ostream &stream, const EmptyMessage
const char * name = tileName(tt);
if (name != NULL && name[0] != 0)
type->set_caption(name);
type->set_shape((RemoteFortressReader::TiletypeShape)tileShape(tt));
type->set_special((RemoteFortressReader::TiletypeSpecial)tileSpecial(tt));
type->set_material((RemoteFortressReader::TiletypeMaterial)tileMaterial(tt));
type->set_variant((RemoteFortressReader::TiletypeVariant)tileVariant(tt));
type->set_shape(TranslateShape(tileShape(tt)));
type->set_special(TranslateSpecial(tileSpecial(tt)));
type->set_material(TranslateMaterial(tileMaterial(tt)));
type->set_variant(TranslateVariant(tileVariant(tt)));
type->set_direction(tileDirection(tt).whole);
count++;
}