From 0517bb3753a86e5bccae871a9495fc681adc11a2 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Fri, 4 Sep 2026 20:06:15 -0500 Subject: [PATCH] feat(map): C_Map map hierarchy (Tier 2) Add GetMapInfo, GetMapChildrenInfo, GetMapInfoAtPosition and GetFallbackWorldMapID, giving the World -> Continent -> Zone tree. Continents carry no AreaTable id (their WorldMapArea rows have areaID 0), so they are named by a negative -(WorldMapArea row) -- mirroring the engine's own zoneOrSort idiom, where positive is an AreaTable zone and negative a QuestSort row. Names and mapType for those come from Map.dbc. Move the row math into Map::Area (RowForUiMapID, ContinentRowForMapID, RowRect, PercentInRow, WorldFromRow) and put the Tier 1 coordinate functions on it, so they accept continent ids too and GetMapRectOnMap honors topUiMapID. --- docs/API.md | 113 +++++++++++++-- src/map/Area.cpp | 96 ++++++++++++ src/map/Area.h | 41 ++++++ src/map/Coordinates.cpp | 103 ++++++------- src/map/MapInfo.cpp | 313 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 602 insertions(+), 64 deletions(-) create mode 100644 src/map/MapInfo.cpp diff --git a/docs/API.md b/docs/API.md index 8c1b657a..0feb77b6 100644 --- a/docs/API.md +++ b/docs/API.md @@ -420,7 +420,11 @@ build instructions. - [`C_Map.GetAreas()`](#c_mapgetareas) - [`C_Map.GetAreaTriggerInfo(triggerID)` / `C_Map.GetAreaTriggers([mapID])`](#c_mapgetareatriggerinfotriggerid--c_mapgetareatriggersmapid) - [`C_Map.GetBestMapForUnit(unitToken)`](#c_mapgetbestmapforunitunittoken) + - [`C_Map.GetFallbackWorldMapID()`](#c_mapgetfallbackworldmapid) - [`C_Map.GetMapAreaIDs()`](#c_mapgetmapareaids) + - [`C_Map.GetMapChildrenInfo(uiMapID)`](#c_mapgetmapchildreninfouimapid) + - [`C_Map.GetMapInfo(uiMapID)`](#c_mapgetmapinfouimapid) + - [`C_Map.GetMapInfoAtPosition(uiMapID, x, y)`](#c_mapgetmapinfoatpositionuimapid-x-y) - [`C_Map.GetMapOverlays([areaID])`](#c_mapgetmapoverlaysareaid) - [`C_Map.GetMapPosFromWorldPos(continentID, worldPosition[, overrideUiMapID])`](#c_mapgetmapposfromworldposcontinentid-worldposition-overrideuimapid) - [`C_Map.GetMapRectOnMap(uiMapID, topUiMapID)`](#c_mapgetmaprectonmapuimapid-topuimapid) @@ -10022,6 +10026,88 @@ point's offset in yards within the zone. local w, h = C_Map.GetMapWorldSize(85) -- Tirisfal: ~4518.7, 3012.5 ``` +### `C_Map.GetMapInfo(uiMapID)` + +Returns a map's details table, or `nil` for an id that names no map: + +| field | meaning | +|---|---| +| `mapID` | the id you passed | +| `name` | localized map name | +| `mapType` | `Enum.UIMapType`: `1` world, `2` continent, `3` zone, `4` dungeon | +| `parentMapID` | the map one level up, or `0` at the top | +| `flags` | always `0` | + +**What a `uiMapID` is.** Maps form a three-level tree: + +``` +World + +-- Continent (Kalimdor, Eastern Kingdoms) + +-- Zone (Durotar, Elwynn Forest, ...) +``` + +A **zone** is named by its positive `AreaTable.dbc` area id — the same +identity [`C_Map.GetBestMapForUnit`](#c_mapgetbestmapforunitunittoken) +returns. The **world and continent** levels have no area id, so they use +**negative** ids: Kalimdor is `-13`, Eastern Kingdoms is `-14`, and the world +map is `-694`. Instance maps (dungeons, raids, battlegrounds) are addressed +the same negative way. The two ranges never overlap, so every function in +this section accepts either kind. + +Read the ids from `GetMapChildrenInfo`, `GetFallbackWorldMapID`, or +`GetBestMapForUnit` rather than writing them in by hand. + +```lua +/dump C_Map.GetMapInfo(14) +-- mapID=14, name="Durotar", mapType=3, parentMapID=-13 +/dump C_Map.GetMapInfo(-13) +-- mapID=-13, name="Kalimdor", mapType=2, parentMapID=-694 +``` + +The world map names the whole world but carries no coordinate rect, so the +coordinate functions return `nil` for it. + +### `C_Map.GetMapChildrenInfo(uiMapID)` + +Returns an array of [`C_Map.GetMapInfo`](#c_mapgetmapinfouimapid) tables for +the maps one level below `uiMapID` — the continents of the world map, or the +zones of a continent. A zone has no children, so it gives an empty table. +Always returns a table. + +```lua +local continents = C_Map.GetMapChildrenInfo(C_Map.GetFallbackWorldMapID()) +-- 2 entries: Kalimdor (-13), Eastern Kingdoms (-14) + +local zones = C_Map.GetMapChildrenInfo(-13) +-- 35 entries: Durotar (14), Mulgore (215), ... +``` + +The zone list covers every map a continent has a world map for, city maps +included. + +### `C_Map.GetMapInfoAtPosition(uiMapID, x, y)` + +Returns the [`C_Map.GetMapInfo`](#c_mapgetmapinfouimapid) table for the zone +at a position on `uiMapID`, or `nil` when the point is on no zone. `x` and +`y` are map-relative, each `0..1`. + +Pass a continent id to answer "which zone sits under this point on the +continent map" — the hit test behind clicking a continent. + +```lua +/dump C_Map.GetMapInfoAtPosition(-13, 0.589, 0.52) +-- Durotar +``` + +### `C_Map.GetFallbackWorldMapID()` + +Returns the uiMapID of the whole-world map — the safe default to show when +no specific map is known. Returns `0` if the client has no world map. + +```lua +local worldID = C_Map.GetFallbackWorldMapID() -- -694 +``` + ### `C_Map.GetPlayerMapPosition(uiMapID, unitToken)` Returns a unit's position inside a zone as a `Vector2DMixin`. Call @@ -10029,11 +10115,11 @@ Returns a unit's position inside a zone as a `Vector2DMixin`. Call `.x` / `.y`. Returns `nil` when the unit's world position is outside that zone — a different zone, or a map with no world rect. -`uiMapID` is an `AreaTable.dbc` id — the same identity -[`C_Map.GetBestMapForUnit`](#c_mapgetbestmapforunitunittoken) returns. The -result comes from the unit's live world coordinates, so it is current the -moment you call it and does not depend on which map the world map frame -shows. +`uiMapID` is a zone or continent id (see +[`C_Map.GetMapInfo`](#c_mapgetmapinfouimapid)) — pass a continent to get the +unit's position on the continent map. The result comes from the unit's live +world coordinates, so it is current the moment you call it and does not +depend on which map the world map frame shows. ```lua local uiMapID = C_Map.GetBestMapForUnit("player") @@ -10057,8 +10143,9 @@ Returns `continentID, worldPosition`: - `worldPosition` — a `Vector2DMixin` of the world coordinates (`.x` north, `.y` west). -`uiMapID` is an `AreaTable.dbc` id. `mapPosition` is any table with `.x` / -`.y` in `0..1` (a `Vector2DMixin`, or the result of +`uiMapID` is a zone or continent id (see +[`C_Map.GetMapInfo`](#c_mapgetmapinfouimapid)). `mapPosition` is any table +with `.x` / `.y` in `0..1` (a `Vector2DMixin`, or the result of [`C_Map.GetPlayerMapPosition`](#c_mapgetplayermappositionuimapid-unittoken)). Returns `nil` when the zone has no world rect. This is the inverse of [`C_Map.GetMapPosFromWorldPos`](#c_mapgetmapposfromworldposcontinentid-worldposition-overrideuimapid). @@ -10079,9 +10166,9 @@ Converts absolute world coordinates to a map-relative position. Returns `continentID` is a `Map.dbc` map id. `worldPosition` is a table with `.x` / `.y` world coordinates. Without `overrideUiMapID`, the result is the zone -the point falls in. Pass `overrideUiMapID` (an `AreaTable.dbc` id) to force -the result into that specific zone. The call then returns `nil` when the -point lies outside that zone. This is the inverse of +the point falls in. Pass `overrideUiMapID` (a zone or continent id) to force +the result into that specific map. The call then returns `nil` when the +point lies outside it. This is the inverse of [`C_Map.GetWorldPosFromMapPos`](#c_mapgetworldposfrommapposuimapid-mapposition). ```lua @@ -10095,9 +10182,9 @@ Returns where a zone sits on its continent map, as four numbers: continent coordinates. Returns nothing (`nil`) when the zone has no world rect. -`uiMapID` is an `AreaTable.dbc` zone id. `topUiMapID` names the continent -the zone belongs to; the rectangle is measured on the zone's own -continent. +`uiMapID` is the zone. `topUiMapID` is the map to measure against, normally +the zone's continent (see [`C_Map.GetMapInfo`](#c_mapgetmapinfouimapid)). +When `topUiMapID` names no usable map, the zone's own continent is used. ```lua local minX, maxX, minY, maxY = C_Map.GetMapRectOnMap(14, 1) -- Durotar on Kalimdor diff --git a/src/map/Area.cpp b/src/map/Area.cpp index 11c1492d..25425f68 100644 --- a/src/map/Area.cpp +++ b/src/map/Area.cpp @@ -211,6 +211,102 @@ bool PercentInZone(int areaID, float x, float y, double *outMapX, double *outMap return false; } +int RowForUiMapID(int uiMapID) { + if (uiMapID > 0) + return RowForAreaID(static_cast(uiMapID)); + if (uiMapID < 0) { + const int row = -uiMapID; + if (DBC::Record(Offsets::VAR_WORLDMAP_AREA_RECORDS, + Offsets::VAR_WORLDMAP_AREA_COUNT, + static_cast(row)) != nullptr) + return row; + } + return -1; +} + +int UiMapIDForRow(int row) { + if (row <= 0) + return 0; + const uint8_t *rec = DBC::Record(Offsets::VAR_WORLDMAP_AREA_RECORDS, + Offsets::VAR_WORLDMAP_AREA_COUNT, + static_cast(row)); + if (rec == nullptr) + return 0; + const int areaID = Game::Read(rec, Offsets::OFF_WMA_AREA_ID); + return (areaID != 0) ? areaID : -row; +} + +int ContinentRowForMapID(int mapID) { + const int count = Game::Read(Offsets::VAR_WORLDMAP_AREA_COUNT); + for (int id = 1; id <= count; ++id) { + const uint8_t *rec = DBC::Record(Offsets::VAR_WORLDMAP_AREA_RECORDS, + Offsets::VAR_WORLDMAP_AREA_COUNT, + static_cast(id)); + if (rec == nullptr) + continue; + if (Game::Read(rec, Offsets::OFF_WMA_MAP_ID) != mapID) + continue; + if (Game::Read(rec, Offsets::OFF_WMA_AREA_ID) != 0) + continue; // a zone row, not the continent-spanning one + const double left = FloatField(rec, Offsets::OFF_WMA_LOC_LEFT); + const double right = FloatField(rec, Offsets::OFF_WMA_LOC_RIGHT); + const double top = FloatField(rec, Offsets::OFF_WMA_LOC_TOP); + const double bottom = FloatField(rec, Offsets::OFF_WMA_LOC_BOTTOM); + if (left - right <= 0.0 || top - bottom <= 0.0) + continue; // stray zero-rect continent row (the "World" row) + return id; + } + return -1; +} + +bool RowRect(int row, double *outLeft, double *outRight, double *outTop, + double *outBottom, int *outMapID) { + if (row <= 0) + return false; + const uint8_t *rec = DBC::Record(Offsets::VAR_WORLDMAP_AREA_RECORDS, + Offsets::VAR_WORLDMAP_AREA_COUNT, + static_cast(row)); + if (rec == nullptr) + return false; + if (outLeft != nullptr) + *outLeft = FloatField(rec, Offsets::OFF_WMA_LOC_LEFT); + if (outRight != nullptr) + *outRight = FloatField(rec, Offsets::OFF_WMA_LOC_RIGHT); + if (outTop != nullptr) + *outTop = FloatField(rec, Offsets::OFF_WMA_LOC_TOP); + if (outBottom != nullptr) + *outBottom = FloatField(rec, Offsets::OFF_WMA_LOC_BOTTOM); + if (outMapID != nullptr) + *outMapID = Game::Read(rec, Offsets::OFF_WMA_MAP_ID); + return true; +} + +bool PercentInRow(int row, float x, float y, double *outPx, double *outPy) { + double left, right, top, bottom; + if (!RowRect(row, &left, &right, &top, &bottom, nullptr)) + return false; + const double spanY = left - right; // horizontal (world Y) + const double spanX = top - bottom; // vertical (world X) + if (spanX <= 0.0 || spanY <= 0.0) + return false; + *outPx = (left - y) / spanY; + *outPy = (top - x) / spanX; + return true; +} + +bool WorldFromRow(int row, double px, double py, double *outX, double *outY) { + double left, right, top, bottom; + if (!RowRect(row, &left, &right, &top, &bottom, nullptr)) + return false; + const double spanY = left - right; + const double spanX = top - bottom; + if (spanX <= 0.0 || spanY <= 0.0) + return false; + *outY = left - px * spanY; + *outX = top - py * spanX; + return true; +} + bool ContinentPercent(int mapID, float x, float y, double *outPx, double *outPy) { const int count = Game::Read(Offsets::VAR_WORLDMAP_AREA_COUNT); for (int id = 1; id <= count; ++id) { diff --git a/src/map/Area.h b/src/map/Area.h index ee07e234..eba61cae 100644 --- a/src/map/Area.h +++ b/src/map/Area.h @@ -76,4 +76,45 @@ bool PercentInZone(int areaID, float x, float y, double *outMapX, double *outMap // projection retail's TaxiNodeInfo.position uses. bool ContinentPercent(int mapID, float x, float y, double *outPx, double *outPy); +// --- uiMapID identity ------------------------------------------------------ +// +// A ClassicAPI `uiMapID` names either a zone or a whole map: +// positive = an `AreaTable.dbc` area id (a zone) — what +// `C_Map.GetBestMapForUnit` returns. +// negative = -(WorldMapArea row) — continent / world / instance maps, +// which carry no AreaTable id (their rows have areaID 0). +// The negative half mirrors the engine's own two-namespaces-in-one-int idiom +// (a quest's `zoneOrSort`: positive = AreaTable zone, negative = -QuestSort +// row), so the two id spaces can never collide. + +// WorldMapArea row for a `uiMapID` in either namespace. Returns -1 when the +// id resolves to no row. +int RowForUiMapID(int uiMapID); + +// The `uiMapID` that names WorldMapArea row `row` — its areaID when it has +// one, else -(row). Returns 0 for an unreadable row. +int UiMapIDForRow(int row); + +// The continent-level WorldMapArea row for a `Map.dbc` map: the areaID == 0 +// row with a non-degenerate rect (the same selection `ContinentPercent` +// makes, so the stray zero-rect "World" row never wins). -1 when none. +int ContinentRowForMapID(int mapID); + +// Reads WorldMapArea row `row`'s placement rect and its `Map.dbc` map id. +// Every out-parameter is optional (pass null to skip it). False when the row +// is unreadable. +bool RowRect(int row, double *outLeft, double *outRight, double *outTop, + double *outBottom, int *outMapID); + +// Projects world (x, y) into row `row`'s rect as 0..1 (`outPx` horizontal off +// world Y, `outPy` vertical off world X). Unlike `PercentInZone` there is NO +// containment gate — a point outside the rect yields a value outside 0..1, and +// the caller decides whether that is meaningful. False for a missing or +// degenerate rect. +bool PercentInRow(int row, float x, float y, double *outPx, double *outPy); + +// Inverse of `PercentInRow`: a 0..1 position on row `row` back to +// continent-space world coordinates. False for a missing or degenerate rect. +bool WorldFromRow(int row, double px, double py, double *outX, double *outY); + } // namespace Map::Area diff --git a/src/map/Coordinates.cpp b/src/map/Coordinates.cpp index 90415109..25cee334 100644 --- a/src/map/Coordinates.cpp +++ b/src/map/Coordinates.cpp @@ -22,10 +22,13 @@ // // ## The identity scheme (shared with the rest of `Map::*`) // -// `uiMapID` is a vanilla `AreaTable.dbc` area id — the same identity -// `C_Map.GetBestMapForUnit` returns and `GetMapWorldSize` / `GetMapOverlays` -// accept. `continentID` is a `Map.dbc` map id (0 = Eastern Kingdoms, -// 1 = Kalimdor, or an instance map). World coordinates are continent-space +// `uiMapID` is a vanilla `AreaTable.dbc` area id for a zone — the same +// identity `C_Map.GetBestMapForUnit` returns and `GetMapWorldSize` / +// `GetMapOverlays` accept — or a NEGATIVE `-(WorldMapArea row)` for a +// continent / world / instance map (see `Map::Area`'s uiMapID section), so a +// continent map works anywhere a zone does. `continentID` is a `Map.dbc` map +// id (0 = Eastern Kingdoms, 1 = Kalimdor, or an instance map). World +// coordinates are continent-space // (WoW world axes: +X north, +Y west), exactly what `AreaTrigger.dbc` stores // and what the object `GetPosition` virtual returns. All four functions reuse // `Map::Area`'s zone rect math (`PercentInZone`, `ZonePercent`, @@ -51,29 +54,9 @@ namespace Map::Coordinates { namespace { -// A zone's WorldMapArea placement rect (world coords) plus its continent. -struct ZoneRect { - double left, right, top, bottom; // locLeft/Right bound world Y; locTop/Bottom world X - int continentID; // Map.dbc map id -}; - -// Fills `out` for an AreaTable `areaID`. False when the zone has no -// WorldMapArea row. -bool GetZoneRect(int areaID, ZoneRect *out) { - const int row = Map::Area::RowForAreaID(static_cast(areaID)); - if (row <= 0) - return false; - const uint8_t *rec = DBC::Record(Offsets::VAR_WORLDMAP_AREA_RECORDS, - Offsets::VAR_WORLDMAP_AREA_COUNT, - static_cast(row)); - if (rec == nullptr) - return false; - out->left = Game::Read(rec, Offsets::OFF_WMA_LOC_LEFT); - out->right = Game::Read(rec, Offsets::OFF_WMA_LOC_RIGHT); - out->top = Game::Read(rec, Offsets::OFF_WMA_LOC_TOP); - out->bottom = Game::Read(rec, Offsets::OFF_WMA_LOC_BOTTOM); - out->continentID = Game::Read(rec, Offsets::OFF_WMA_MAP_ID); - return true; +// True when a projected 0..1 map position actually lands on the map. +bool OnMap(double px, double py) { + return px >= 0.0 && px <= 1.0 && py >= 0.0 && py <= 1.0; } // Reads `t.x` / `t.y` from the table at absolute stack index `idx` (a vector2 @@ -122,18 +105,20 @@ int __fastcall Script_GetPlayerMapPosition(void *L) { const int areaID = ok ? static_cast(Game::Lua::ToNumber(L, 1)) : 0; const char *token = ok ? Game::Lua::ToString(L, 2) : nullptr; + const int row = ok ? Map::Area::RowForUiMapID(areaID) : -1; float pos[3]; - double mapX = 0.0, mapY = 0.0; - const bool have = ok && token != nullptr && areaID > 0 && + double px = 0.0, py = 0.0; + const bool have = ok && token != nullptr && row > 0 && Unit::Position::ReadToken(token, pos) && - Map::Area::PercentInZone(areaID, pos[0], pos[1], &mapX, &mapY); + Map::Area::PercentInRow(row, pos[0], pos[1], &px, &py) && + OnMap(px, py); Game::Lua::SetTop(L, 0); if (!have) { Game::Lua::PushNil(L); return 1; } - PushVector2D(L, mapX / 100.0, mapY / 100.0); + PushVector2D(L, px, py); return 1; } @@ -146,22 +131,22 @@ int __fastcall Script_GetWorldPosFromMapPos(void *L) { Game::Lua::IsNumber(L, 1) && Game::Lua::Type(L, 2) == Game::Lua::TYPE_TABLE; const int areaID = ok ? static_cast(Game::Lua::ToNumber(L, 1)) : 0; + const int row = ok ? Map::Area::RowForUiMapID(areaID) : -1; double mx = 0.0, my = 0.0; - ZoneRect r{}; - const bool have = - ok && ReadXY(L, 2, &mx, &my) && areaID > 0 && GetZoneRect(areaID, &r); + double worldX = 0.0, worldY = 0.0; + int continentID = 0; + const bool have = ok && ReadXY(L, 2, &mx, &my) && row > 0 && + Map::Area::RowRect(row, nullptr, nullptr, nullptr, nullptr, + &continentID) && + Map::Area::WorldFromRow(row, mx, my, &worldX, &worldY); Game::Lua::SetTop(L, 0); if (!have) { Game::Lua::PushNil(L); return 1; } - const double spanY = r.left - r.right; // world Y span (horizontal / mapX) - const double spanX = r.top - r.bottom; // world X span (vertical / mapY) - const double worldY = r.left - mx * spanY; // inverse of (left - y)/spanY - const double worldX = r.top - my * spanX; // inverse of (top - x)/spanX - Game::Lua::PushNumber(L, static_cast(r.continentID)); + Game::Lua::PushNumber(L, static_cast(continentID)); PushVector2D(L, worldX, worldY); // vector.x = world X (north), .y = world Y (west) return 2; } @@ -187,11 +172,17 @@ int __fastcall Script_GetMapPosFromWorldPos(void *L) { bool have = false; if (readOk) { if (hasOverride) { - have = overrideArea > 0 && - Map::Area::PercentInZone(overrideArea, static_cast(wx), - static_cast(wy), &mapX, &mapY); - if (have) + const int row = Map::Area::RowForUiMapID(overrideArea); + double px = 0.0, py = 0.0; + have = row > 0 && + Map::Area::PercentInRow(row, static_cast(wx), + static_cast(wy), &px, &py) && + OnMap(px, py); + if (have) { areaID = overrideArea; + mapX = px * 100.0; // normalized below with the ZonePercent path + mapY = py * 100.0; + } } else { have = Map::Area::ZonePercent(continentID, static_cast(wx), static_cast(wy), &areaID, &mapX, &mapY); @@ -215,17 +206,27 @@ int __fastcall Script_GetMapPosFromWorldPos(void *L) { // continent. Returns nothing (→ nil ×4) when the zone has no map row. int __fastcall Script_GetMapRectOnMap(void *L) { const int areaID = Game::Lua::IsNumber(L, 1) ? static_cast(Game::Lua::ToNumber(L, 1)) : 0; + const int topID = Game::Lua::IsNumber(L, 2) ? static_cast(Game::Lua::ToNumber(L, 2)) : 0; - ZoneRect r{}; + const int row = Map::Area::RowForUiMapID(areaID); + double left = 0.0, right = 0.0, top = 0.0, bottom = 0.0; + int continentID = 0; double aPx = 0.0, aPy = 0.0, bPx = 0.0, bPy = 0.0; - bool have = areaID > 0 && GetZoneRect(areaID, &r); + bool have = row > 0 && Map::Area::RowRect(row, &left, &right, &top, &bottom, + &continentID); if (have) { - // Project the zone rect's two extreme world corners through the - // continent transform. Corner A = (world X=top, Y=left); B = (bottom, right). - have = Map::Area::ContinentPercent(r.continentID, static_cast(r.top), - static_cast(r.left), &aPx, &aPy) && - Map::Area::ContinentPercent(r.continentID, static_cast(r.bottom), - static_cast(r.right), &bPx, &bPy); + // `topUiMapID` names the map to measure against; fall back to the + // zone's own continent when it isn't a usable map. + int topRow = (topID != 0) ? Map::Area::RowForUiMapID(topID) : -1; + if (topRow <= 0) + topRow = Map::Area::ContinentRowForMapID(continentID); + // Project the rect's two extreme world corners onto that map. + // Corner A = (world X=top, Y=left); B = (bottom, right). + have = topRow > 0 && + Map::Area::PercentInRow(topRow, static_cast(top), + static_cast(left), &aPx, &aPy) && + Map::Area::PercentInRow(topRow, static_cast(bottom), + static_cast(right), &bPx, &bPy); } Game::Lua::SetTop(L, 0); diff --git a/src/map/MapInfo.cpp b/src/map/MapInfo.cpp new file mode 100644 index 00000000..877a07f1 --- /dev/null +++ b/src/map/MapInfo.cpp @@ -0,0 +1,313 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +// `C_Map` map hierarchy — the tree the coordinate layer hangs off: +// +// GetMapInfo(uiMapID) -> UiMapDetails table +// GetMapChildrenInfo(uiMapID) -> array of UiMapDetails +// GetMapInfoAtPosition(uiMapID,x,y) -> UiMapDetails at a point +// GetFallbackWorldMapID() -> the world map's uiMapID +// +// ## The tree +// +// Three levels, all read from `WorldMapArea.dbc` + `Map.dbc`: +// +// World (the "World" row) +// +- Continent (Kalimdor, Eastern Kingdoms) +// +- Zone (Durotar, Elwynn Forest, ...) +// +// A zone is named by its positive `AreaTable.dbc` area id. The World and +// continent levels carry no AreaTable id (their `WorldMapArea` rows have +// areaID 0), so they use the negative `-(WorldMapArea row)` half of the +// uiMapID space — see the uiMapID section in `Map::Area`. Instance maps +// (dungeons, raids, battlegrounds) are the same shape as continents: an +// areaID-0 row, addressed negatively. +// +// A zone's parent is the continent-spanning row of the `Map.dbc` map it sits +// on. Each continent's parent is the World row. The World row's own rect is +// all zeroes, so it names the map but carries no geometry — the coordinate +// functions correctly report nothing for it. +// +// ## mapType +// +// `Enum.UIMapType`: Cosmic 0, World 1, Continent 2, Zone 3, Dungeon 4. A +// positive id is always a Zone. A negative id takes its type from the +// `Map.dbc` instance type of the map it names (0 = world map → Continent, +// anything else → Dungeon), except the World row itself. + +#include "Game.h" +#include "Offsets.h" +#include "dbc/Lookup.h" +#include "dbc/Names.h" +#include "map/Area.h" + +#include +#include + +namespace Map::MapInfo { + +namespace { + +// Enum.UIMapType values we can produce. +constexpr int kMapTypeWorld = 1; +constexpr int kMapTypeContinent = 2; +constexpr int kMapTypeZone = 3; +constexpr int kMapTypeDungeon = 4; + +// `Map.dbc` is indexed by map id, and map id 0 (Eastern Kingdoms) is a real +// row — so the shared `DBC::Record` (which treats id 0 as "unused slot") can't +// be used here. Mirrors the reader in `Instance::Info`. +const uint8_t *MapRecord(int mapID) { + if (mapID < 0) + return nullptr; + const int count = Game::Read(Offsets::VAR_MAP_COUNT); + if (mapID > count) + return nullptr; + auto *const *records = *reinterpret_cast( + static_cast(Offsets::VAR_MAP_RECORDS)); + if (records == nullptr) + return nullptr; + return records[mapID]; +} + +// Localized `Map.dbc` display name ("Eastern Kingdoms", "Deadmines"), or null. +const char *MapName(int mapID) { + const uint8_t *rec = MapRecord(mapID); + if (rec == nullptr) + return nullptr; + const uint32_t locale = Game::Read(Offsets::VAR_LOCALE_INDEX); + const char *name = *reinterpret_cast( + rec + Offsets::OFF_MAP_NAME + locale * 4); + if (name == nullptr || name[0] == '\0') + return nullptr; + return name; +} + +// `Map.dbc` instance type (0 = world map, 1 = dungeon, 2 = raid, 3 = BG). +// -1 when the map has no row. +int MapInstanceType(int mapID) { + const uint8_t *rec = MapRecord(mapID); + if (rec == nullptr) + return -1; + return Game::Read(rec, Offsets::OFF_MAP_INSTANCE_TYPE); +} + +// The WorldMapArea directory name of a row ("Kalimdor", "World", ""), or null. +const char *RowName(int row) { + if (row <= 0) + return nullptr; + const uint8_t *rec = DBC::Record(Offsets::VAR_WORLDMAP_AREA_RECORDS, + Offsets::VAR_WORLDMAP_AREA_COUNT, + static_cast(row)); + if (rec == nullptr) + return nullptr; + return Game::Read(rec, Offsets::OFF_WMA_NAME); +} + +bool IsWorldRow(int row) { + const char *name = RowName(row); + return name != nullptr && std::strcmp(name, "World") == 0; +} + +// The WorldMapArea row the client uses for the whole-world map: the areaID-0 +// row named "World". -1 when the client has none. +int WorldRow() { + const int count = Game::Read(Offsets::VAR_WORLDMAP_AREA_COUNT); + for (int id = 1; id <= count; ++id) { + const uint8_t *rec = DBC::Record(Offsets::VAR_WORLDMAP_AREA_RECORDS, + Offsets::VAR_WORLDMAP_AREA_COUNT, + static_cast(id)); + if (rec == nullptr || + Game::Read(rec, Offsets::OFF_WMA_AREA_ID) != 0) + continue; + if (IsWorldRow(id)) + return id; + } + return -1; +} + +// Builds the UiMapDetails table for `uiMapID` and leaves it on the stack top. +// Returns false (pushing nothing) when the id names no map. +bool PushMapInfo(void *L, int uiMapID) { + const int row = Map::Area::RowForUiMapID(uiMapID); + if (row <= 0) + return false; + + const char *name = nullptr; + int mapType = kMapTypeZone; + int parentMapID = 0; + + if (uiMapID > 0) { + // A zone: AreaTable name, parented to its continent. + name = DBC::AreaName(static_cast(uiMapID), + /*resolveToParent=*/false); + mapType = kMapTypeZone; + int continentID = 0; + if (Map::Area::RowRect(row, nullptr, nullptr, nullptr, nullptr, + &continentID)) { + const int contRow = Map::Area::ContinentRowForMapID(continentID); + if (contRow > 0) + parentMapID = -contRow; + } + } else if (IsWorldRow(row)) { + // The whole-world map: named by the row itself, no parent. + name = RowName(row); + mapType = kMapTypeWorld; + parentMapID = 0; + } else { + // A continent or instance map: Map.dbc supplies the display name and + // the type; continents hang off the world map. + int mapID = 0; + Map::Area::RowRect(row, nullptr, nullptr, nullptr, nullptr, &mapID); + name = MapName(mapID); + if (name == nullptr) + name = RowName(row); // custom map with no Map.dbc row + const int instanceType = MapInstanceType(mapID); + if (instanceType == 0) { + mapType = kMapTypeContinent; + const int worldRow = WorldRow(); + if (worldRow > 0) + parentMapID = -worldRow; + } else { + mapType = kMapTypeDungeon; + } + } + + Game::Lua::NewTable(L); + Game::Lua::SetFieldNumber(L, "mapID", static_cast(uiMapID)); + Game::Lua::SetFieldString(L, "name", name); + Game::Lua::SetFieldNumber(L, "mapType", static_cast(mapType)); + Game::Lua::SetFieldNumber(L, "parentMapID", static_cast(parentMapID)); + Game::Lua::SetFieldNumber(L, "flags", 0.0); + return true; +} + +// `C_Map.GetMapInfo(uiMapID)` — the map's details table, or nil. +int __fastcall Script_GetMapInfo(void *L) { + const bool ok = Game::Lua::IsNumber(L, 1); + const int uiMapID = ok ? static_cast(Game::Lua::ToNumber(L, 1)) : 0; + Game::Lua::SetTop(L, 0); + if (!ok || !PushMapInfo(L, uiMapID)) + Game::Lua::PushNil(L); + return 1; +} + +// `C_Map.GetMapChildrenInfo(uiMapID)` — the maps one level below `uiMapID`: +// the continents of the world map, or the zones of a continent / instance map. +// Always returns a table (empty for a zone, which has no children). +int __fastcall Script_GetMapChildrenInfo(void *L) { + const bool ok = Game::Lua::IsNumber(L, 1); + const int uiMapID = ok ? static_cast(Game::Lua::ToNumber(L, 1)) : 0; + const int row = ok ? Map::Area::RowForUiMapID(uiMapID) : -1; + + // A zone (positive id) has no children; anything else enumerates below. + const bool wantContinents = (row > 0) && IsWorldRow(row); + int parentMapDbcID = 0; + const bool wantZones = (row > 0) && uiMapID < 0 && !wantContinents && + Map::Area::RowRect(row, nullptr, nullptr, nullptr, + nullptr, &parentMapDbcID); + + Game::Lua::SetTop(L, 0); + Game::Lua::NewTable(L); + if (!wantContinents && !wantZones) + return 1; + + const int count = Game::Read(Offsets::VAR_WORLDMAP_AREA_COUNT); + int outIdx = 0; + for (int id = 1; id <= count; ++id) { + const uint8_t *rec = DBC::Record(Offsets::VAR_WORLDMAP_AREA_RECORDS, + Offsets::VAR_WORLDMAP_AREA_COUNT, + static_cast(id)); + if (rec == nullptr) + continue; + const int areaID = Game::Read(rec, Offsets::OFF_WMA_AREA_ID); + const int mapID = Game::Read(rec, Offsets::OFF_WMA_MAP_ID); + + int childID = 0; + if (wantContinents) { + // Continent-spanning rows of real world maps, minus the World row. + if (areaID != 0 || id == row || MapInstanceType(mapID) != 0) + continue; + if (Map::Area::ContinentRowForMapID(mapID) != id) + continue; // skip the stray zero-rect duplicate + childID = -id; + } else { + // Zone rows of this map. + if (areaID == 0 || mapID != parentMapDbcID) + continue; + childID = areaID; + } + + outIdx += 1; + Game::Lua::PushNumber(L, static_cast(outIdx)); + if (!PushMapInfo(L, childID)) { + Game::Lua::SetTop(L, -2); // drop the index; nothing to store + outIdx -= 1; + continue; + } + Game::Lua::SetTable(L, -3); + } + return 1; +} + +// `C_Map.GetMapInfoAtPosition(uiMapID, x, y)` — the zone at a normalized +// (0..1) point on `uiMapID`, or nil when the point is on no zone. +int __fastcall Script_GetMapInfoAtPosition(void *L) { + const bool ok = Game::Lua::IsNumber(L, 1) && Game::Lua::IsNumber(L, 2) && + Game::Lua::IsNumber(L, 3); + const int uiMapID = ok ? static_cast(Game::Lua::ToNumber(L, 1)) : 0; + const double px = ok ? Game::Lua::ToNumber(L, 2) : 0.0; + const double py = ok ? Game::Lua::ToNumber(L, 3) : 0.0; + + const int row = ok ? Map::Area::RowForUiMapID(uiMapID) : -1; + double worldX = 0.0, worldY = 0.0; + int mapID = 0; + int areaID = 0; + double mapX = 0.0, mapY = 0.0; + const bool have = + row > 0 && + Map::Area::RowRect(row, nullptr, nullptr, nullptr, nullptr, &mapID) && + Map::Area::WorldFromRow(row, px, py, &worldX, &worldY) && + Map::Area::ZonePercent(mapID, static_cast(worldX), + static_cast(worldY), &areaID, &mapX, &mapY); + + Game::Lua::SetTop(L, 0); + if (!have || !PushMapInfo(L, areaID)) + Game::Lua::PushNil(L); + return 1; +} + +// `C_Map.GetFallbackWorldMapID()` — the uiMapID of the whole-world map, the +// safe default when no better map is known. +int __fastcall Script_GetFallbackWorldMapID(void *L) { + const int row = WorldRow(); + Game::Lua::SetTop(L, 0); + Game::Lua::PushNumber(L, (row > 0) ? static_cast(-row) : 0.0); + return 1; +} + +void RegisterLuaFunctions() { + Game::Lua::RegisterTableFunction("C_Map", "GetMapInfo", &Script_GetMapInfo); + Game::Lua::RegisterTableFunction("C_Map", "GetMapChildrenInfo", + &Script_GetMapChildrenInfo); + Game::Lua::RegisterTableFunction("C_Map", "GetMapInfoAtPosition", + &Script_GetMapInfoAtPosition); + Game::Lua::RegisterTableFunction("C_Map", "GetFallbackWorldMapID", + &Script_GetFallbackWorldMapID); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace + +} // namespace Map::MapInfo