Author Topic: Getting world data  (Read 227 times)

Offline Ioncloud

  • Zombie Food
  • *
  • Posts: 6
    • View Profile
Getting world data
« on: July 18, 2017, 02:48:19 AM »

This post will sorta explain wtf: http://smf.cataclysmdda.com/index.php?topic=14581.0

Given that, I'm working on getting world data out of DDA and things were going along rather well when I came to this: http://imgur.com/a/jX33V

I started scratching my head, I was certain my chunk loading was centered around the player... that cant be world 0,0... Then I realized this was SCREEN space coordinates I was getting and not world space *derp*

I'm still working my way around the DDA code trying to puzzle it all out, but given the following function what would be the best way to query the data out of the world? (or can I o.0):
Code: [Select]
    Map* getTilesBetween(IVector2 from, IVector2 to) {
        Map* map = (Map*)::CoTaskMemAlloc(sizeof(Map));
        IVector2 bottomLeft;
        IVector2 topRight;
        if (from.x < to.x && from.y < to.y) {
            bottomLeft = from;
            topRight = to;
        }
        else {
            bottomLeft = to;
            topRight = from;
        }
        int width = topRight.x - bottomLeft.x + 1;
        int height = topRight.y - bottomLeft.y + 1;
        map->width = width;
        map->height = height;
        map->tiles = (Tile*)::CoTaskMemAlloc(sizeof(Tile) * width * height);

        int i = 0;
        for (int dx = bottomLeft.x; dx <= topRight.x; dx++) {
            for (int dy = bottomLeft.y; dy <= topRight.y; dy++) {
                const tripoint p = tripoint(dx, dy, 0);
                ter_str_id ter = g->m.ter(p)->id;
                map->tiles[i].ter = (char*)::CoTaskMemAlloc(ter.str().length() + 1);
                strcpy(map->tiles[i].ter, ter.c_str());
                furn_str_id furn = g->m.furn(p)->id;
                map->tiles[i].furn = (char*)::CoTaskMemAlloc(furn.str().length() + 1);
                strcpy(map->tiles[i].furn, furn.c_str());
                map->tiles[i].loc.x = p.x;
                map->tiles[i].loc.y = p.z;
                map->tiles[i].loc.z = p.y; // swap z and y for unity coordinate system
                i++;
            }
        }

        return map;
    }

Offline Coolthulhu

  • Contributor
  • Survivor
  • ****
  • Posts: 3729
    • View Profile
Re: Getting world data
« Reply #1 on: July 18, 2017, 05:52:55 AM »
What is the question exactly?

What do you want to get and what you're getting instead of that?

Offline Ioncloud

  • Zombie Food
  • *
  • Posts: 6
    • View Profile
Re: Getting world data
« Reply #2 on: July 18, 2017, 01:46:06 PM »
what I'm trying to get is the world between 2 points
What I'm getting is only the screen tiles

When the game loads I get the player's position g->u.pos then load the chunk the player is in as well as x chunks away from the player (configurable, in the screen shot I had it set to 50x50 tiles per chunk with 7x7 chunks loading)
Chunks are configured to pull data from DDA using the method I pasted.
the screenshot I attached illustrates the issue. There's no way that the player is at/near world 0,0 but the bottom left of that picture is, in fact, 0,0 per the data from DDA

What it seems that I'm getting is the players place on the console screen, and the tiles I'm getting are the tiles at the console screen position 0,0 - 131,131

The idea is that I don't need to pass a "look" command back to DDA to pan around, I should be able to get all the tiles the player can see and simply switch to free cam in unity to allow for smooth panning. I also need to be able to load tiles outside of the visible range in the background so there's no extra lag when a player crosses a chunk boundary.

Offline Coolthulhu

  • Contributor
  • Survivor
  • ****
  • Posts: 3729
    • View Profile
Re: Getting world data
« Reply #3 on: July 18, 2017, 03:29:06 PM »
The code you listed should be getting tiles in local map coords, not screen coords, meaning look command should not be affecting anything.

You can't easily get tiles outside 0-131 range because they may not exist.
You can use mapbuffer::lookup_submap to try to get a submap, but expect nulls for submaps that aren't loaded into memory yet. Also expect submaps in memory that aren't part of the currently loaded map to be unassigned without warning.
Submaps that aren't generated will always be nulls here. You could try triggering a load with map::loadn, but it may have side effects.
A side-effect-free (as long as you run it serially, not in parallel with the game) way would be to create a map object and map::load a submap with it, but expect that to be relatively slow.

Offline Ioncloud

  • Zombie Food
  • *
  • Posts: 6
    • View Profile
Re: Getting world data
« Reply #4 on: July 18, 2017, 05:16:36 PM »
Hmm, I see... Is there a way to translate local to world point? I'd imagine submap = chunk in my case so a world point would be something like submap.x*131+local.x, submap.y*131+local.y?

I had started looking at submap, I'll have to poke it a bit more and see what falls out.

Offline Coolthulhu

  • Contributor
  • Survivor
  • ****
  • Posts: 3729
    • View Profile
Re: Getting world data
« Reply #5 on: July 18, 2017, 06:49:48 PM »
Look up coordinate_conversions.h
Not the most intuitive thing, but it's usable.

Offline Zhilkin

  • NPC
  • **
  • Posts: 134
    • View Profile
Re: Getting world data
« Reply #6 on: July 27, 2017, 09:14:08 AM »
Why map-files contained in maps\x.y.z subfolders have coordinate values multiplied by two (2*x,2*y,2*z)?

For example, there are following files:

\cdda\save\Magee\maps\0.0.0\10.0.0.map
\cdda\save\Magee\maps\0.0.0\10.1.0.map
\cdda\save\Magee\maps\0.0.0\10.2.0.map
\cdda\save\Magee\maps\0.0.0\10.3.0.map


These files have following coordinates:

"coordinates":[20,0,0]
"coordinates":[20,2,0]
"coordinates":[20,4,0]
"coordinates":[20,6,0]


Is it 2 *2 submaps which are described in `coordinate_conversions.h`?

Code: [Select]
* submap (sm): each overmap terrain contains (2*2) submaps.
 * Translating from sm to omt coordinates:
 * sm.x /= 2
 * sm.y /= 2

Offline Coolthulhu

  • Contributor
  • Survivor
  • ****
  • Posts: 3729
    • View Profile
Re: Getting world data
« Reply #7 on: July 27, 2017, 12:39:19 PM »
There are 4 coordinate systems:
  • Local map tile (1x1)
  • Submap (12x12 local map tiles)
  • Overmap tile (2x2 submaps, 24x24 local map tiles)
  • Overmap (180x180 overmap tiles)

Each file contains an overmap tile.

Offline Zhilkin

  • NPC
  • **
  • Posts: 134
    • View Profile
Re: Getting world data
« Reply #8 on: July 27, 2017, 06:36:49 PM »
Thanks!

Each file contains an overmap tile.
I presume youve meant `submap` instead of `overmap`? Otherwise there is no sense in having four map files.

Offline Coolthulhu

  • Contributor
  • Survivor
  • ****
  • Posts: 3729
    • View Profile
Re: Getting world data
« Reply #9 on: July 27, 2017, 06:47:29 PM »
No, not submap. A tile on overmap, so overmap tile - 2x2 submaps.