Bay 12 Games Forum

Please login or register.

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

Author Topic: DFHack script: showbiomes  (Read 25393 times)

PatrikLundell

  • Bay Watcher
    • View Profile
DFHack script: showbiomes
« on: September 29, 2016, 04:38:41 am »

Current version: 2.6 2017-02-16.

I've made a DFHack script that displays which biomes are present on a Fortress embark, allows presentation of biome parameters (rainfall, salinity, drainage, savagery, temperature, volcanism, and evilness), as well as detecting biome shears ("foreign" biomes up in the air).

The script is available at this link:https://www.dropbox.com/s/5nxesztkpnb6f5w/showbiomes.lua?dl=0
It can also be found here: https://github.com/PatrikLundell/scripts/tree/own_scripts

Function: When invoked, the script overlays the DF window with one of its own (as gui/gm-editor does). This window then presents the selected information in the form of a colored ASCII character for each tile of the embark.
- The help/info page displays the biome types detected on the embark, as well as any biomes that might be present in the air in unidentified biome shears.
- The starting view is the biomes at the (varying) ground level with a character representing the biome and the color presenting savagery/evilness.
- The other views present the selected data element on the current Z level (starting in the air at the top Z level), with keys to step up and down along the Z levels to display the presented parameter in each tile. Note that in a normal embark the air is uniform, but of the biome belonging to the world map tile to the NW, not the embark's own world map tile. If shears are present they occupy a  complete 16*16 tile block where the parameters match those of a different biome than the air background biome (usually: an unexaminable shear can be of the same biome).
- There are two kinds of possible biome shears in the air: ones that can be examined, and ones that can not. The examinable ones are displayed as air biomes on the help/info page (if not present on the ground as well) and their parameters are displayed when viewing that Z level. Unexaminable shears are displayed with red '@' characters, and they will have a biome from either the list of identified biomes or the potential biome list. Note that the biome of each 16*16 tile block unexaminable shear is unrelated to the biome of neighboring blocks.
At the ground level and downwards the parameters are uniform within each biome.
- The first thing to do when using the script the first time is to bring up the help/info screen for more info.

Note that this page is update to match the current state of the script without providing edit indications as is customary, as I think that serves the purpose of this first post better.
Feedback is welcome, and I'll try to correct bugs reported.

Nerd section (background info/assumptions the script is based on):
- The base air biome is the biome of the world map tile to the NW of the embark, rather than the embark itself. This is likely to be a DF bug.
- Biome shears are "random" in that embarking in the same location from copies of the same world generation save can produce a different set of shears.
- Tropicality determination, i.e. whether a biome is tropical or temperate, is a complicated matter, as it's not a property inherent in the biome itself. Instead, all biomes with a tropical and a temperate version become tropical if their biome reference world map tile is at 200 or above when value has been scaled to a north pole only world of maximum size, or has a temperature of 85 or above if the world has no poles.
  - A biome may be tropical or temperate if the adjusted tile location is 171-199 for a world with poles or the temperature is 75-84 for one without, but only for the two largest world sizes (which explains why tropical dry broadleaf can only exist in such worlds). The exceptions is lakes, which are always tropical in the "maybe" zone regardless of world size.
  - For biomes in the "maybe" zone the determination is made based on a relation between rainfall and "latitude" within the region. This relation varies per biome type.
- The term "biome reference world map tile" used above has three different versions:
   1. The standard one is the one for surface biomes. It uses a series of offsets that first references an embark level tile that then references a world map tile. The result is either the embark's world map tile or one of its 8 neighbors.
   2. Surface rivers/brooks and ponds are not real biomes, and the tropicality determination for these uses the world map tile of the embark.
   3. Air biomes use the world map tile that has the biome as its own biome.
- Each biome can be present on the embark as both its temperate and its tropical incarnation at the same time if the conditions are suitable.

Change log:
2.6
- (Hopefully) Fixed an issue when an embark bordering the edge of the region references a biome in the next world map tile.
- Added X/Y coordinates of the DF cursor to the help/info page.

2.5
- Fixed bug when embarking in the easternmost column of world map tiles.
 
2.4
- Split panning between the help screen and the others, so they pan independently.
- Changed starting Z level to the top level - levels_above_surface to be closer to the ground level.
- Restructured the display logic to improve speed.

2.3
- Panning now affects the help screen below the key bindings, at the cost of a significant lag when the help is displayed.

2.2
- Reverted back the cursor background, should you manage to move the cursor while showbiomes is active.

2.1
- Added DF cursor indicator showing where the DF cursor is on all pages, ignoring the Z dimension.
- Fixed bug causing biomes present on the surface to sometimes be shown as air only when an air biome and a surface biome of the same type exist, but they actually are different biome entities (with slightly different parameters).
« Last Edit: January 12, 2020, 05:39:16 am by PatrikLundell »
Logged

Meph

  • Bay Watcher
    • View Profile
    • worldbicyclist
Re: DFHack script: showbiomes
« Reply #1 on: September 29, 2016, 05:23:57 am »

Sounds nice, but your link is broken. ;) It links to the modding forum itself.
Logged
::: ☼Meph Tileset☼☼Map Tileset☼- 32x graphic sets with TWBT :::
::: ☼MASTERWORK DF☼ - A comprehensive mod pack now on Patreon - 250.000+ downloads and counting :::
::: WorldBicyclist.com - Follow my bike tours around the world - 148 countries visited :::

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: DFHack script: showbiomes
« Reply #2 on: September 29, 2016, 05:25:15 am »

Posting from the initial thread (didn't see edit at the time of the reply :P)

Well, that's interesting news on vegetation.

Between 100 and 299, altitude, to my knowledge, affects the following:
- Aquifer rate (higher = less)
- temperature (higher = less). This can result in no-pole worlds having temperate biomes where there should be tropical ones.
- relative altitude affects embark fatness.
- {unconfirmed} Perhaps what shears are available.
- river and lake formation. Tend to form in valleys. Needs at least 104 altitude to form. Needs around 122 for regular patterns to take root, Once past 104, the likelihood that an adjacent ocean combines with a lake and erases that biome for ocean increases (depending on how likely the lake is) up until around 150, after which it goes back down.
- [tentative] Whether civilization can plop down on it in world gen (at least humans seems to prefer 200z areas over 299 ones)
- Often but not always, embark-level geography in the whole world.

Now, testing and bugs....On that same 5-biome 2x2 with river embark:

Shearing:

An interesting case, found report of terrifying temperate grassland (104z, upper left corner):



Now, admittedly, the world does have some - lots of region tiles away to the south, so tropical ones.

So, I check with probe - yes it is terrifying, and then farm plot.


what

It's a swamp. Can grow sliver barbs and papyrus on same plot.

Needless to say, that isn't grassland. Need to say, this world shouldn't have any terrifying tropical swamps at all.
(Neither should it have temperate broadleafs, yet brown recluse spider silk litters on ground on embark.)

Also, there are more obvious bugs.

Spoiler: image: like this (click to show/hide)

Yes, there is a tropical swamp cutting off (or at least something untamed) on the lower right like that, though what threw me is how it's only 3z above embark surface (on 103z), while one needs go 1 more deeper to see river (biome).

However, the joyous tropical dry broadleaf marked on the upper left corner...isn't. It's joyous alright, but a farm plot reports Sun Berries and Teff (Tropical Grassland exclusive) rather than Sun Berries and Tomato (dry broadleaf exclusive).

Similarly, the untamed rocky desert to the south is also teff-capable, so untamed tropical grassland. Also continues z-level above.

And to the right, the untamed swamp reported...



Okay, I definitely didn't expect this.



Yes, it got "tropical swamp" correct. Probe and showbiomes match.

Yet, I can grow both sliver barbs AND sun berries on that same farm plot.

Looking again, the same thing occurs with that not-badlands mentioned before, and that "terrifying grassland" mentioned at start - I just didn't notice at the time.

In fact, I can grow sun berries even on farm plot built on terrifying grassland.

Well. Okay.

One more crash bug, embarking on top of temperate grassland 2w1h (while, curiously, 2x2 covering that area+more works):
Spoiler (click to show/hide)

Onto latitude.

One thing that may throw off is that the index of it counts from 1. I.e. on 17S with 4-, the tropical latitudes are 1., 2., 3., 4. DF internal for site region locations start from 0, and I think I've read that lua uses nonstandard indexing as well.

Tentatively looking at few simple worldgen embarks from the south side as well, I see some temperates labeled as tropical as well as...


Okay, yeah, that should be calm temperate shrubland, not impossibly savage tropical ocean.

However, I further suspect that it pulls from current region tile for all biomes, on account of it reporting same grassland biomes as tropical OR temperate, depending on which region tile you embark in - I've yet to see it note the distinction between tropical and temperate of same biome.

EDIT: on further checking, it seems it reports all colors as 1 higher, i.e. 3 evilness is reported as light cyan not white.
« Last Edit: September 29, 2016, 05:36:54 am by Fleeting Frames »
Logged

PatrikLundell

  • Bay Watcher
    • View Profile
Re: DFHack script: showbiomes
« Reply #3 on: September 29, 2016, 06:49:33 am »

It's rather common to be able to grow both sliver barbs and sun berries in the same plot for me, but I never manage to get hold of sliver barb seeds (and sun berries only occasionally). I usually embark in good areas, but my understanding is that cultivation is not dependent on the good/evil rating.

Crash: OK, it seems the Probe logic isn't completely safe either. If I could get the world and embark location I should be able to print what it's trying to read and see if Probe goes belly up on it as well. If nothing else I can catch that case and output a default "failure to read data" character.

Embarks & bugs: I think I need the worldgen parameters and the embark locations to reproduce issues and examine them.

The ocean: A surface embark of Ocean is obviously off if it covers the whole embark. However, it isn't impossibly savage, as the grey color indicates a Calm embark (low savagery, average evilness). I think you're looking at the value range color codes ;)

Color coding: You're right there's a bug there. However, it's in the legend rather than the code itself. I've missed the 0-9 range there so all values have been shifted up one step. Thus the top should be > 109.

Edit:
And yes, embark tile values start at 0 (tested by embarking in the top corner), not one, so I'm reverting back to correcting the index.

Edit 2: And it didn't work on worlds with no poles. Should be fixed now (version 1.1), same link.
« Last Edit: September 29, 2016, 07:24:16 am by PatrikLundell »
Logged

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: DFHack script: showbiomes
« Reply #4 on: September 29, 2016, 10:39:12 am »

Crash: I tested it on that same 2x2 + river world on some random tropical grasslands and swamps (4th one from the left 4-5 tiles just above the river, for instance), but first checking that you can get 2wx1h to work?

Even trying that on simple worldgens, it crashes. And, come to think of it, I recall that showshears.lua previously reported 2x1 as 3 columns and 6 rows, not the opposite, so there might be dimensions getting flipped there.

..Trying 3wx2h, covering bit of swamp + half tropical, half temperate grassland in the 5b+r area. Crash. 2x3, then? Crash too. 3x3 over that same area both covered? No problem, buddy.

Actually, screw that all that typing, picture is like, three sentences:

Params!

You're right on the ocean ^^;;

Mind, those were all on the vegetation-present version :v

Am-getting the 1.1 now..

EDIT: Eh. Might still pull from current region tile? Half tropical and half temperate grassland still gets reported as either all tropical or all temperate depending on which latitude you embark in.
« Last Edit: September 29, 2016, 10:48:10 am by Fleeting Frames »
Logged

PatrikLundell

  • Bay Watcher
    • View Profile
Re: DFHack script: showbiomes
« Reply #5 on: September 29, 2016, 11:29:35 am »

Dimension flipping should be easy enough to check out. Will be next to be tested.

I've just had a case of a 166 elevation embark that is reported as a swamp (which matches the parameters), but a farm plot on it does not allow anything to grow, and DFs view shows it as a mountain. Since there are no trees nor shub growing on it, I think it is a low elevation mountain, and that the wiki page info is off.
I'm considering trying to use the 10 enumeration biome type to constrain what the biome determination outcomes can be.

Since I have no better info than the latitude table for temperate/tropical determination in worlds with poles, the tool won't show both variants of the same type in the embark. Better info on what determines the type is needed, but lacking...

Edit: Yes, indices flipped. Should now be updated to 1.2. Fixed a bug related to clipping and LUA not truncating integers on division, as well as added coordinates (single coordinate for each 16*16 area) for shears printed to the DFHack window.

Edit 2: If I recall correctly, I've got an older version of your world, so the embarks don't match. With my memory being what it is, I don't recall where you posted the latest parameters. Could you please update me?
« Last Edit: September 29, 2016, 12:00:15 pm by PatrikLundell »
Logged

ragundo

  • Bay Watcher
    • View Profile
Re: DFHack script: showbiomes
« Reply #6 on: September 29, 2016, 01:00:59 pm »

Hello
https://github.com/ragundo/exportmaps/blob/master/cpp/df_utils/biome_type.cpp#L105
That's a reverse engineering conversion to C++ of the DF routine that determines the biome type of a given world coordinate (x,y)

Hope that it helps to you
Greetings

Logged

PatrikLundell

  • Bay Watcher
    • View Profile
Re: DFHack script: showbiomes
« Reply #7 on: September 29, 2016, 02:07:42 pm »

Thanks ragundo, that certainly looks like the info we've been looking for.

I've just skimmed it, but there are two question (at least for now):
- Is the get_biome_savanna function correct? Most functions check against "< 66" whereas that one checks against "<= 6".
- I'm not familiar with __OFSUB__. A quick search on the net provides two pieces of assembly code I may be able to decipher with some effort, but is there a simple explanation?
Logged

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: DFHack script: showbiomes
« Reply #8 on: September 29, 2016, 02:28:29 pm »

Mountain(biome) can occur at any elevation :v I've had mountains occur at sea level. Still, a bug.

The latitude does determine the biome, but only the biome of 9th square by width and height. As you well know, how biomes spread out from there is random. *goes looking for demonstration image with 1-tile lake* *can't find it, despite having seen it several times before*

From what you say, it seems it currently indeed looks at embark latitude, not biome latitutude, which yeah explains it not detecting the differences.

As for parameters, I just posted the ones for 2x2+river above. I have two others I've used, though...adding in a bit:

Spoiler: The embarks (click to show/hide)

@ragundo: Not sure. How do you find out from it the facts that, say, on large world tropical grasslands occur on indices 0-76 while tropical savannas occur on 0-74?

...or, actually, simpler, since it has only 7 matches: The differences between the latitude of occurring of freshwater and saltwater swamps (0-80 vs 0-82) and marshes (0-78 vs 0-79).
« Last Edit: September 29, 2016, 02:39:21 pm by Fleeting Frames »
Logged

PatrikLundell

  • Bay Watcher
    • View Profile
Re: DFHack script: showbiomes
« Reply #9 on: September 29, 2016, 03:30:14 pm »

ragundo's code has a much lower mountain level than the wiki, but it's still well above sea level, which is odd given your experience.

The formulae include temperature as a factor, which explains why tropical and temperate can exists side by side. The code isn't for embark level determination but pre embark (i.e. world tile), though, so I'm replacing the temperature gotten from there with the one displayed for the individual tile within the embark.
Anyway, I'm in the process of coding it, so eventually (probably sometimes tomorrow) we'll see what happens.
Logged

Meph

  • Bay Watcher
    • View Profile
    • worldbicyclist
Re: DFHack script: showbiomes
« Reply #10 on: September 29, 2016, 04:17:28 pm »

Just wanted to say that this is a cool project :) Nice to see some new scripts popping up, hope you will manage to fix/improve the issues.
Logged
::: ☼Meph Tileset☼☼Map Tileset☼- 32x graphic sets with TWBT :::
::: ☼MASTERWORK DF☼ - A comprehensive mod pack now on Patreon - 250.000+ downloads and counting :::
::: WorldBicyclist.com - Follow my bike tours around the world - 148 countries visited :::

PatrikLundell

  • Bay Watcher
    • View Profile
Re: DFHack script: showbiomes
« Reply #11 on: September 29, 2016, 04:54:06 pm »

Thanks Meph.
If ragundo's code works as it ought to it should at least be a fair bit better.
Logged

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: DFHack script: showbiomes
« Reply #12 on: September 29, 2016, 05:17:01 pm »

About the mountain bit, I bet that only counts for the 9th/9th tile of said region tile, and the height of it. You know as well as me as when you paint mountains and oceans adjacent, you'll not get a third biome in-between, which is what I referred to ^^;;

PatrikLundell

  • Bay Watcher
    • View Profile
Re: DFHack script: showbiomes
« Reply #13 on: September 30, 2016, 09:17:04 am »

Well, we'll see. ragundo's code is for the world map tile level, so I've adapted that (with an implementation of a description I've found of the __OFSUB__ macro). Given that ragundo's code is for the world map tile level it doesn't in itself account for anything else. I've translated the code (handling the fact that LUA actually treats boolean as a type, rather than just integers), with all of its weird things (using the longitude in the tropical/temperate determination logic, for instance).
A cursory check indicates things work, but I wouldn't exactly be surprised if there are bugs in there. Since the biome determination results in the DF standard enumeration, I've done away with the non standard variants and adjusted letter assignments accordingly.
Now it's time to go back to looking at embark oddities to see if they remain with a replaced logic.

Thus, I've updated the script to 1.3.

Another conclusion drawn from this exercise is that the biome description might not be wrong as such, but rather aimed at the world generation level rather than interpretation of a generated world.

Edit:
OK, even when you pointed out the parameters were posted above I had to look 5 times to find them tucked onto the end of the image...
 Unfortunately, I don't get the same world as you do (similar, but not the same), so I guess it's the OS dependent rejection seed scrambling that's at work here. I'll look at the corresponding locations regardless, but it makes information transfer trickier.

Edit: BAH. Uninitialized variables are a bad thing... 1.4

Edit2: No ragundo's code doesn't agree with DF, it seems. I embarked in the "maybe" (43) zone, and the embark had 3 temperate regions (mountain, ocean, and grasslands). The script claims the two variable ones are tropical.
I think I need a crash course in how to identify biomes using farm plot growable plants. Anyway, the grassland supports kaniwa and blueberries, which are temperate but not tropical, according to the wiki. I'd guess the convoluted "maybe zone" determination logic is to blame, somehow. Tracing that is probably going to be messy...

Edit 3: OK, I've traced the issue down to the pieces of code I fail to decipher, which look like this on original:
    int v9 = __OFSUB__(v6,200);
    int v8 = (v6-200) < 0;
    if (!(v8 ^ v9))
        is_tropical_area_by_latitude = true;

My attempt to translate that resulted in a value of "true" even when I screwed up and interchanged the x and y parameters of the embark (as well as other things that caused the value to fail to be doubled), so row 9 of a 129 world still was considered tropical. My incorrect translation is:
 
 function macro_sets (x)
    if x < 0 then
     return 1
    else
     return 0
   end
  end
 
  --============================================================
 
  function ofsub (x, y)
    local y2 = y
   local sx = macro_sets (x)
   return (bit.band (bit.bxor (sx, macro_sets (y2)), (bit.bxor (sx, macro_sets (x - y2)))))
  end
:
:
   local v9 = ofsub (v6, 200)
   local v8
   if v6 - 200 < 0 then
     v8 = 1
   else
     v8 = 0
       end

   if bit.bnot (bit.bxor (v8, v9)) ~= 0 then
     is_tropical_area_by_latitude = true
   end

And I still have no idea what that code tries to do...
« Last Edit: September 30, 2016, 12:52:04 pm by PatrikLundell »
Logged

ragundo

  • Bay Watcher
    • View Profile
Re: DFHack script: showbiomes
« Reply #14 on: September 30, 2016, 12:59:06 pm »

Hi
I've generated dozens of world biome maps using that code and all matched exactly with the ones exported using DF in Legends mode.

Th OFSUB is one artifact that IDA Dissassembler generates. I was lazy and just used that ugly code  :-[.

As I said, that routine is for world coordinates. If you want to go more "high resolution",  down to the embark level, you need to take in count that a world coordinate has a 16x16 embark sub-coordinates.
In embark coordinates you need to do additional processing in order to generate the correct biome.

Check this https://github.com/ragundo/exportmaps/blob/master/cpp/consumers/DF/biome_consumer.cpp#L103
for details about how to process it using a world coordinate and the world region details vector.

Luckily for you, in embark mode, world::world_data::world_region_details vector is already filled  with the data for the embark area, so you don't need to generate it "on demand", that is the purpose of the unmerged exportmaps plugin.

Also, be warned that if you move your cursor in the embark screen, the content of the world_region_details vector changes also!!

Greetings
« Last Edit: September 30, 2016, 01:04:21 pm by ragundo »
Logged
Pages: [1] 2 3 ... 8