Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: DFHack plugin development - How can I get started?  (Read 1934 times)

sagenth

  • Bay Watcher
    • View Profile
DFHack plugin development - How can I get started?
« on: December 12, 2017, 12:56:48 am »

I want to develop a dfhack plugin to export functions for random number generation.

The C++ random number distributions are looking quite appealing for a lua script I'm developing. There are some table entries I need to randomly select. I want to exhaust all selection options, randomly, in the fewest iterations necessary. So a std::uniform_int_distribution would be quite fitting for this task. Even creating a custom number distribution would be easier written in C++ over lua. A custom number distribution wherein: I create an array of my selection options (ie. a bunch of indices), then shuffle the array to a random order, and finally "generate" numbers by iterating through it.

I've looked over dfhack documentation, but there doesn't seem to be anything covering the creation of plugins. I've never worked with lua before last week, so I'm not even sure how exporting functions works.

  • How can I get started?
  • Do plugins require dfhack source files to integrate/build?
  • Do I need to clone the entire repo? If so can anybody point me to a thread that covers getting setup? (preferrably in visual studio 2013-2017)
  • Could I instead create my plugin without any of the dfhack source files, then drop it in? I feel this would be too convenient to be possible.



« Last Edit: December 12, 2017, 01:02:43 am by sagenth »
Logged

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: DFHack plugin development - How can I get started?
« Reply #1 on: December 12, 2017, 01:19:11 am »

I want to develop a dfhack plugin to export functions for random number generation.

The C++ random number distributions are looking quite appealing for a lua script I'm developing. There are some table entries I need to randomly select. I want to exhaust all selection options, randomly, in the fewest iterations necessary. So a std::uniform_int_distribution would be quite fitting for this task. Even creating a custom number distribution would be easier written in C++ over lua. A custom number distribution wherein: I create an array of my selection options (ie. a bunch of indices), then shuffle the array to a random order, and finally "generate" numbers by iterating through it.

I've looked over dfhack documentation, but there doesn't seem to be anything covering the creation of plugins. I've never worked with lua before last week, so I'm not even sure how exporting functions works.

  • How can I get started?
  • Do plugins require dfhack source files to integrate/build?
  • Do I need to clone the entire repo? If so can anybody point me to a thread that covers getting setup? (preferrably in visual studio 2013-2017)
  • Could I instead create my plugin without any of the dfhack source files, then drop it in? I feel this would be too convenient to be possible.

So there is a module that already has Mersenne twister implemented (before we could use c++XX) in a module . It could be extended to have more random stuff exposed to lua.

On the other hand module vs plugin (AFAIK) only difference is that modules are "baked in" into main library so other plugins/modules can easily use them.

  • See: Compiling docs and Any files in here . There is also "skeleton plugin" but imho it's more complicated than it needs to be.
  • Yes and no. Yes it needs for easiest build and integration. But there are ways to compile it without compiling whole dfhack. I think only mifki (of TWBT and other cool stuff) does that.
  • Yes cloning entire repo is preferable. Currently we are in "new df version chaos" so pull-req are not being handled (afaik) and the compiling link should cover setup
  • There are ways to do it... I did it by using online Continous Integration thing, but it does full dfhack compile (30min) so developing is slow and i'm using it only when i'm too lazy to update dfhack away from from my main pc.
As always: you can post your issues in dfhack thread or on irc channel

sagenth

  • Bay Watcher
    • View Profile
Re: DFHack plugin development - How can I get started?
« Reply #2 on: December 12, 2017, 02:22:11 am »

Holy hell there's a new version of DF! Haha, I had no idea.

Thanks my dude. I think I'll get started tomorrow afternoon. I got it downloaded, but I'll get to reading the compile doc and find that skeleton file. I checked out a few of the plugins while you were typing your reply and they seem to be following different standards and practices. rename.cpp seems pretty solid though, and fastdwarf.cpp#L248 seems handy too. I'm sure the skeleton file will clear some things up.

I wonder if I can figure out how by reading up on the TWBT source code (I assume it is available). If not I may want some elaboration on "online Continuous Integration thing" cause the search results on google are dubious.
Logged

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: DFHack plugin development - How can I get started?
« Reply #3 on: December 12, 2017, 07:07:59 am »

Holy hell there's a new version of DF! Haha, I had no idea.

Thanks my dude. I think I'll get started tomorrow afternoon. I got it downloaded, but I'll get to reading the compile doc and find that skeleton file. I checked out a few of the plugins while you were typing your reply and they seem to be following different standards and practices. rename.cpp seems pretty solid though, and fastdwarf.cpp#L248 seems handy too. I'm sure the skeleton file will clear some things up.

I wonder if I can figure out how by reading up on the TWBT source code (I assume it is available). If not I may want some elaboration on "online Continuous Integration thing" cause the search results on google are dubious.

Twbt stuff: repo
About plugins: see this file the ones using Lua are more towards what you want. Some plugins do init/deinit stuff, some don't have any state and only add a command to dfhack. Yours should probably only export some lua functions.
As for CI I used this: Appveyor . Still suprised that someone could spare the cpu time needed (and disk space) for stuff like this - FOR FREE...

sagenth

  • Bay Watcher
    • View Profile
Re: DFHack plugin development - How can I get started?
« Reply #4 on: December 12, 2017, 01:46:35 pm »

I guess today's the day. I finally learn about make files, sigh.. I've put this off for years, and I thought I'd be able to die without having done it.
Logged

sagenth

  • Bay Watcher
    • View Profile
Re: DFHack plugin development - How can I get started?
« Reply #5 on: December 13, 2017, 05:03:25 am »

I think I'm all setup to develop my cxxrandom plugin now, but I'm unsure how to import plugin functions into a lua script.
require('plugin.<name>') seems to be the correct syntax for importing, although the docs specify mkmodule('plugin.<name>') which did nothing but complain with errors.

The next step would be to give a shot at calling a function from a plugin. I can't seem to do that, so I'm guessing I'm either trying wrong or I imported the plugin incorrectly. I'm testing with the burrows plugin. Can somebody tell me how this is supposed to work?
Logged

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: DFHack plugin development - How can I get started?
« Reply #6 on: December 13, 2017, 06:30:00 am »

I think I'm all setup to develop my cxxrandom plugin now, but I'm unsure how to import plugin functions into a lua script.
require('plugin.<name>') seems to be the correct syntax for importing, although the docs specify mkmodule('plugin.<name>') which did nothing but complain with errors.

The next step would be to give a shot at calling a function from a plugin. I can't seem to do that, so I'm guessing I'm either trying wrong or I imported the plugin incorrectly. I'm testing with the burrows plugin. Can somebody tell me how this is supposed to work?

I think it needs something like this: almost empty lua file to work. Don't know the exact reason why though...

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack plugin development - How can I get started?
« Reply #7 on: December 13, 2017, 03:04:16 pm »

It's because Lua's require() requires a file to actually exist, and I guess it was easier to make stub files like that than to patch require(). To clarify, mkmodule() should be in the plugins/lua/some_plugin.lua file, which gets installed at hack/lua/plugins/some_plugin.lua, and can then be loaded with require('plugins.some_plugin').

Also, check out dfhack.random if you haven't yet.

You shouldn't have to deal with makefiles directly (unless you meant CMake files), because CMake generates those. (Changing a Makefile generated by CMake is probably not a good idea because it'll get overwritten.)
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.

sagenth

  • Bay Watcher
    • View Profile
Re: DFHack plugin development - How can I get started?
« Reply #8 on: December 13, 2017, 03:58:50 pm »

Okay, I drop a file into ./plugins/lua/ to do the mkmodule call. I see though that burrows has a file in there already, which likely explains why I could call require('plugins.burrows') in my test.lua file.

I couldn't call any of the exported lua functions however, how am I s
I was going to ask how calling imported functions was supposed to work, and then I realized it probably has a standard answer across lua. Which would explain why nobody answered that part.

bur = require('plugins.burrow')
bur.findByName('blah',0)
>Burrow not found: 'blah'

Thanks google!

Thanks to you guys too. I'm just gonna drop the file in there since it is a lua file, so I'm assuming that is how it is supposed to go.
Logged