Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3

Author Topic: [SCRIPTING] DFHack Lua/Ruby scripting questions thread  (Read 5601 times)

Nopenope

  • Bay Watcher
    • View Profile
[SCRIPTING] DFHack Lua/Ruby scripting questions thread
« on: August 10, 2016, 03:29:53 pm »

The original DFHack megathread is getting huge and cluttered, so I figured a separate thread for specific scripting questions would probably be a good idea (the main thread can still be used for things like DFHack core development and whatnot).

I'll start with a question of mine: is it possible to get the current weather state through scripting in fortress mode? I'm specifically thinking about clouds, fog and moon phase (though the latter can be checked indirectly with the current date I guess).
Logged

Atomic Chicken

  • Bay Watcher
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #1 on: August 14, 2016, 02:37:34 am »

The moon phase can be easily obtained from
Code: [Select]
df.global.world.world_data.moon_phase
Code: (values) [Select]
0 = full moon
1-4 = waning gibbous
5-8 = waning half
9-12 = waning crescent
13-14 = new moon
15-18 = waxing crescent
19-22 = waxing half
23-26 = waxing gibbous
27 = full moon

You could check out weather.lua for help with the weather stuff.
Logged
As mentioned in the previous turn, the most exciting field of battle this year will be in the Arstotzkan capitol, with plenty of close-quarter fighting and siege warfare.  Arstotzka, accordingly, spent their design phase developing a high-altitude tactical bomber. 

figment

  • Bay Watcher
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #2 on: August 24, 2016, 09:55:22 pm »

I really tend to mostly play Adventure mode because DF is one of the more interesting rogue-likes and while its got a ways to go its got quite a bit to like.   Anyway, I generally find the inventory management to be a little annoying and searching through items is not terribly fun when I probably won't pick any of it up but feel compelled to scour just in case something good is lying around.

I was thinking about of doing something like what Cataclysm: DDA did with their side bars. Basically list everything within eyesight in a side bar to the right. And use the cursor to navigate up and down while showing detail of the item at the bottom. It supports filtering by name and grouping by type.  Its helps with navigating, filtering quickly through whatever is nearby.  With tab key it allows swapping between items and npcs/monsters to get the gist of where everything around you.

Example:
Spoiler (click to show/hide)

It would also be nice to use the Surrounding area inventory manager which allows you drop stuff on any of the 9 surrounding squares or your inventory without having to go through the get/drop/remove menus for each item one at a time.

Example:
Spoiler (click to show/hide)

Anyway, the question part is has this already been done or anything like it?  I dont really need to reinvent the wheel if already done.  Or any examples of scripts that have similar complexity that I should review first? 

Will the dfhack gui handle this?  Presumably yes because I've done menus in the past though nothing this complicated.  Anything I should be aware of if I try something like this?    I did a site picker for my teleport mod which used the listpicker but this seems to be a bit more complex.   I've done some ruby in the past but I'm most familiar with lua with dfhack so would try in lua. 
Logged

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #3 on: August 25, 2016, 04:04:50 am »

I've had the same idea not long ago. Yes lua is perfect for this. You can look at gui folder in scripts (forgot how the companion tool was called)

figment

  • Bay Watcher
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #4 on: August 25, 2016, 11:09:48 pm »

Thanks.  So I've started writing some preliminary scripts like one to just sort by distance of player to quickly show items and filter out junk.   One thing I've run into is I'm not sure how to filter out items that cannot be picked up like coffer, table, throne, ... furniture-type stuff.  I'm sure there are others but I'd just want to show things that normally only show up in the get menu.   

Is there a quick way or do I need to build something based on itemtype and filter it that way which seems more error prone.   For now I'll use the item:isFurniture(false) function but it seems easy to miss other cases like that. 

Next a visibility filter would be nice otherwise will stick to small radius.   Not sure how to do this yet but I guess it would be to check the screen for this somehow on the grid cell.

Is is possible to force focus the df window back to front with focus while running a script from console?  Not sure this is a good idea for production but I keep having focus issues and would use this as a debug aide.   On a related note, moving the mouse to center of the df window would be useful for testing.  ( I will assume no on both counts but just asking)
Logged

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #5 on: August 26, 2016, 03:45:58 am »

Thanks.  So I've started writing some preliminary scripts like one to just sort by distance of player to quickly show items and filter out junk.   One thing I've run into is I'm not sure how to filter out items that cannot be picked up like coffer, table, throne, ... furniture-type stuff.  I'm sure there are others but I'd just want to show things that normally only show up in the get menu.   

Is there a quick way or do I need to build something based on itemtype and filter it that way which seems more error prone.   For now I'll use the item:isFurniture(false) function but it seems easy to miss other cases like that. 

Next a visibility filter would be nice otherwise will stick to small radius.   Not sure how to do this yet but I guess it would be to check the screen for this somehow on the grid cell.

Is is possible to force focus the df window back to front with focus while running a script from console?  Not sure this is a good idea for production but I keep having focus issues and would use this as a debug aide.   On a related note, moving the mouse to center of the df window would be useful for testing.  ( I will assume no on both counts but just asking)
You should filter by item.flags.onground (or similar) all other items are either in container (general_ref_contained?) or in buildings (also depends if it's structural part of building or just on floor in building)

Dirst

  • Bay Watcher
  • [EASILY_DISTRA
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #6 on: August 26, 2016, 11:59:54 am »

Thanks.  So I've started writing some preliminary scripts like one to just sort by distance of player to quickly show items and filter out junk.   One thing I've run into is I'm not sure how to filter out items that cannot be picked up like coffer, table, throne, ... furniture-type stuff.  I'm sure there are others but I'd just want to show things that normally only show up in the get menu.   

Is there a quick way or do I need to build something based on itemtype and filter it that way which seems more error prone.   For now I'll use the item:isFurniture(false) function but it seems easy to miss other cases like that. 

Next a visibility filter would be nice otherwise will stick to small radius.   Not sure how to do this yet but I guess it would be to check the screen for this somehow on the grid cell.

Is is possible to force focus the df window back to front with focus while running a script from console?  Not sure this is a good idea for production but I keep having focus issues and would use this as a debug aide.   On a related note, moving the mouse to center of the df window would be useful for testing.  ( I will assume no on both counts but just asking)
You can center the cursor with
Code: [Select]
local dm = require('gui.dwarfmode')
local size_x = dm.getPanelLayout().map.width
local size_y = dm.getPanelLayout().map.height
df.global.cursor.x = df.global.window_x + math.floor(size_x / 2)
df.global.cursor.y = df.global.window_y + math.floor(size_y / 2)

Unfortunately if TWBT is active, this "center" will be several tiles right of the real center of the screen.
Logged
Just got back, updating:
(0.42 & 0.43) The Earth Strikes Back! v2.15 - Pay attention...  It's a mine!  It's-a not yours!
(0.42 & 0.43) Appearance Tweaks v1.03 - Tease those hippies about their pointy ears.
(0.42 & 0.43) Accessibility Utility v1.04 - Console tools to navigate the map

figment

  • Bay Watcher
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #7 on: August 26, 2016, 11:18:03 pm »

You should filter by item.flags.onground (or similar) all other items are either in container (general_ref_contained?) or in buildings (also depends if it's structural part of building or just on floor in building)

I settled on the following to see items for now.   The foreign flag is debatable but needed to see items in stores which you can see from normal map so seems reasonable. Will probably just decorate the name with $ and provide a filter option to hide those.  Still kept the furniture check since those still show up as containers on the ground.

Code: [Select]
local match_items = function (x)
    return x.pos.z == center.z
            and (x.flags.on_ground or x.flags.foreign)
            and item_dist(x) <= MAX_VIEW_DIST
            and not x:isFurniture(false)
end

I also found the fog_of_war and designation flags in map_block which I'm using to filter the list to visible items.  Not sure fow is useful here though.

Next questions:  Is there an approved/recommended way of saving configuration settings?  Like an init file or something for lua scripts.  I would probably want to save last used settings and possibly give config settings like width of sidebar or max search radius.  Looks like dwarfmonitor uses a json file in dfhack-config for that.


Edit:
New major issue.  I never noticed but the custom lua screens always do full screen painting. So as I try to make a custom sidebar its always over a black screen.  While I can make this work, it would be so much better if I can just overlay the existing screen as a sidebar.  Are there known issues or reasons why this is always done?
« Last Edit: August 27, 2016, 02:12:49 pm by figment »
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #8 on: August 27, 2016, 03:16:04 pm »

JSON is good for global settings. It hasn't been used for much save-specific stuff yet, although there is a project underway for that.

New major issue.  I never noticed but the custom lua screens always do full screen painting. So as I try to make a custom sidebar its always over a black screen.  While I can make this work, it would be so much better if I can just overlay the existing screen as a sidebar.  Are there known issues or reasons why this is always done?

Are you using TwbT?
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.

mifki

  • Bay Watcher
  • works secretly...
    • View Profile
    • mifki
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #9 on: August 28, 2016, 05:47:12 pm »

JSON is good for global settings. It hasn't been used for much save-specific stuff yet, although there is a project underway for that.

New major issue.  I never noticed but the custom lua screens always do full screen painting. So as I try to make a custom sidebar its always over a black screen.  While I can make this work, it would be so much better if I can just overlay the existing screen as a sidebar.  Are there known issues or reasons why this is always done?

Are you using TwbT?

Can you post a screenshot? If the screen is completely blank, you're probably not calling the underlying screen's render()?

However, this isn't going to work this way anyway with TWBT because if it detects any screen on top of the main screen, it won't render the map. And actually, I think you should not implement it as a separate screen, but rather interpose (in C++) the main screen's render() method to draw your stuff in the sidebar, as many other plugins do.

lethosor

  • Bay Watcher
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #10 on: August 28, 2016, 08:28:11 pm »

Many sidebars are written in lua too (as separate screens), and  C++ isn't necessary for any of them.
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.

mifki

  • Bay Watcher
  • works secretly...
    • View Profile
    • mifki
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #11 on: August 28, 2016, 10:39:18 pm »

Many sidebars are written in lua too (as separate screens), and  C++ isn't necessary for any of them.

And they stay on screen all the time?

lethosor

  • Bay Watcher
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #12 on: August 28, 2016, 10:42:48 pm »

Until you close them, if that's what you mean. See gui/liquids, gui/siege-engine, etc. for examples.
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.

mifki

  • Bay Watcher
  • works secretly...
    • View Profile
    • mifki
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #13 on: August 28, 2016, 10:53:19 pm »

Until you close them, if that's what you mean. See gui/liquids, gui/siege-engine, etc. for examples.

I thought what figment is doing was a real sidebar that stays open and shows some additional information while the game is running. That can't be done as a Lua screen.

Crab

  • Bay Watcher
    • View Profile
Re: [SCRIPTING] DFHack Lua/Ruby scripting questions thread
« Reply #14 on: August 29, 2016, 03:33:58 am »

Is there any good tutorial for learning how lua scripting works in DF context for someone with no prior experience?
Logged
Pages: [1] 2 3