2020-06-29 19:35:07 -06:00
|
|
|
// Dynamically enables and disables cooking restrictions for plants and seeds
|
|
|
|
// in order to limit the number of seeds available for each crop type
|
|
|
|
|
|
|
|
// With thanks to peterix for DFHack and Quietust for information
|
|
|
|
// http://www.bay12forums.com/smf/index.php?topic=91166.msg2605147#msg2605147
|
2011-10-24 09:53:31 -06:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Core.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "modules/World.h"
|
2020-06-29 19:35:07 -06:00
|
|
|
#include "modules/Materials.h"
|
2018-05-17 18:09:57 -06:00
|
|
|
#include "modules/Kitchen.h"
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "VersionInfo.h"
|
2012-01-16 20:29:09 -07:00
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/plant_raw.h"
|
2012-01-16 19:16:16 -07:00
|
|
|
#include "df/item_flags.h"
|
2012-02-01 07:09:11 -07:00
|
|
|
#include "df/items_other_id.h"
|
2011-10-24 09:53:31 -06:00
|
|
|
|
2012-02-01 07:09:11 -07:00
|
|
|
using namespace std;
|
2012-01-16 20:22:42 -07:00
|
|
|
using namespace DFHack;
|
2012-01-21 17:31:15 -07:00
|
|
|
using namespace df::enums;
|
2012-01-16 20:29:09 -07:00
|
|
|
|
2014-12-06 16:47:35 -07:00
|
|
|
DFHACK_PLUGIN("seedwatch");
|
2013-09-30 03:19:51 -06:00
|
|
|
DFHACK_PLUGIN_IS_ENABLED(running); // whether seedwatch is counting the seeds or not
|
2011-10-24 09:53:31 -06:00
|
|
|
|
2014-12-06 16:47:35 -07:00
|
|
|
REQUIRE_GLOBAL(world);
|
|
|
|
|
|
|
|
const int buffer = 20; // seed number buffer - 20 is reasonable
|
|
|
|
|
2011-10-29 19:52:25 -06:00
|
|
|
// abbreviations for the standard plants
|
2012-02-01 07:09:11 -07:00
|
|
|
map<string, string> abbreviations;
|
2011-10-24 09:53:31 -06:00
|
|
|
|
2012-01-16 19:16:16 -07:00
|
|
|
bool ignoreSeeds(df::item_flags& f) // seeds with the following flags should not be counted
|
2011-10-24 09:53:31 -06:00
|
|
|
{
|
2011-10-26 19:31:13 -06:00
|
|
|
return
|
2012-01-16 19:16:16 -07:00
|
|
|
f.bits.dump ||
|
|
|
|
f.bits.forbid ||
|
2012-03-30 00:44:52 -06:00
|
|
|
f.bits.garbage_collect ||
|
2012-01-16 19:16:16 -07:00
|
|
|
f.bits.hidden ||
|
|
|
|
f.bits.hostile ||
|
|
|
|
f.bits.on_fire ||
|
|
|
|
f.bits.rotten ||
|
|
|
|
f.bits.trader ||
|
|
|
|
f.bits.in_building ||
|
|
|
|
f.bits.in_job;
|
2011-10-24 09:53:31 -06:00
|
|
|
};
|
|
|
|
|
2011-10-26 19:31:13 -06:00
|
|
|
// searches abbreviations, returns expansion if so, returns original if not
|
2012-02-01 07:09:11 -07:00
|
|
|
string searchAbbreviations(string in)
|
2011-10-24 09:53:31 -06:00
|
|
|
{
|
2011-10-26 19:31:13 -06:00
|
|
|
if(abbreviations.count(in) > 0)
|
|
|
|
{
|
|
|
|
return abbreviations[in];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return in;
|
|
|
|
}
|
2011-10-24 09:53:31 -06:00
|
|
|
};
|
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
|
|
|
|
{
|
2021-01-30 08:14:46 -07:00
|
|
|
if(enable == true)
|
|
|
|
{
|
|
|
|
if(Core::getInstance().isWorldLoaded())
|
|
|
|
{
|
|
|
|
running = true;
|
|
|
|
out.print("seedwatch supervision started.\n");
|
|
|
|
} else {
|
|
|
|
out.printerr(
|
|
|
|
"This plugin needs a fortress to be loaded and will deactivate automatically otherwise.\n"
|
|
|
|
"Activate with 'seedwatch start' after you load the game.\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
running = false;
|
|
|
|
out.print("seedwatch supervision stopped.\n");
|
|
|
|
}
|
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result df_seedwatch(color_ostream &out, vector<string>& parameters)
|
2011-10-24 09:53:31 -06:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
CoreSuspender suspend;
|
2011-10-24 09:53:31 -06:00
|
|
|
|
2020-06-29 19:35:07 -06:00
|
|
|
map<string, int32_t> plantIDs;
|
2012-02-01 07:09:11 -07:00
|
|
|
for(size_t i = 0; i < world->raws.plants.all.size(); ++i)
|
2011-10-26 19:31:13 -06:00
|
|
|
{
|
2022-07-31 15:22:21 -06:00
|
|
|
auto & plant = world->raws.plants.all[i];
|
|
|
|
if (plant->material_defs.type[plant_material_def::seed] != -1)
|
|
|
|
plantIDs[plant->id] = i;
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
2011-10-24 09:53:31 -06:00
|
|
|
|
2012-01-16 20:22:42 -07:00
|
|
|
t_gamemodes gm;
|
2012-10-06 03:46:20 -06:00
|
|
|
World::ReadGameMode(gm);// FIXME: check return value
|
2011-10-29 19:52:25 -06:00
|
|
|
|
|
|
|
// if game mode isn't fortress mode
|
2015-03-09 18:17:09 -06:00
|
|
|
if(gm.g_mode != game_mode::DWARF || !World::isFortressMode(gm.g_type))
|
2011-10-29 19:52:25 -06:00
|
|
|
{
|
|
|
|
// just print the help
|
2022-07-31 14:28:29 -06:00
|
|
|
return CR_WRONG_USAGE;
|
2011-10-29 19:52:25 -06:00
|
|
|
}
|
|
|
|
|
2012-02-01 07:09:11 -07:00
|
|
|
string par;
|
2011-10-26 19:31:13 -06:00
|
|
|
int limit;
|
|
|
|
switch(parameters.size())
|
|
|
|
{
|
|
|
|
case 0:
|
2013-10-30 14:58:14 -06:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
2011-10-26 19:31:13 -06:00
|
|
|
case 1:
|
|
|
|
par = parameters[0];
|
2013-10-30 14:58:14 -06:00
|
|
|
if ((par == "help") || (par == "?"))
|
|
|
|
{
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
2011-10-26 19:31:13 -06:00
|
|
|
else if(par == "start")
|
|
|
|
{
|
2021-01-30 08:14:46 -07:00
|
|
|
plugin_enable(out, true);
|
2021-01-29 04:26:46 -07:00
|
|
|
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
else if(par == "stop")
|
|
|
|
{
|
2021-01-30 08:14:46 -07:00
|
|
|
plugin_enable(out, false);
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
else if(par == "clear")
|
|
|
|
{
|
2012-01-16 20:22:42 -07:00
|
|
|
Kitchen::clearLimits();
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("seedwatch watchlist cleared\n");
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
else if(par == "info")
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("seedwatch Info:\n");
|
2011-10-26 19:31:13 -06:00
|
|
|
if(running)
|
|
|
|
{
|
2022-07-31 14:28:29 -06:00
|
|
|
out.print("seedwatch is supervising. Use 'disable seedwatch' to stop supervision.\n");
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-31 14:28:29 -06:00
|
|
|
out.print("seedwatch is not supervising. Use 'enable seedwatch' to start supervision.\n");
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
2020-06-29 19:35:07 -06:00
|
|
|
map<int32_t, int16_t> watchMap;
|
2012-01-16 20:22:42 -07:00
|
|
|
Kitchen::fillWatchMap(watchMap);
|
2011-10-26 19:31:13 -06:00
|
|
|
if(watchMap.empty())
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("The watch list is empty.\n");
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("The watch list is:\n");
|
2020-06-29 19:35:07 -06:00
|
|
|
for(auto i = watchMap.begin(); i != watchMap.end(); ++i)
|
2011-10-26 19:31:13 -06:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("%s : %u\n", world->raws.plants.all[i->first]->id.c_str(), i->second);
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(par == "debug")
|
|
|
|
{
|
2020-06-29 19:35:07 -06:00
|
|
|
map<int32_t, int16_t> watchMap;
|
2012-01-16 20:22:42 -07:00
|
|
|
Kitchen::fillWatchMap(watchMap);
|
2012-03-10 04:55:42 -07:00
|
|
|
Kitchen::debug_print(out);
|
2011-10-29 19:52:25 -06:00
|
|
|
}
|
2011-10-26 19:31:13 -06:00
|
|
|
else
|
|
|
|
{
|
2012-02-01 07:09:11 -07:00
|
|
|
string token = searchAbbreviations(par);
|
2020-06-29 19:35:07 -06:00
|
|
|
if(plantIDs.count(token) > 0)
|
2011-10-26 19:31:13 -06:00
|
|
|
{
|
2020-06-29 19:35:07 -06:00
|
|
|
Kitchen::removeLimit(plantIDs[token]);
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("%s is not being watched\n", token.c_str());
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("%s has not been found as a material.\n", token.c_str());
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
limit = atoi(parameters[1].c_str());
|
|
|
|
if(limit < 0) limit = 0;
|
|
|
|
if(parameters[0] == "all")
|
|
|
|
{
|
2022-07-31 15:22:21 -06:00
|
|
|
for(auto & entry : plantIDs)
|
|
|
|
Kitchen::setLimit(entry.second, limit);
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-01 07:09:11 -07:00
|
|
|
string token = searchAbbreviations(parameters[0]);
|
2020-06-29 19:35:07 -06:00
|
|
|
if(plantIDs.count(token) > 0)
|
2011-10-26 19:31:13 -06:00
|
|
|
{
|
2020-06-29 19:35:07 -06:00
|
|
|
Kitchen::setLimit(plantIDs[token], limit);
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("%s is being watched.\n", token.c_str());
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("%s has not been found as a material.\n", token.c_str());
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-10-30 14:58:14 -06:00
|
|
|
return CR_WRONG_USAGE;
|
2011-10-26 19:31:13 -06:00
|
|
|
break;
|
|
|
|
}
|
2011-10-24 09:53:31 -06:00
|
|
|
|
2012-01-16 20:22:42 -07:00
|
|
|
return CR_OK;
|
2011-10-24 09:53:31 -06:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_init(color_ostream &out, vector<PluginCommand>& commands)
|
2011-10-24 09:53:31 -06:00
|
|
|
{
|
2022-07-31 14:28:29 -06:00
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"seedwatch",
|
|
|
|
"Toggles seed cooking based on quantity available.",
|
|
|
|
df_seedwatch));
|
2011-10-26 19:31:13 -06:00
|
|
|
// fill in the abbreviations map, with abbreviations for the standard plants
|
|
|
|
abbreviations["bs"] = "SLIVER_BARB";
|
|
|
|
abbreviations["bt"] = "TUBER_BLOATED";
|
|
|
|
abbreviations["bw"] = "WEED_BLADE";
|
|
|
|
abbreviations["cw"] = "GRASS_WHEAT_CAVE";
|
|
|
|
abbreviations["dc"] = "MUSHROOM_CUP_DIMPLE";
|
|
|
|
abbreviations["fb"] = "BERRIES_FISHER";
|
|
|
|
abbreviations["hr"] = "ROOT_HIDE";
|
|
|
|
abbreviations["kb"] = "BULB_KOBOLD";
|
|
|
|
abbreviations["lg"] = "GRASS_LONGLAND";
|
|
|
|
abbreviations["mr"] = "ROOT_MUCK";
|
|
|
|
abbreviations["pb"] = "BERRIES_PRICKLE";
|
|
|
|
abbreviations["ph"] = "MUSHROOM_HELMET_PLUMP";
|
|
|
|
abbreviations["pt"] = "GRASS_TAIL_PIG";
|
|
|
|
abbreviations["qb"] = "BUSH_QUARRY";
|
|
|
|
abbreviations["rr"] = "REED_ROPE";
|
|
|
|
abbreviations["rw"] = "WEED_RAT";
|
|
|
|
abbreviations["sb"] = "BERRY_SUN";
|
|
|
|
abbreviations["sp"] = "POD_SWEET";
|
|
|
|
abbreviations["vh"] = "HERB_VALLEY";
|
2020-06-29 19:35:07 -06:00
|
|
|
abbreviations["ws"] = "BERRIES_STRAW";
|
2011-10-26 19:31:13 -06:00
|
|
|
abbreviations["wv"] = "VINE_WHIP";
|
2012-01-16 20:22:42 -07:00
|
|
|
return CR_OK;
|
2011-10-24 09:53:31 -06:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
|
2011-12-30 07:12:15 -07:00
|
|
|
{
|
2021-01-30 16:08:17 -07:00
|
|
|
if (event == SC_MAP_UNLOADED) {
|
2011-12-30 07:12:15 -07:00
|
|
|
if (running)
|
2021-01-30 16:08:17 -07:00
|
|
|
out.print("seedwatch deactivated due to game unload\n");
|
2011-12-30 07:12:15 -07:00
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
|
2012-01-16 20:22:42 -07:00
|
|
|
return CR_OK;
|
2011-12-30 07:12:15 -07:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_onupdate(color_ostream &out)
|
2011-10-24 09:53:31 -06:00
|
|
|
{
|
2011-12-30 07:12:15 -07:00
|
|
|
if (running)
|
2011-10-26 19:31:13 -06:00
|
|
|
{
|
2011-12-30 07:12:15 -07:00
|
|
|
// reduce processing rate
|
|
|
|
static int counter = 0;
|
|
|
|
if (++counter < 500)
|
2012-01-16 20:22:42 -07:00
|
|
|
return CR_OK;
|
2011-12-30 07:12:15 -07:00
|
|
|
counter = 0;
|
|
|
|
|
2012-01-16 20:22:42 -07:00
|
|
|
t_gamemodes gm;
|
2012-10-06 03:46:20 -06:00
|
|
|
World::ReadGameMode(gm);// FIXME: check return value
|
2011-10-29 19:52:25 -06:00
|
|
|
// if game mode isn't fortress mode
|
2015-03-09 18:17:09 -06:00
|
|
|
if(gm.g_mode != game_mode::DWARF || !World::isFortressMode(gm.g_type))
|
2011-10-29 19:52:25 -06:00
|
|
|
{
|
|
|
|
// stop running.
|
|
|
|
running = false;
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("seedwatch deactivated due to game mode switch\n");
|
2012-01-16 20:22:42 -07:00
|
|
|
return CR_OK;
|
2011-10-29 19:52:25 -06:00
|
|
|
}
|
|
|
|
// this is dwarf mode, continue
|
2020-06-29 19:35:07 -06:00
|
|
|
map<int32_t, int16_t> seedCount; // the number of seeds
|
2012-01-16 20:29:09 -07:00
|
|
|
|
2011-10-29 19:52:25 -06:00
|
|
|
// count all seeds and plants by RAW material
|
2012-02-01 07:09:11 -07:00
|
|
|
for(size_t i = 0; i < world->items.other[items_other_id::SEEDS].size(); ++i)
|
2011-10-29 19:52:25 -06:00
|
|
|
{
|
2020-06-29 19:35:07 -06:00
|
|
|
df::item *item = world->items.other[items_other_id::SEEDS][i];
|
|
|
|
MaterialInfo mat(item);
|
|
|
|
if (!mat.isPlant())
|
|
|
|
continue;
|
|
|
|
if (!ignoreSeeds(item->flags))
|
|
|
|
++seedCount[mat.plant->index];
|
2011-10-29 19:52:25 -06:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:35:07 -06:00
|
|
|
map<int32_t, int16_t> watchMap;
|
2012-01-16 20:22:42 -07:00
|
|
|
Kitchen::fillWatchMap(watchMap);
|
2011-10-29 19:52:25 -06:00
|
|
|
for(auto i = watchMap.begin(); i != watchMap.end(); ++i)
|
2011-10-26 19:31:13 -06:00
|
|
|
{
|
|
|
|
if(seedCount[i->first] <= i->second)
|
|
|
|
{
|
2012-01-16 20:22:42 -07:00
|
|
|
Kitchen::denyPlantSeedCookery(i->first);
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
else if(i->second + buffer < seedCount[i->first])
|
|
|
|
{
|
2012-01-16 20:22:42 -07:00
|
|
|
Kitchen::allowPlantSeedCookery(i->first);
|
2011-10-26 19:31:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-16 20:22:42 -07:00
|
|
|
return CR_OK;
|
2011-10-24 09:53:31 -06:00
|
|
|
}
|
|
|
|
|
2012-01-16 20:22:42 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown(Core* pCore)
|
2011-10-24 09:53:31 -06:00
|
|
|
{
|
2012-01-16 20:22:42 -07:00
|
|
|
return CR_OK;
|
2012-02-13 21:54:08 -07:00
|
|
|
}
|