Merge remote-tracking branch 'origin/perSaveScripts' into scriptOrganization

Conflicts:
	plugins/CMakeLists.txt
develop
expwnent 2014-07-07 09:01:07 -04:00
commit 0a16bc2e12
12 changed files with 488 additions and 55 deletions

@ -59,10 +59,6 @@ keybinding add Ctrl-Shift-Z@dwarfmode/Default "stocks show"
# open an overview window summarising some stocks (dfstatus)
keybinding add Ctrl-Shift-I@dwarfmode/Default "gui/dfstatus"
# Workflow
keybinding add Ctrl-W@dwarfmode/QueryBuilding/Some "gui/workflow"
keybinding add Ctrl-I "gui/workflow status"
# q->stockpile; p - copy & paste stockpiles
keybinding add Alt-P copystock

@ -393,6 +393,26 @@ static bool try_autocomplete(color_ostream &con, const std::string &first, std::
return false;
}
string findScript(string path, string name) {
//first try the save folder if it exists
string save = World::ReadWorldFolder();
if ( save != "" ) {
string file = path + "/data/save/" + save + "/raw/scripts/" + name;
if (fileExists(file)) {
return file;
}
}
string file = path + "/raw/scripts/" + name;
if (fileExists(file)) {
return file;
}
file = path + "/hack/scripts/" + name;
if (fileExists(file)) {
return file;
}
return "";
}
command_result Core::runCommand(color_ostream &con, const std::string &first, vector<string> &parts)
{
if (!first.empty())
@ -446,18 +466,20 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
return CR_OK;
}
}
auto filename = getHackPath() + "scripts/" + parts[0];
if (fileExists(filename + ".lua"))
{
string help = getScriptHelp(filename + ".lua", "-- ");
string path = this->p->getPath();
string file = findScript(path, parts[0] + ".lua");
if ( file != "" ) {
string help = getScriptHelp(file, "-- ");
con.print("%s: %s\n", parts[0].c_str(), help.c_str());
return CR_OK;
}
if (plug_mgr->ruby && plug_mgr->ruby->is_enabled() && fileExists(filename + ".rb"))
{
string help = getScriptHelp(filename + ".rb", "# ");
con.print("%s: %s\n", parts[0].c_str(), help.c_str());
return CR_OK;
if (plug_mgr->ruby && plug_mgr->ruby->is_enabled() ) {
file = findScript(path, parts[0] + ".rb");
if ( file != "" ) {
string help = getScriptHelp(file, "# ");
con.print("%s: %s\n", parts[0].c_str(), help.c_str());
return CR_OK;
}
}
con.printerr("Unknown command: %s\n", parts[0].c_str());
}
@ -765,15 +787,19 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
command_result res = plug_mgr->InvokeCommand(con, first, parts);
if(res == CR_NOT_IMPLEMENTED)
{
auto filename = getHackPath() + "scripts/" + first;
std::string completed;
if (fileExists(filename + ".lua"))
string completed;
string path = this->p->getPath();
string filename = findScript(path, first + ".lua");
bool lua = filename != "";
if ( !lua ) {
filename = findScript(path, first + ".rb");
}
if ( lua )
res = runLuaScript(con, first, parts);
else if (plug_mgr->ruby && plug_mgr->ruby->is_enabled() && fileExists(filename + ".rb"))
else if ( filename != "" && plug_mgr->ruby && plug_mgr->ruby->is_enabled() )
res = runRubyScript(con, plug_mgr, first, parts);
else if (try_autocomplete(con, first, completed))
return CR_NOT_IMPLEMENTED;// runCommand(con, completed, parts);
else if ( try_autocomplete(con, first, completed) )
return CR_NOT_IMPLEMENTED;
else
con.printerr("%s is not a recognized command.\n", first.c_str());
}
@ -811,13 +837,23 @@ bool Core::loadScriptFile(color_ostream &out, string fname, bool silent)
}
}
static void run_dfhack_init(color_ostream &out, Core *core)
{
if (!core->loadScriptFile(out, "dfhack.init", true))
{
core->runCommand(out, "gui/no-dfhack-init");
core->loadScriptFile(out, "dfhack.init-example", true);
}
}
// Load dfhack.init in a dedicated thread (non-interactive console mode)
void fInitthread(void * iodata)
{
IODATA * iod = ((IODATA*) iodata);
Core * core = iod->core;
color_ostream_proxy out(core->getConsole());
core->loadScriptFile(out, "dfhack.init", true);
run_dfhack_init(out, core);
}
// A thread function... for the interactive console.
@ -837,7 +873,7 @@ void fIOthread(void * iodata)
return;
}
core->loadScriptFile(con, "dfhack.init", true);
run_dfhack_init(con, core);
con.print("DFHack is ready. Have a nice day!\n"
"Type in '?' or 'help' for general help, 'ls' to see all commands.\n");

@ -371,20 +371,41 @@ internal.scripts = internal.scripts or {}
local scripts = internal.scripts
local hack_path = dfhack.getHackPath()
function dfhack.run_script(name,...)
local key = string.lower(name)
local function findScript(name)
local file = hack_path..'scripts/'..name..'.lua'
local env = scripts[key]
if dfhack.filesystem.exists(file) then
return file
end
file = dfhack.getSavePath()
if file then
file = file .. '/raw/scripts/' .. name .. '.lua'
if dfhack.filesystem.exists(file) then
return file
end
end
file = hack_path..'../raw/scripts/' .. name .. '.lua'
if dfhack.filesystem.exists(file) then
return file
end
return nil
end
function dfhack.run_script(name,...)
local file = findScript(name)
if not file then
error('Could not find script ' .. name)
end
local env = scripts[file]
if env == nil then
env = {}
setmetatable(env, { __index = base_env })
end
local f,perr = loadfile(file, 't', env)
if f == nil then
error(perr)
if f then
scripts[file] = env
return f(...)
end
scripts[key] = env
return f(...)
error(perr)
end
local function _run_command(...)

@ -1404,10 +1404,12 @@ std::string DFHack::Units::getCasteProfessionName(int race, int casteid, df::pro
{
std::string prof, race_prefix;
if (pid < (df::profession)0 || !is_valid_enum_item(pid))
return "";
bool use_race_prefix = (race >= 0 && race != df::global::ui->race_id);
if (pid < (df::profession)0 || !is_valid_enum_item(pid))
return "";
int16_t current_race = df::global::ui->race_id;
if (df::global::gamemode && *df::global::gamemode == df::game_mode::ADVENTURE)
current_race = world->units.active[0]->race;
bool use_race_prefix = (race >= 0 && race != current_race);
if (auto creature = df::creature_raw::find(race))
{

@ -1 +1 @@
Subproject commit 8c0d23090539be98bc9c67b9070cbe080383ae9f
Subproject commit c66ab33071842bcfb7d37c3993f6a024923ca358

@ -137,7 +137,9 @@ if (BUILD_SUPPORTED)
DFHACK_PLUGIN(prospector prospector.cpp)
DFHACK_PLUGIN(power-meter power-meter.cpp LINK_LIBRARIES lua)
DFHACK_PLUGIN(regrass regrass.cpp)
DFHACK_PLUGIN(remotefortressreader remotefortressreader.cpp PROTOBUFS RemoteFortressReader)
DFHACK_PLUGIN(rename rename.cpp LINK_LIBRARIES lua PROTOBUFS rename)
add_subdirectory(rendermax)
DFHACK_PLUGIN(resume resume.cpp)
DFHACK_PLUGIN(reveal reveal.cpp)
DFHACK_PLUGIN(search search.cpp)
@ -165,7 +167,3 @@ OPTION(BUILD_SKELETON "Build the skeleton plugin." OFF)
if(BUILD_SKELETON)
add_subdirectory(skeleton)
endif()
OPTION(BUILD_RENDERMAX "Build the rendermax alt-renderers plugin." OFF)
if(BUILD_RENDERMAX)
add_subdirectory(rendermax)
endif()

@ -197,14 +197,12 @@ static command_result GetEmbarkInfo(color_ostream &stream, const MapRequest *in,
out->set_available(false);
return CR_OK;
}
if(!in->has_save_folder()) { //probably should send the stuff anyway, but nah.
out->set_available(false);
return CR_OK;
}
if(!(in->save_folder() == df::global::world->cur_savegame.save_dir)) { //isoworld has a different map loaded, don't bother trying to load tiles for it, we don't have them.
out->set_available(false);
return CR_OK;
}
if (in->has_save_folder()) { //If no save folder is given, it means we don't care.
if (!(in->save_folder() == df::global::world->cur_savegame.save_dir || in->save_folder() == "ANY")) { //isoworld has a different map loaded, don't bother trying to load tiles for it, we don't have them.
out->set_available(false);
return CR_OK;
}
}
out->set_available(true);
out->set_current_year(*df::global::cur_year);
out->set_current_season(*df::global::cur_season);
@ -346,14 +344,12 @@ static command_result GetRawNames(color_ostream &stream, const MapRequest *in, R
out->set_available(false);
return CR_OK;
}
if(!in->has_save_folder()) { //probably should send the stuff anyway, but nah.
out->set_available(false);
return CR_OK;
}
if(!(in->save_folder() == df::global::world->cur_savegame.save_dir)) { //isoworld has a different map loaded, don't bother trying to load tiles for it, we don't have them.
out->set_available(false);
return CR_OK;
}
if (in->has_save_folder()) { //If no save folder is given, it means we don't care.
if (!(in->save_folder() == df::global::world->cur_savegame.save_dir || in->save_folder() == "ANY")) { //isoworld has a different map loaded, don't bother trying to load tiles for it, we don't have them.
out->set_available(false);
return CR_OK;
}
}
out->set_available(true);
for(int i = 0; i < df::global::world->raws.inorganics.size(); i++){
out->add_inorganic(df::global::world->raws.inorganics[i]->id);

@ -0,0 +1,116 @@
package RemoteFortressReader;
//Attempts to provide a complete framework for reading everything from a fortress needed for vizualization
option optimize_for = LITE_RUNTIME;
//We use shapes, etc, because the actual tiletypes may differ between DF versions.
enum TiletypeShape {
NO_SHAPE = -1;
EMPTY = 0;
FLOOR = 1;
BOULDER = 2;
PEBBLES = 3;
WALL = 4;
FORTIFICATION = 5;
STAIR_UP = 6;
STAIR_DOWN = 7;
STAIR_UPDOWN = 8;
RAMP = 9;
RAMP_TOP = 10;
BROOK_BED = 11;
BROOK_TOP = 12;
TREE = 13;
SAPLING = 14;
SHRUB = 15;
ENDLESS_PIT = 16;
}
enum TiletypeSpecial {
NO_SPECIAL = -1;
NORMAL = 0;
RIVER_SOURCE = 1;
WATERFALL = 2;
SMOOTH = 3;
FURROWED = 4;
WET = 5;
DEAD = 6;
WORN_1 = 7;
WORN_2 = 8;
WORN_3 = 9;
TRACK = 10;
};
enum TiletypeMaterial {
NO_MATERIAL = -1;
AIR = 0;
SOIL = 1;
STONE = 2;
FEATURE = 3;
LAVA_STONE = 4;
MINERAL = 5;
FROZEN_LIQUID = 6;
CONSTRUCTION = 7;
GRASS_LIGHT = 8;
GRASS_DARK = 9;
GRASS_DRY = 10;
GRASS_DEAD = 11;
PLANT = 12;
HFS = 13;
CAMPFIRE = 14;
FIRE = 15;
ASHES = 16;
MAGMA = 17;
DRIFTWOOD = 18;
POOL = 19;
BROOK = 20;
RIVER = 21;
}
message MapBlock
{
required int32 map_x = 1;
required int32 map_y = 2;
required int32 map_z = 3;
repeated TiletypeShape tiletype_shapes = 4;
repeated TiletypeSpecial tiletype_specials = 5;
repeated TiletypeMaterial tiletype_materials = 6;
}
message MatPair {
required int32 mat_type = 1;
required int32 mat_index = 2;
}
message ColorDefinition {
required int32 red = 1;
required int32 green = 2;
required int32 blue = 3;
}
message MaterialDefinition{
required MatPair mat_pair = 1;
optional string id = 2;
optional string name = 3;
optional ColorDefinition state_color = 4; //Simplifying colors to assume room temperature.
}
message MaterialList{
repeated MaterialDefinition material_list = 1;
}
message BlockRequest
{
optional int32 blocks_needed = 1;
optional int32 min_x = 2;
optional int32 max_x = 3;
optional int32 min_y = 4;
optional int32 max_y = 5;
optional int32 min_z = 6;
optional int32 max_z = 7;
}
message BlockList
{
repeated MapBlock map_blocks = 1;
optional int32 map_x = 2;
optional int32 map_y = 3;
}

@ -0,0 +1,235 @@
// This is a generic plugin that does nothing useful apart from acting as an example... of a plugin that does nothing :D
// some headers required for a plugin. Nothing special, just the basics.
#include "Core.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
// DF data structure definition headers
#include "DataDefs.h"
#include "df/world.h"
#include "df/ui.h"
#include "df/item.h"
#include "df/creature_raw.h"
#include "df/caste_raw.h"
#include "df/body_part_raw.h"
#include "df/historical_figure.h"
#include "df/job_item.h"
#include "df/job_material_category.h"
#include "df/dfhack_material_category.h"
#include "df/matter_state.h"
#include "df/material_vec_ref.h"
#include "df/builtin_mats.h"
#include "df/descriptor_color.h"
#include "df/descriptor_pattern.h"
#include "df/descriptor_shape.h"
#include "df/physical_attribute_type.h"
#include "df/mental_attribute_type.h"
#include <df/color_modifier_raw.h>
//DFhack specific headers
#include "modules/Maps.h"
#include "modules/MapCache.h"
#include "modules/Materials.h"
#include "TileTypes.h"
//Needed for writing the protobuff stuff to a file.
#include <vector>
#include "RemoteFortressReader.pb.h"
#include "RemoteServer.h"
using namespace DFHack;
using namespace df::enums;
using namespace RemoteFortressReader;
using namespace std;
// Here go all the command declarations...
// mostly to allow having the mandatory stuff on top of the file and commands on the bottom
static command_result GetMaterialList(color_ostream &stream, const EmptyMessage *in, MaterialList *out);
static command_result GetBlockList(color_ostream &stream, const BlockRequest *in, BlockList *out);
void CopyBlock(df::map_block * DfBlock, RemoteFortressReader::MapBlock * NetBlock);
void FindChangedBlocks();
// A plugin must be able to return its name and version.
// The name string provided must correspond to the filename - skeleton.plug.so or skeleton.plug.dll in this case
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)
{
//// Fill the command list with your commands.
//commands.push_back(PluginCommand(
// "isoworldremote", "Dump north-west embark tile to text file for debug purposes.",
// isoWorldRemote, false, /* true means that the command can't be used from non-interactive user interface */
// // Extended help string. Used by CR_WRONG_USAGE and the help command:
// " This command does nothing at all.\n"
// "Example:\n"
// " isoworldremote\n"
// " Does nothing.\n"
//));
return CR_OK;
}
DFhackCExport RPCService *plugin_rpcconnect(color_ostream &)
{
RPCService *svc = new RPCService();
svc->addFunction("GetMaterialList", GetMaterialList);
svc->addFunction("GetBlockList", GetBlockList);
return svc;
}
// This is called right before the plugin library is removed from memory.
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
// in a zombie state, but things won't crash.
return CR_OK;
}
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;
}
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;
}
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);
mat_def->mutable_state_color()->set_green(color->green);
mat_def->mutable_state_color()->set_blue(color->blue);
}
}
}
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);
}
}
}
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->add_tiletype_shapes((RemoteFortressReader::TiletypeShape)tileShape(tile));
NetBlock->add_tiletype_materials((RemoteFortressReader::TiletypeMaterial)tileMaterial(tile));
NetBlock->add_tiletype_specials((RemoteFortressReader::TiletypeSpecial)tileSpecial(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;
}

@ -1 +1 @@
Subproject commit c0a2a9a2a6909cc79f9a564c8039caf8103010a8
Subproject commit 0d19548402932c970c8a0d45404808cbe8fe1bcd

@ -13,6 +13,8 @@
#include "modules/Gui.h"
#include "TileTypes.h"
#include "df/deep_vein_hollow.h"
using namespace DFHack;
using namespace df::enums;
using df::global::world;

@ -0,0 +1,31 @@
-- Shows the warning about missing configuration file.
local gui = require 'gui'
local dlg = require 'gui.dialogs'
local dfhack_init = { text = 'dfhack.init', pen = COLOR_LIGHTCYAN }
local dfhack_init_example = { text = 'dfhack.init-example', pen = COLOR_LIGHTCYAN }
local message = {
'The ', dfhack_init, ' configuration file is missing. To customize', NEWLINE,
'your DFHack installation, rename the ', dfhack_init_example, ' file', NEWLINE,
'to ', dfhack_init, ' and edit it to suit your needs.', NEWLINE, NEWLINE,
'For now, ', dfhack_init_example, ' will be used instead.'
}
dfhack.print('\n')
for k,v in ipairs(message) do
if type(v) == 'table' then
dfhack.color(v.pen)
dfhack.print(v.text)
else
dfhack.color(COLOR_YELLOW)
dfhack.print(v)
end
end
dfhack.color(COLOR_RESET)
dfhack.print('\n\n')
dlg.showMessage('DFHack is not configured', message, COLOR_YELLOW)