Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: DFHack LUA Script, Rainfall Flooding  (Read 916 times)

Gashcozokon

  • Bay Watcher
    • View Profile
DFHack LUA Script, Rainfall Flooding
« on: March 16, 2018, 03:47:56 am »

For I don't even remember how many years I have wanted to try this, and I finally got around to doing some SCIENCE the result is in the spoiler tag below.

First Version
Spoiler: Lua Script (click to show/hide)

Second Version
Spoiler: Lua Script (click to show/hide)

This script converts roughly 20% of Grass tiles, and 40% of Ramp tiles into POND tiles. The result is a bit ugly, but what it allows is for actual water to spawn and move around during Rain. In a real hurry you can end up with a real mess, and depending on your landscape and Fort design, may have potential for some serious flooding FUN!

So, I am posting this to share, I'd love to hear feedback. Also I suspect some of you may be more familiar with DF Lua than I, and could probably clean the function up a bit. Making alterations based on Material Type instead of straight Tile value and what not.

Enjoy, I look forward hearing about it.

-=-=--=-=-=
Edit: Updated script in the Spoiler Tag.

@lethosor Thank you for your notes, I made modifications based on them. For the moment, I did keep the magic numbers, but thanks to your advice I know what they are so that helps alot. I chose to keep the values over text for length of the file, but I guess for clarity and modability of anyone wishing to tinker with it, text would be better.
I'll add the values below. I have the full list of ~700 Tile names, I thought I might post some place if it isn't available already, I haven't checked yet.

One thing, I did try to implement your suggested shorthand
Code: [Select]
if( tiles[current]==true and math.random(100) < 20) theninstead of the has_value( ) it didn't quite give the results I expected, and some reading suggested that the table declaration would need to change from {257,332,etc...} to {[257]=true,[332]=true,[etc]=true...}
and again, with using names over numbers, I can do it, but is it worth it?
Finished, fixed this and got named indexes working.

-=-=--=-=-=
Edit: Added second Spoiler Tag.
 Modified concept, rather than changing ground tiles to be scattered with pond tiles, using a qurik of SemiMoltenRock was able to scatter pond tiles across the top of the skybox, so now the surface is misty with buckets of rain!
« Last Edit: July 06, 2018, 01:41:03 am by Gashcozokon »
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #1 on: May 16, 2018, 05:53:30 pm »

I'm very late, apparently, but this sounds fun. Mind if it ends up in DFHack?

Some things I would probably change:
- Use names instead of magic numbers in the tiles/ramps table. For example, change 231 to df.tiletype.GrassLightRamp. You can get that with "~df.tiletype[231]" in the Lua interpreter (while df.tiletype.GrassLightRamp evaluates to 231).
- You could build a map like this:
Code: [Select]
local tiles = {
  [df.tiletype.GrassLightRamp] = true,
  ...
}
and then use "tiles[val]" instead of "has_value(tiles, val)", which is (maybe) faster and easier to use.
If you want to be fancy and cut down on repetitiveness more, you could do something like:
Code: [Select]
local tiles = {
  GrassLightRamp = true,
 ...
}
if tiles[df.tiletype[block.tiletype[x][y]]] then
  -- do something with grass tile
end
this works because block.tiletype[x][y] gives a number, and df.tiletype[number] gives a name, which can then be used to index tiles. Probably not necessary for your case, but that sort of thing can be useful when dealing with large numbers of members of the same enum.

- Minor thing: you're calling enableFlooding(...), and ... expands to all arguments given to the current script. However, enableFlooding() takes no arguments, so that's pointless.

- Your script has everything in functions, which is nice - I'm not sure where you got that from, since most one-off DFHack scripts don't do that. It's easy to add a couple lines to make it possible for other scripts to call this one using reqscript() - see https://github.com/DFHack/scripts/blob/2840996c5f39917713cfb0e7ce1c6dca779177e1/fix/retrieve-units.lua for an example (search for "module" - you'll need both of the things that come up).

- The surface detection could change - I don't think 159 is always an appropriate lower bound. Not sure what to suggest, though.
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.

Gashcozokon

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #2 on: June 27, 2018, 03:23:15 pm »

I'm very late, apparently, but this sounds fun. Mind if it ends up in DFHack?
I would LOVE for this to end up in DFHack.

Yes I agree that the hard coded numbers is a poor choice. This was mostly quick and dirty code to get it working for my embark, something better like testing that the tile isn't a shrub/plant/tree would probably be enough. I could argue for and against rocky or stone tiles being changed. But considering the wide variety of terrain types I guess really just testing [CanSeeSky] or whatever the Outside boolean is, should be enough.

The oddness of the function calling is because this is recycled code, I have a lot of programming but not a lot of LUA, (or free time). So it could all be better.

The surface detection, really doesn't even have to be there. It doesn't take all that long, and this is the kind of script that you'll only run once. We 'might' be able to work out an undo function, but that would be kludgy at best, guessing if it was a real pond or just a rouge tile.

I am super glad to hear you enjoy though, I hope you have fun with it, I certainly enjoy the extra Fun.
Logged

Gashcozokon

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #3 on: July 02, 2018, 06:31:51 pm »

Two spoiler tags here with the list of names for the indexes in the script (first post spoiler)
Spoiler: Ramp Tile Names (click to show/hide)
Spoiler: Floor Tile Names (click to show/hide)

I will post a link to the full list here once I find it or make it.
Logged

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #4 on: July 02, 2018, 07:00:36 pm »

[twbt](https://github.com/mifki/df-twbt] github page has a list of names which might be useful for cross-checking at least (in fact, looks like you took it from there).

Whether you want things like feature, ice or constructed ramps to become murky pool slopes, though...Well that's a different question, no?

Regarding surface detection, one idea is that you could descend down from sky, and once you find no outside tiles in a whole z-layer cancel further checking. Though that isn't needed for something you only run once. (You may want to use something like dfhack.gui.showAnnouncement to post a message it'll take a bit of time, so players don't think df has hanged 'n' time to kill the process).

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #5 on: July 02, 2018, 08:21:13 pm »

The list is also in DFHack, like I mentioned. You can use @df.tiletype in the Lua interpreter to list every value.
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.

Gashcozokon

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #6 on: July 02, 2018, 11:02:54 pm »

[twbt](https://github.com/mifki/df-twbt] github page has a list of names which might be useful for cross-checking at least (in fact, looks like you took it from there).
Naw, I just took the info from lethosor in the post above, and simply made a loop:
Code: [Select]
    for num=0, 1000 do
        print (num, df.tiletype[num])
    end
then I just picked out the ones I wanted.

Whether you want things like feature, ice or constructed ramps to become murky pool slopes, though...Well that's a different question, no?
I am going to change it to text labels so anyone can pick easier what they want, but Ice, would just instantly freeze and constructions seem silly to me, but again preference. Feature I took to mean HSF, is that available on the surface? I need to play the new version more.

(You may want to use something like dfhack.gui.showAnnouncement to post a message it'll take a bit of time, so players don't think df has hanged 'n' time to kill the process).
This sounds like a brilliant Idea, could you show me the syntax of a working example? When I test it give me an error saying a number is expected when the dfhack manual says ( Text, color [, bright] )
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #7 on: July 02, 2018, 11:16:05 pm »

That syntax is working for me:
Code: [Select]
[lua]# dfhack.gui.showAnnouncement('text', 4)
[lua]# dfhack.gui.showAnnouncement('text', 4, true)
(I didn't test with a world loaded, but the lack of error messages means the arguments are at least the right types.)

Note that instead of 4, you should use COLOR_RED, for example. The color constants are listed on http://dfhack.readthedocs.io/en/stable/docs/Lua%20API.html too (specifically http://dfhack.readthedocs.io/en/stable/docs/Lua%20API.html#global-environment).
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.

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #8 on: July 03, 2018, 10:10:48 am »

Text labels is a great idea for future proofing. Feature is available on the surface if you channel a really deep pit. Though the only feature ramps would be at the very bottom.

Gashcozokon

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #9 on: July 03, 2018, 12:36:12 pm »

I just had an idea I can't wait to test after work.

One of my failed attempts was returning true all the time.  And replaced a good portion of the air with pond tiles.
I do not recall if I unpaused....  but if they magically float then we can just set the top z or few of the sky box to have random pond tiles and never mind the terrain type at all!!
 
Logged

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #10 on: July 03, 2018, 02:58:22 pm »

Ha ha, that's some idea you have there.

I imagine they'd ordinarily cave in if you dug a tile exactly under them. However, I recall itg revealed that SMR provides support back during 34.11, so adding 1 tile of smr might provide you with such floating pond ramps.

Though if you're doing that, could also just spawn 1/7 water randomly at top of map when it is raining - gui/liquids could provide a code sample for placing water in desired area. (Though this would stop working once dfhack is removed, i.e. when upgrading to bleeding-edge vanilla without it.)
« Last Edit: July 03, 2018, 03:00:10 pm by Fleeting Frames »
Logged

Gashcozokon

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #11 on: July 03, 2018, 03:16:00 pm »

Ha ha, that's some idea you have there.

I imagine they'd ordinarily cave in if you dug a tile exactly under them. However, I recall itg revealed that SMR provides support back during 34.11
Both great ideas.  Not just to test not being paused but for cave ins during excavation.  I will also consider SMR walls under the pond floors.  Of course while doing this in the sky keeps the ground looking cleaner and eases testing for valid placement. Whole buckets of water are going to be falling from space.  Is yet to see if the mist offsets the head trauma ^__^
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #12 on: July 03, 2018, 03:21:55 pm »

Note that upper sky blocks aren't always allocated, even if they appear on the map. I think gui/liquids and a few other tools have ways to allocate them on demand, though.
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.

Gashcozokon

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #13 on: July 05, 2018, 01:10:14 am »

Ha it works. Thank you Fleeting Frames for the SMR idea, that is just what was needed. In a minute or so of raining so far I have not seen any injury but I have seen waterfall thoughts.
I am not writing off the concern yet about getting hurt, but so far so good. I am going to put the alternate version in a spoiler in the first post.
Logged

Gashcozokon

  • Bay Watcher
    • View Profile
Re: DFHack LUA Script, Rainfall Flooding
« Reply #14 on: July 10, 2018, 09:25:50 pm »

So floating SMR tiles work pretty good. But there are occasional collapse warnings.
Need more testing to find out if the cause is just physics noticing the floating rocks. Or if placement might help prevent it. I'm going to export before and after images and see if I notice a pattern in which tiles cave in.
Logged