Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 107 108 [109] 110 111 ... 243

Author Topic: DFHack 50.13-r2.1  (Read 830726 times)

Roses

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1620 on: January 31, 2019, 08:29:18 pm »

We actually submitted a bug report to GitHub about that (it's because the scripts submodule uses a relative URL now, ../../DFHack/scripts). They know it's broken but it's presumably not a high-priority issue.

That's been an issue for years on github. I spent a good month refactoring a large code base for use on github with submodules when they figured out that relative paths were causing issues.

A couple questions;

1. If I tie a save file function into the onUnload event should I save to current or the region save file?
2. For the GUI stuff is there a way to distinguish between the arrow keys, numpad keys, and numbers for the keys or are they all treated the same?
3. Are unit or item ids every reused while playing? I'm guessing historical unit ids are unique, but several of my scripts assume unit ids are also unique.
4. There are a lot of great functions available in dfhack.units and the other dfhack.BLANK stuff. If I was interested in porting some of my functions that I currently use that I think other people might find useful from my lua functions to built in functions how should I go about that? (I am familiar with writing C code so that isn't an issue)

EDIT: Another question I forgot, is there an easy way to detect when custom view screens "turn on and off". My detailed unit viewer currently let's you access the gui/gm-editor for the unit directly for it, but if you change anything in the editor you have to exit out of the viewer and start it again. I was hoping to be able to automatically reload my viewer when exiting the editor but my tests with screen:onShow haven't been successful

EDIT2: Do we know the speed change calculations for calculating gait speed based on strength/agility? I wrote a small script to calculate the kph value of a unit based on the gaits file in the raws but it doesn't takes into account and modifiers (the current dhack.units.computeMovementSpeed always returns 0)
« Last Edit: January 31, 2019, 09:08:37 pm by Roses »
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1621 on: February 01, 2019, 06:29:15 pm »

1. If I tie a save file function into the onUnload event should I save to current or the region save file?
That is probably too late to access any world data. It might fire after DF copies the current folder to the region folder too. If you can't do it any earlier, you might have to save to the region folder, but even that name might not be accessible any more. I would experiment with saving something to current and see where that ends up.
Quote
2. For the GUI stuff is there a way to distinguish between the arrow keys, numpad keys, and numbers for the keys or are they all treated the same?
You should be using DF's key IDs, which depend on the keybindings that are set. Use STANDARDSCROLL_UP/DOWN. when you're scrolling through a list and CURSOR_UP/etc. when you're moving a cursor (like the X on the map) around the screen. DF and DFHack don't have a way to distinguish between arrow keys and number keys once they've been translated to IDs.
Quote
3. Are unit or item ids every reused while playing? I'm guessing historical unit ids are unique, but several of my scripts assume unit ids are also unique.
Don't know about this. They might potentially be reused in different forts in the same world, but I doubt it. Histfig IDs are unique (I'm pretty sure those are the IDs used in the unit-X.dat files in the save folders). If you're concerned, you could tack on the fort/site ID as well for whatever you're doing.
Quote
4. There are a lot of great functions available in dfhack.units and the other dfhack.BLANK stuff. If I was interested in porting some of my functions that I currently use that I think other people might find useful from my lua functions to built in functions how should I go about that? (I am familiar with writing C code so that isn't an issue)
Files that need to be changed:
https://github.com/DFHack/dfhack/blob/master/library/modules/Units.cpp (for dfhack.units; other files for other modules, of course)
https://github.com/DFHack/dfhack/blob/master/library/include/modules/Units.h (again, depends on the module)
https://github.com/DFHack/dfhack/blob/master/library/LuaApi.cpp (usually just a WRAP macro works)
https://github.com/DFHack/dfhack/blob/master/docs/Lua%20API.rst
Ideally the changelog too, but that's not as important.

Quote
EDIT: Another question I forgot, is there an easy way to detect when custom view screens "turn on and off". My detailed unit viewer currently let's you access the gui/gm-editor for the unit directly for it, but if you change anything in the editor you have to exit out of the viewer and start it again. I was hoping to be able to automatically reload my viewer when exiting the editor but my tests with screen:onShow haven't been successful
Looking at https://github.com/DFHack/dfhack/blob/master/plugins/manipulator.cpp, I think the logic() vmethod gets called periodically when the screen is active (render() should too - logic() might be onIdle() in higher Lua APIs, but render() or onRender() or whatever you're using should work for your purpose). Your screen could set a flag when it opens the screen, and the next time logic() or render() are called, it could refresh itself and reset the flag. "do_refresh_names" is the flag manipulator uses. Does that help?
Quote
EDIT2: Do we know the speed change calculations for calculating gait speed based on strength/agility? I wrote a small script to calculate the kph value of a unit based on the gaits file in the raws but it doesn't takes into account and modifiers (the current dhack.units.computeMovementSpeed always returns 0)
Not seeing anything in the XML files about it, sorry. Someone else might know.
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.

Roses

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1622 on: February 02, 2019, 05:06:54 pm »

--snip--

Thanks, I played around with saving through the unUnload function and it is indeed after DF copies the current folder to the region folder. In fact if you use the getSavePath() function you will get nil back. It's not a big deal though since I am saving into the current folder regularly, so everything is already getting correctly copied over to the region folder.
Logged

Roses

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1623 on: February 09, 2019, 05:19:26 pm »

Is there a way to access the plant raw strings like we can access the creature raw strings? I figure the plant_raw.anon_1 is the list of strings similar to creature_raw.raws, but it's all userdata entries.
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1624 on: February 09, 2019, 07:37:27 pm »

Is there a way to access the plant raw strings like we can access the creature raw strings? I figure the plant_raw.anon_1 is the list of strings similar to creature_raw.raws, but it's all userdata entries.
Yup:
Code: [Select]
[lua]# ~df.reinterpret_cast('string',df.plant_raw.find(0).anon_1[0])
<string: 0x112e5b010>
value                  = [NAME:single-grain wheat]
Obviously don't use this in a script, but I'll add a name to the xml files.
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.

Roses

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1625 on: February 10, 2019, 01:30:51 pm »

Is there a way to access the plant raw strings like we can access the creature raw strings? I figure the plant_raw.anon_1 is the list of strings similar to creature_raw.raws, but it's all userdata entries.
Yup:
Code: [Select]
[lua]# ~df.reinterpret_cast('string',df.plant_raw.find(0).anon_1[0])
<string: 0x112e5b010>
value                  = [NAME:single-grain wheat]
Obviously don't use this in a script, but I'll add a name to the xml files.

Great, perfect!
Logged

Roses

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1626 on: February 13, 2019, 12:19:29 pm »

Is there a way to access a non-existent item in a DF struct without it crashing? I seem to recall doing something once with a pcall, but I don't quite remember the right syntax. For instance, with a normal table I can do
Code: [Select]
a = Table.FOO or BARand if the table Table doesn't have the FOO entry I will get BAR. But I can't do
Code: [Select]
a = itemRaw.adjective or ""because some item raws don't have the adjective entry, so it will give me an error instead of just a blank string.
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1627 on: February 13, 2019, 01:08:35 pm »

To be clear, you don't mean actually crashing, right? Crashes aren't possible to prevent after the fact.

pcall takes a function to call (optionally with arguments) and returns a boolean representing whether an error occurred, followed by the function's return values or the error. So something like this should work:
Code: [Select]
field_exists = pcall(function() return object.some_field end)
Something like this ought to be in utils.lua or exposed through the C++/Lua bridge...
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.

Roses

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1628 on: February 13, 2019, 02:56:18 pm »

To be clear, you don't mean actually crashing, right? Crashes aren't possible to prevent after the fact.

pcall takes a function to call (optionally with arguments) and returns a boolean representing whether an error occurred, followed by the function's return values or the error. So something like this should work:
Code: [Select]
field_exists = pcall(function() return object.some_field end)
Something like this ought to be in utils.lua or exposed through the C++/Lua bridge...

Yes, sorry, crashing is definitely not what it is doing. It is just printing an error "Cannot read field itemdef_foodst.adjective: not found". And it's only printing the error because I am looping through all the item raws and didn't really want to take the time to handle each itemdef separately. The pcall trick is exactly what I need, granted it would be awesome if trying to access itemdef_foodst.adjective just returned nil instead of an error, but this works just as well
« Last Edit: February 13, 2019, 03:03:24 pm by Roses »
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1629 on: February 13, 2019, 03:31:07 pm »

Worth noting that pcall will be significantly slower than handling individual types, if that's something you can do, so don't use the pcall approach in anything performance-intensive. I assume there are good reasons for not returning nil for nonexistent fields, although I wasn't around when that decision was made - it's probably better for those accesses to fail early instead of silently succeeding for typos and such.
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.

Roses

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1630 on: February 13, 2019, 04:36:40 pm »

Worth noting that pcall will be significantly slower than handling individual types, if that's something you can do, so don't use the pcall approach in anything performance-intensive. I assume there are good reasons for not returning nil for nonexistent fields, although I wasn't around when that decision was made - it's probably better for those accesses to fail early instead of silently succeeding for typos and such.

I suppose I could just do a
Code: [Select]
b={}
for k,v in pairs(itemdef_XXX) do
    b[k]=v
end
adjective = b.adjective or ""

Would that be faster than pcall? I know a list of if statements would probably be faster, but could also get very long
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1631 on: February 13, 2019, 06:37:00 pm »

Almost certainly slower if you're running the loop more than once. Maybe a table of types you know have the field you want, indexed by the type so you don't have to do a linear scan, would work and be cleaner than an if statement chain.
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.

Roses

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1632 on: February 14, 2019, 12:08:22 pm »

Almost certainly slower if you're running the loop more than once. Maybe a table of types you know have the field you want, indexed by the type so you don't have to do a linear scan, would work and be cleaner than an if statement chain.

That's a good idea actually, then I can have a table that I can use across multiple scripts, it only needs to be generated at startup and I can add/remove things from it easily as things change. No idea why I didn't think of that, it's a much more elegant solution than a list of if statements and avoids constant loops and pcalls. Thanks!
Logged

Roses

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1633 on: February 15, 2019, 11:55:06 pm »

Trying to use the onBuildingCreatedDestroyed eventful trigger, it worked in the past when I tested it, but now, instead of returning the buliding_id it seems to just be returning a random number, a different number in each world I've generated, but the same number every time. I've reloaded the eventful plugin several times and even tried completely closing DF and reopening and starting a new world, but nothing seems to help.

This is the eventful code, fairly simple but not working.
Code: [Select]
eventful.enableEvent(eventful.eventType.BUILDING, 10)
eventful.onBuildingCreatedDestroyed.buildingTrigger = function(buildingID)
 print(buildingID)
 building = df.building.find(buildingID)
 if building then
  checkBuildingCreated(buildingID)
 else
  checkBuildingDestroyed(buildingID)
 end
end
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack 0.44.12-r2
« Reply #1634 on: February 16, 2019, 02:31:21 pm »

Not seeing an obvious reason, but I'm not familiar with eventful. What does the 10 argument mean?
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.
Pages: 1 ... 107 108 [109] 110 111 ... 243