Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 211 212 [213] 214 215 ... 373

Author Topic: DFHack 0.34.11 r3  (Read 983678 times)

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3180 on: March 10, 2013, 06:55:55 pm »

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):

Code: [Select]
-- 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:

Code: [Select]
#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".
« Last Edit: March 10, 2013, 06:58:20 pm by Putnam »
Logged

falconne

  • Bay Watcher
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3181 on: March 11, 2013, 12:16:23 am »

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:

I can't speak for the build errors yet as I'm at work and can't compile that to see, but I can suggest a change to the onupdate function you got from my code, to make it work better for what what you need:

Code: [Select]
DFhackCExport command_result plugin_onupdate (color_ostream &out)
{
    if(!Maps::IsValid())
        return CR_OK;

    static decltype(world->frame_counter) last_frame_count = 0;
   
    bool is_paused = Core::getInstance().getWorld()->ReadPauseState();
    if (is_paused)
        return CR_OK;

    if (world->frame_counter - last_frame_count < 150)
        return CR_OK;

    last_frame_count = world->frame_counter;

    ... DO STUFF ...

    return CR_OK;
}
Logged
Utility plugins for DFHack, for improving Dwarf Fortress' user interface.

Laggy

  • Bay Watcher
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3182 on: March 11, 2013, 01:27:55 am »

Is there some kind of reference somewhere for the data structures available to lua from the game?  Looking through a few example scripts, I saw things like df.global.world.units.all for a table with all the units in it.  But where did it come from?!  I can't seem to find any reference lists anywhere :(
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3183 on: March 11, 2013, 02:30:30 am »

library/xml in the github.

I mostly use printall(table), though...

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3184 on: March 11, 2013, 05:24:57 am »

@Putnam: i just remembered that there is onupdate in lua (or similar thing anyway...):
https://github.com/warmist/dfhack/blob/master/Lua%20API.rst#id30
Namely: dfhack.timeout(time,mode,callback). Setting it up like: dfhack.timeout(150,'ticks',callback) would work. Also if combined with:
Code: [Select]
dfhack.onStateChange.foo = function(code)
if code==SC_MAP_LOADED then --not sure if SC_MAP_LOADED or SC_WORLD_LOADED
dfhack.timeout(150,'ticks',callback) --disables if map/world is unloaded automatically
end
--and then in callback

function callback()
-- real stuff that does whatever
--
--
dfhack.timeout(150,'ticks',callback)
end

Laggy

  • Bay Watcher
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3185 on: March 11, 2013, 11:59:50 am »

library/xml in the github.

I mostly use printall(table), though...
Thanks!  I had already checked out the git repository (yea yea, I'm used to SVN, its called checkout there :P) but the library/xml folder wasn't populated.  I didn't realized I had to run a sub-module Update as well.

Also, after a bit of searching, i found the built in lua-interpreter.  That definitely makes things easier for playing around!  (fun fact:  for _,item in ipairs(df.global.world.items.all) do printall(item) end ---- in a fort with 50k items takes a while!)
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3186 on: March 11, 2013, 12:02:25 pm »

Back before I removed all my god tiers, I did a printall for the raws for every creature in the game. It took two minutes :P

Laggy

  • Bay Watcher
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3187 on: March 11, 2013, 12:14:19 pm »

Only 2?  Weak!  That printall up there took at least 5.  Maybe more, I didn't actually time it.
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3188 on: March 11, 2013, 12:46:15 pm »

Is there any way to check if a syndrome that a unit is affected by is the same as one on a material? They're stored really damn differently. If not, it'd be pretty easy to just make modders have their syndrome end at 150.

Laggy

  • Bay Watcher
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3189 on: March 11, 2013, 01:36:43 pm »

So playing around with this a bit as a learning exercise, I made a script to remove all the wear from all the items in your fort.  If anyone is interested:

Spoiler: removewear.lua (click to show/hide)


In an unrelated question, is there like...a list of material IDs somewhere or something?  Like, i just picked a random item, and printall shows that mat_type = 30, and mat_index = 536.  I know that one of those has to refer to Silk, but for the life of me I can't find the struct that has that info in it.  Attempting to cross-reference all this stuff at this point has just been an exercise in futility, basically just printall'ing one table after another hoping I find something.
« Last Edit: March 11, 2013, 01:44:55 pm by Laggy »
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3190 on: March 11, 2013, 01:48:38 pm »

Yeah, I kinda need to know about mat_types too--where to find a struct for the ones that aren't 0 (inorganic).

Japa

  • Bay Watcher
  • Spoopy time!
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3191 on: March 11, 2013, 02:18:41 pm »

The first 18 mat types are listed here: https://github.com/angavrilov/df-structures/blob/master/df.materials.xml

These ones are fixed. After that, they're split into plant mats and creature mats, but which are which is raw-dependent.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3192 on: March 11, 2013, 04:20:50 pm »

Related to the earlier issue with lua:

Code: [Select]
local function teleport(unit)
local playerblockoccupancy = dfhack.maps.getTileBlock(player.pos).occupancy
player.pos.x = unitToTeleportTo.pos.x-1
player.pos.y = unitToTeleportTo.pos.y
player.pos.z = unitToTeleportTo.pos.z
for k,v in ipairs(playerblockoccupancy) do
for k,v in ipairs(v) do
v.unit = false
v.unit_grounded = false
end
end
end

This seems a bit stupid, but a bit of testing reveals that it works. Should I expect anything weird out of this?

Laggy

  • Bay Watcher
    • View Profile
Re: DFHack 0.34.11 r2
« Reply #3193 on: March 11, 2013, 06:53:34 pm »

Here's another little utility mod I made for myself (*****WARNING******* Back up your save before using, just in case.  I tested this for my game, but it could potentially cause problems.  I hope not, but just be safe.) to delete all my excess useless stone.  I had something like 20,000 stone in my fort, and it took up a lot of space (or walking time to dump into a Quantum Stockpile, or time marking a ton of stuff to dump and then using autodump).

Spoiler: destroy_stone.lua (click to show/hide)

Change the destroyThese table entries for the stones you want to destroy.  I'm using Masterwork mod, and I only wanted to keep Obsidian, and metal-bearing ore/stones that were ~steel quality or better.  Adjust per your needs/wants. 
Logged

Quietust

  • Bay Watcher
  • Does not suffer fools gladly
    • View Profile
    • QMT Productions
Re: DFHack 0.34.11 r2
« Reply #3194 on: March 11, 2013, 08:59:35 pm »

This seems a bit stupid, but a bit of testing reveals that it works. Should I expect anything weird out of this?
Yes - that'll clear the unit occupany bits on all tiles in that map block, not just the one formerly occupied by your player.
Additionally:
1. It isn't bothering to set the proper occupancy bits in the destination tile.
2. It isn't checking occupancy in the destination tile to see if the player needs to be set Prone.
3. It isn't even checking if the destination tile is valid - for all you know, your target could be standing next to a wall, and your player will end up inside the wall. Even better, your target could be standing next to the edge of the map, resulting in you being teleported off of the map.
Logged
P.S. If you don't get this note, let me know and I'll write you another.
It's amazing how dwarves can make a stack of bones completely waterproof and magmaproof.
It's amazing how they can make an entire floodgate out of the bones of 2 cats.
Pages: 1 ... 211 212 [213] 214 215 ... 373