Bay 12 Games Forum

Please login or register.

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

Author Topic: world-map-to-svg: A DFHack script to generate an interactive SVG worldmap.  (Read 3537 times)

therahedwig

  • Bay Watcher
    • View Profile
    • wolthera.info

This may be a little late with the new release around the corner, but after I finally got the hang of lua and the DFHack structures, I realized I could use it to generate a worldmap SVG.

See, in the past I had used python and the various legends xmls, detailed maps and more to generate this file:

Click me, because this map is quite big

But, it took a long while to load the legends, and not everything can be found in the legends, and the maps need to be manually created with an image manipulation program, and that in turn is not easy to script, and even then I also had to manually add in names and the like. So I was looking for a more scriptable solution.

So I was quite excited to find the map details in the DFHack structures. Of course, these map details are missing the precise region tiles information, because DF just doesn't load those until needed. So I decided to go somewhat the opposite direction and aim to make it very informative.

This ended up with surprisingly cute results:



Now, the above is the SVG linked as an image. For the interactivity, you will need to open the link directly into a tab (for self-generated svg files, just open them in chrome or firefox (haven't tested anything else)). Here you can see that regions light up when hovered (using css), and clicking them zooms in(using svg views).
  • Everything except the site-entity links can be hovered over for more information.
  • Everything that glows on hover can be clicked on to zoom in. Use backspace or remove the last part of the url from '#' onwards to go back to the full map.
  • On the left hand side there's a list of landmasses. These can be clicked to zoom in.
  • On the right hand side there's a list of civilizations and their colors. The colors are the actual colors used by the historical maps for these civs. Clicking their name will zoom in on the area of their claims. The actual claim borders are also drawn into the svg, but hidden by default, as they, while correct, look weird.
  • All the colors, outside of the site-entity links and some stroke colors, are stored as inkscape swatches meaning you can open the map in inkscape and change all the colors of the deserts or something.

If you zoomed in on a section, and you link that in an img tag, you will get the zoomed in section (tested on firefox and chrome only), so this code gives the following img:

Code: [Select]
[img width=400]https://wolthera.info/df-worlds/region1-Slozgo-Sagus-svg-map.svg#221-the-disemboweled-realm-view[/img]


This of course requires that you have a place (server) to upload and access this file directly from.

Here's some other regions I have been testing this script on:

A Pocket island
A Small Island
A Large Region (Warning: 8~mb file)

Limitations and things that could use improving:
  • The entity links need work. I am not sure how I am going to approach this, as it highly depends on when you'd want them to be visible and how much info you can get about them.
  • Right now it's not possible to get biomes within a region, which means that I cannot get the permafrost for the artic ocean figured out. This now needs better tropicality detection
  • I am unsure about detecting a site as a ruin. This seems to be the correct way to handle it.
  • The script is very messy, I apologize.
  • Also only tested on four worlds.

So, without further ado, here's a github gist with the script:

LINK
« Last Edit: January 21, 2020, 01:11:58 pm by therahedwig »
Logged
Stonesense Grim Dark 0.2 Alternate detailed and darker tiles for stonesense. Now with all ores!

ragundo

  • Bay Watcher
    • View Profile

Excellent work.
I'll study it thoroughly.

Probably I'll adapt your code to show the svg in a Qt application.


 local fgcolor = df.global.world.raws.descriptors.colors[entity.resources.unk23[0]]
 local bgcolor = df.global.world.raws.descriptors.colors[entity.resources.unk24[0]]


Did you figure out these unks?. Please make a pull request to dfhack https://github.com/DFHack/df-structures to include them.

I did years ago also an export map plugin that generated the whole map with all the details but required to call DF subroutines instead of using dfhack.


Logged

therahedwig

  • Bay Watcher
    • View Profile
    • wolthera.info

Quote
Did you figure out these unks?. Please make a pull request to dfhack https://github.com/DFHack/df-structures to include them.
Yeah, I wasn't too sure how to go about that (don't use github normally, so everything is weird), but now I realize that of course github had the online editing thingy. Submitted.

Quote
Probably I'll adapt your code to show the svg in a Qt application.
While I don't think I used anything that qtsvg can't handle, I think that if you have this qt application talk directly to dfhack like armokvision or stonesense do, it might be much easier to do that. Do feel free to reuse the colors though; I suspect they're the biggest contribution I made here ;)
Logged
Stonesense Grim Dark 0.2 Alternate detailed and darker tiles for stonesense. Now with all ores!

ragundo

  • Bay Watcher
    • View Profile

I just compiled today develop dfhack and saw that your commit was merged. Congratulations!



Please, keep the good work, we need more people working in dfhack/ df tools. ;)
« Last Edit: January 18, 2020, 01:34:33 pm by ragundo »
Logged

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile

Impressive work! I like how lairs, shrines, burrows and vaults are much more visible here.

Hosting, well. Can save dffd links to download the file then look at it on local computer. But that doesn't get the trick for image sharing with #, which is a nice innovation over other legends exploring utilities.

(Ex, this could potentially be a nice place for fishing-based fort:

)

Regarding detecting permafrost/biomes: If you have active save loaded, you could probably take notes from Biome Manipulator, as that can do that.

PatrikLundell

  • Bay Watcher
    • View Profile

The fresh out of the oven DFHack version should have biome determination somewhere in Maps, so it should be a matter of calling that operation (that's the logic used by Biome Manipulator, which was originally disassembled by ragundo from DF. It's ragundo's code that's provided through the DFHack interface).
Logged

therahedwig

  • Bay Watcher
    • View Profile
    • wolthera.info

Thanks you guys, that was really helpful!

I've updated the script to have a simplified version of the biome detection(the tropicality detection still needs to be done properly), plus deserts now get colored with their sand color, and mountains have the regions that they get a new 'higher' mountain tile for in DF marked. Also adjusted related biome colors to be a little bit closer and more structured.

I also updated to the latest dfhack, and fixed a bunch of stuff with sites, including but not limited to the appropriate civ owner colors(the previous used the builder civ, this now uses the occupier/reclaimer civ). Didn't do anything else with entity-site links yet, so these still look messy.
Logged
Stonesense Grim Dark 0.2 Alternate detailed and darker tiles for stonesense. Now with all ores!

hertggf

  • Bay Watcher
    • View Profile
Re: world-map-to-svg: A DFHack script to generate an interactive SVG worldmap.
« Reply #7 on: February 14, 2020, 12:32:36 pm »

I tried it and got this error:
Code: [Select]
...ers\xxx\Desktop\DF\df_47_02_win/raw/scripts/svg-map.lua:1254: Cannot read field world_site.T_subtype_info.is_tower: not found.
stack traceback:
        [C]: in metamethod '__index'
        ...ers\xxx\Desktop\DF\df_47_02_win/raw/scripts/svg-map.lua:1254: in local 'script_code'
        C:\Users\xxx\Desktop\DF\df_47_02_win\hack\lua\dfhack.lua:680: in function 'dfhack.run_script_with_env'
        (...tail calls...)
this is with 47.02 and dfhack prerelease-200210002

edit: same result with dfhack-0.47.02-alpha0-200213003-Windows-64
« Last Edit: February 14, 2020, 12:42:47 pm by hertggf »
Logged

PatrikLundell

  • Bay Watcher
    • View Profile
Re: world-map-to-svg: A DFHack script to generate an interactive SVG worldmap.
« Reply #8 on: February 14, 2020, 02:40:12 pm »

"is_tower" has been renamed to tower_type since it isn't a boolean, and has been converted to an enum (although the underlying values are still the same, of course). Thus, changing "is_tower" to "tower_type" ought to do it.
Note that the two adjacent field have also been changed into enums, although only the next one had a boolean-sounding name that was changed.
Logged

therahedwig

  • Bay Watcher
    • View Profile
    • wolthera.info
Re: world-map-to-svg: A DFHack script to generate an interactive SVG worldmap.
« Reply #9 on: February 14, 2020, 04:42:47 pm »

I don't build dfhack myself, so that bug will stay there until there's a linux release available, I'm afraid :)
Logged
Stonesense Grim Dark 0.2 Alternate detailed and darker tiles for stonesense. Now with all ores!

PatrikLundell

  • Bay Watcher
    • View Profile
Re: world-map-to-svg: A DFHack script to generate an interactive SVG worldmap.
« Reply #11 on: February 14, 2020, 05:56:37 pm »

And scripts have the advantage of not requiring compilation, which means hertggf can just change the problem line in the local copy to get it to work locally until things stabilize (although there is no guarantee it won't blow up elsewhere).

A disadvantage with scripts provided outside of DFHack is that they're somewhat independent of the version of the code, which means you have to decide whether to keep the script backwards compatible (with variant execution) or always just aim for the latest version, abandoning older versions when incompatible changes affect them.

Another script disadvantage is that there is no automatic mechanic correctness control of the kind compilation provides: they blow up only on execution, and issues may be in branches that are rarely used, and so can go undetected.
Logged

jecowa

  • Bay Watcher
    • View Profile
Re: world-map-to-svg: A DFHack script to generate an interactive SVG worldmap.
« Reply #12 on: February 14, 2020, 11:36:13 pm »

I tried it and got this error:
Code: [Select]
...ers\xxx\Desktop\DF\df_47_02_win/raw/scripts/svg-map.lua:1254: Cannot read field world_site.T_subtype_info.is_tower: not found.
stack traceback:
        [C]: in metamethod '__index'
        ...ers\xxx\Desktop\DF\df_47_02_win/raw/scripts/svg-map.lua:1254: in local 'script_code'
        C:\Users\xxx\Desktop\DF\df_47_02_win\hack\lua\dfhack.lua:680: in function 'dfhack.run_script_with_env'
        (...tail calls...)
this is with 47.02 and dfhack prerelease-200210002

edit: same result with dfhack-0.47.02-alpha0-200213003-Windows-64

I got a similar error on the DFHack 0.47.02 alpha for Mac:

Code: [Select]
....02-L1/df_osx v0.47.02/hack/scripts/world-map-to-svg.lua:379: attempt to index a nil value (local 'log')
stack traceback:
....02-L1/df_osx v0.47.02/hack/scripts/world-map-to-svg.lua:379: in local 'script_code'
./hack/lua/dfhack.lua:680: in function 'dfhack.run_script_with_env'
(...tail calls...)

It looks like it might be working in the 44.12-r3 version, though. Dwarf Fortress has been maxing out a core for the past 40 minutes, at least. It's a "smaller" world with "very short" history. Is it normal to take this long, or did something probably go wrong?

Edit: It's still going after 2 4 6 hours.
« Last Edit: February 15, 2020, 05:07:23 am by jecowa »
Logged

PatrikLundell

  • Bay Watcher
    • View Profile
Re: world-map-to-svg: A DFHack script to generate an interactive SVG worldmap.
« Reply #13 on: February 15, 2020, 03:57:18 am »

Got the generation to work by replacing lines 1253-1283 with (note that "is_tower" was replaced by "fortress_type", not "tower_type", and "monument_type" replaces the old boolean sounding field name):
Spoiler (click to show/hide)

However, trying to display the result gave the following error message:
Code: [Select]
XML Parsing Error: not well-formed
Location: file:///C:/Games/Dwarf%20Fortress/DF_47_02/region1_cascade-Elathu-Ew%C5%A0-svg-map.svg
Line Number 5, Column 98:
<text id="world-name" x="2056.0" y="4176" style="font-size:50pt; text-anchor:middle;" > Elathu Ew￿>
-------------------------------------------------------------------------------------------------^
Removing the character the browser can't interpret allowed it to be displayed.
Logged

jecowa

  • Bay Watcher
    • View Profile
Re: world-map-to-svg: A DFHack script to generate an interactive SVG worldmap.
« Reply #14 on: February 15, 2020, 04:26:06 am »

How long did generation take for you?
Logged
Pages: [1] 2