Bah. I'm trying to implement a version of this code that I wrote that will work automatically (as opposed to requiring the user to activate the script in the console):
-- Checks if creature has an item equipped with a special syndrome and applies item's syndrome if it is.
local function getMaterial(item)
if item.item.mat_type~=0 then return nil end --Can't find out how to get leather and such out, which is annoying but definitely not crippling, unless you want magic dragon scale'
return df.global.world.raws.inorganics[item.item.mat_index]
end
local function getSyndrome(material)
if #material.material.syndrome>0 then return material.material.syndrome[0] end
return nil
end
local function syndromeIsDfHackSyndrome(syndrome)
for k,v in ipairs(syndrome.syn_class) do
if v.value=="DFHACK_ITEM_SYNDROME" then
return true
end
end
return false
end
local function assignSyndrome(target,syn_id) --taken straight from here, but edited so I can understand it better: https://gist.github.com/warmist/4061959/
if target==nil then
qerror("Not a valid target") --this probably won't happen :V
end
local newSyndrome=df.unit_syndrome:new()
local target_syndrome=df.syndrome.find(syn_id)
newSyndrome.type=target_syndrome.id
--newSyndrome.year=
--newSyndrome.year_time=
newSyndrome.ticks=1
newSyndrome.unk1=1
for k,v in ipairs(target_syndrome.ce) do
local sympt=df.unit_syndrome.T_symptoms:new()
sympt.ticks=1
sympt.flags=2
newSyndrome.symptoms:insert("#",sympt)
end
target.syndromes.active:insert("#",newSyndrome)
end
local function findItems()
for _uid,unit in ipairs(df.global.world.units.all) do
for _,item in ipairs(unit.inventory) do
if getMaterial(item)~=nil then
local syndrome = getSyndrome(getMaterial(item))
if syndromeIsDfHackSyndrome(syndrome) then assignSyndrome(unit,syndrome.id) end
end
end
end
end
findItems()
I'm mainly having build issues with my C++ code, but I'm 90% sure my cobbled-together C++ code won't build anyway, especially since the code relies entirely on 1. Functions I don't know how to use and 2. functions I'm not sure exist:
#include "PluginManager.h"
#include "Export.h"
#include "DataDefs.h"
#include "Core.h"
#include "modules/EventManager.h"
#include "df/creature_raw.h"
#include "df/global_objects.h"
#include "df/item.h"
#include "df/syndrome.h"
#include "df/unit_syndrome.h"
#include "df/ui.h"
#include "df/unit.h"
#include "df/general_ref.h"
#include "df/general_ref_type.h"
#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using namespace DFHack;
/*Using this plugin:
Armor with a material that has [SYN_CLASS:DFHACK_ITEM_SYNDROME] will affect the person who wears it.
For example:
//////////////////////////////////////////////
//Simple example syndrome that will cause the equipper to take half damage
[SYNDROME]
[SYN_CLASS:DFHACK_ITEM_SYNDROME]
[CE_MATERIAL_FORCE_MULTIPLIER:MAT_MULT:NONE:NONE:1:2]
//////////////////////////////////////////////
*/
//Code is mostly taken from autoSyndrome.
bool enabled = true;
command_result itemSyndrome(color_ostream& out, vector<string>& parameters);
DFhackCExport command_result plugin_init(color_ostream& out, vector<PluginCommand> &commands) {
commands.push_back(PluginCommand("itemSyndrome", "Automatically give units syndromes when they wear certain materials on armors specified in the raws\n", &itemSyndrome, false,
"itemSyndrome:\n"
" itemSyndrome 0 //disable\n"
" itemSyndrome 1 //enable\n"
" itemSyndrome disable //disable\n"
" itemSyndrome enable //enable\n"
"\n"
"itemSyndrome looks for units wearing armor or holding weapons that are made of a material with a syndrome that has a certain SYN_CLASS, causing the creature who equips that item to gain the syndrome.\n"
"\n"
"Requirement:\n"
" 1) The armor worn must have a material that has a syndrome with the class \"DFHACK_ARMOR_SYNDROME\"\n"
"\n"
"When this conditions are met, the unit wearing the armor will become afflicted with all applicable syndromes associated with the material of the armor, or stones. It should correctly check for whether the creature or caste is affected or immune, and it should also correctly account for affected and immune creature classes.\n"
));
return CR_OK;
}
DFHACK_PLUGIN("itemSyndrome");
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
enabled = false;
return CR_OK;
}
DFhackCExport command_result plugin_onupdate(color_ostream &out) //Copy+pasted from some of Falconne's code because I'm not that good at this :V
{
if (!enabled)
return CR_OK;
// Every 150 frames check for syndromes
static unsigned cnt = 0;
if ((++cnt % 150) != 0 && first_update_done)
return CR_OK;
if (!first_update_done)
{
last_frame_count = world->frame_counter;
run_script(utility/itemsyndrome)
first_update_done = true;
}
return CR_OK;
}
The lua code works for a fact; the C++ code--again, 20% build problems, 80% "I have no idea what I'm doing".