2016-06-05 07:09:07 -06:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015, Michael Casadevall
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
**/
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
#include "Debug.h"
|
|
|
|
#include "LuaTools.h"
|
2016-06-05 07:09:07 -06:00
|
|
|
#include "PluginManager.h"
|
2023-07-21 05:12:01 -06:00
|
|
|
|
|
|
|
#include "modules/Persistence.h"
|
2016-06-05 07:09:07 -06:00
|
|
|
#include "modules/Units.h"
|
2023-07-21 05:12:01 -06:00
|
|
|
#include "modules/World.h"
|
2016-06-05 07:09:07 -06:00
|
|
|
|
|
|
|
#include "df/world.h"
|
|
|
|
|
|
|
|
using namespace DFHack;
|
2023-07-21 05:12:01 -06:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2016-06-05 07:09:07 -06:00
|
|
|
|
|
|
|
DFHACK_PLUGIN("dwarfvet");
|
2023-07-21 05:12:01 -06:00
|
|
|
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
|
2016-06-05 07:09:07 -06:00
|
|
|
|
|
|
|
REQUIRE_GLOBAL(world);
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
namespace DFHack {
|
|
|
|
// for configuration-related logging
|
|
|
|
DBG_DECLARE(dwarfvet, status, DebugCategory::LINFO);
|
|
|
|
// for logging during the periodic scan
|
|
|
|
DBG_DECLARE(dwarfvet, cycle, DebugCategory::LINFO);
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
static const string CONFIG_KEY = string(plugin_name) + "/config";
|
|
|
|
static PersistentDataItem config;
|
|
|
|
enum ConfigValues {
|
|
|
|
CONFIG_IS_ENABLED = 0,
|
2016-06-05 07:09:07 -06:00
|
|
|
};
|
2023-07-21 05:12:01 -06:00
|
|
|
static int get_config_val(int index) {
|
|
|
|
if (!config.isValid())
|
|
|
|
return -1;
|
|
|
|
return config.ival(index);
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
2023-07-21 05:12:01 -06:00
|
|
|
static bool get_config_bool(int index) {
|
|
|
|
return get_config_val(index) == 1;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
2023-07-21 05:12:01 -06:00
|
|
|
static void set_config_val(int index, int value) {
|
|
|
|
if (config.isValid())
|
|
|
|
config.ival(index) = value;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
2023-07-21 05:12:01 -06:00
|
|
|
static void set_config_bool(int index, bool value) {
|
|
|
|
set_config_val(index, value ? 1 : 0);
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
static const int32_t CYCLE_TICKS = 2459; // a prime number that's around 2 days
|
|
|
|
static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle
|
2016-06-05 07:09:07 -06:00
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
static command_result do_command(color_ostream &out, vector<string> ¶meters);
|
|
|
|
static void dwarfvet_cycle(color_ostream &out);
|
2016-06-05 07:09:07 -06:00
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
DFhackCExport command_result plugin_init(color_ostream &out, vector <PluginCommand> &commands) {
|
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
plugin_name,
|
|
|
|
"Allow animals to be treated at hospitals.",
|
|
|
|
do_command));
|
|
|
|
return CR_OK;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
|
|
|
|
if (!Core::getInstance().isWorldLoaded()) {
|
|
|
|
out.printerr("Cannot enable %s without a loaded world.\n", plugin_name);
|
|
|
|
return CR_FAILURE;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
if (enable != is_enabled) {
|
|
|
|
is_enabled = enable;
|
|
|
|
DEBUG(status,out).print("%s from the API; persisting\n",
|
|
|
|
is_enabled ? "enabled" : "disabled");
|
|
|
|
set_config_bool(CONFIG_IS_ENABLED, is_enabled);
|
|
|
|
} else {
|
|
|
|
DEBUG(status,out).print("%s from the API, but already %s; no action\n",
|
|
|
|
is_enabled ? "enabled" : "disabled",
|
|
|
|
is_enabled ? "enabled" : "disabled");
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
2023-07-21 05:12:01 -06:00
|
|
|
return CR_OK;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
DFhackCExport command_result plugin_load_data(color_ostream &out) {
|
|
|
|
cycle_timestamp = 0;
|
|
|
|
config = World::GetPersistentData(CONFIG_KEY);
|
2016-06-05 07:09:07 -06:00
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
if (!config.isValid()) {
|
|
|
|
DEBUG(status,out).print("no config found in this save; initializing\n");
|
|
|
|
config = World::AddPersistentData(CONFIG_KEY);
|
|
|
|
set_config_bool(CONFIG_IS_ENABLED, is_enabled);
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
is_enabled = get_config_bool(CONFIG_IS_ENABLED);
|
|
|
|
DEBUG(status,out).print("loading persisted enabled state: %s\n",
|
|
|
|
is_enabled ? "true" : "false");
|
2016-06-05 07:09:07 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
|
|
|
|
if (event == DFHack::SC_WORLD_UNLOADED) {
|
|
|
|
if (is_enabled) {
|
|
|
|
DEBUG(status,out).print("world unloaded; disabling %s\n",
|
|
|
|
plugin_name);
|
|
|
|
is_enabled = false;
|
|
|
|
}
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
2023-07-21 05:12:01 -06:00
|
|
|
return CR_OK;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
|
|
|
|
if (is_enabled && world->frame_counter - cycle_timestamp >= CYCLE_TICKS)
|
|
|
|
dwarfvet_cycle(out);
|
|
|
|
return CR_OK;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
static bool call_dwarfvet_lua(color_ostream *out, const char *fn_name,
|
|
|
|
int nargs = 0, int nres = 0,
|
|
|
|
Lua::LuaLambda && args_lambda = Lua::DEFAULT_LUA_LAMBDA,
|
|
|
|
Lua::LuaLambda && res_lambda = Lua::DEFAULT_LUA_LAMBDA) {
|
|
|
|
DEBUG(status).print("calling dwarfvet lua function: '%s'\n", fn_name);
|
2016-06-05 07:09:07 -06:00
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
CoreSuspender guard;
|
2016-06-05 07:09:07 -06:00
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
auto L = Lua::Core::State;
|
|
|
|
Lua::StackUnwinder top(L);
|
2016-06-05 07:09:07 -06:00
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
if (!out)
|
|
|
|
out = &Core::getInstance().getConsole();
|
2016-06-05 07:09:07 -06:00
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
return Lua::CallLuaModuleFunction(*out, L, "plugins.dwarfvet", fn_name,
|
|
|
|
nargs, nres,
|
|
|
|
std::forward<Lua::LuaLambda&&>(args_lambda),
|
|
|
|
std::forward<Lua::LuaLambda&&>(res_lambda));
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
static command_result do_command(color_ostream &out, vector<string> ¶meters) {
|
2016-06-05 07:09:07 -06:00
|
|
|
CoreSuspender suspend;
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
if (!Core::getInstance().isWorldLoaded()) {
|
|
|
|
out.printerr("Cannot run %s without a loaded world.\n", plugin_name);
|
|
|
|
return CR_FAILURE;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
bool show_help = false;
|
|
|
|
if (!call_dwarfvet_lua(&out, "parse_commandline", 1, 1,
|
|
|
|
[&](lua_State *L) {
|
|
|
|
Lua::PushVector(L, parameters);
|
|
|
|
},
|
|
|
|
[&](lua_State *L) {
|
|
|
|
show_help = !lua_toboolean(L, -1);
|
|
|
|
})) {
|
|
|
|
return CR_FAILURE;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
return show_help ? CR_WRONG_USAGE : CR_OK;
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
static void dwarfvet_cycle(color_ostream &out) {
|
|
|
|
// mark that we have recently run
|
|
|
|
cycle_timestamp = world->frame_counter;
|
2016-06-05 07:09:07 -06:00
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
DEBUG(cycle,out).print("running %s cycle\n", plugin_name);
|
|
|
|
call_dwarfvet_lua(&out, "checkup");
|
2016-06-05 07:09:07 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 05:12:01 -06:00
|
|
|
DFHACK_PLUGIN_LUA_FUNCTIONS {
|
|
|
|
DFHACK_LUA_FUNCTION(dwarfvet_cycle),
|
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|