Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 42 additions & 56 deletions game/level.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <game/level.hpp>

//This is a much awsomer version
#include <core/game_data.hpp>
#include <core/wad_types.hpp>
#include <rndr/system.hpp>
#include <stdx/to.hpp>
#include <algorithm>
#include <iostream>

namespace game
{
Expand All @@ -13,7 +15,10 @@ namespace game
auto load(core::game_data& data, const size_t lump, const Conversion to_game_type)
{
const auto map_vertices = core::cache_lump_num_as_span<MapType>(data, lump);
return map_vertices | std::views::transform(to_game_type) | stdx::to<std::vector>();
std::vector<decltype(to_game_type(map_vertices[0]))> result;
result.reserve(map_vertices.size());
for (auto& v : map_vertices) result.push_back(to_game_type(v));
return result;
}

template <typename T>
Expand All @@ -40,16 +45,17 @@ namespace game
auto stream = core::lump_byte_stream(data, lump);
const auto info = stream.read<blockmap_info>();
const auto map = stream.read_span<short>(static_cast<size_t>(info.width * info.height));
return {.width = info.width,
.height = info.height,
.map = map,
.x = core::units(info.x),
.y = core::units(info.y)};

// todo clear out mobj chains
// const auto count = sizeof(*blocklinks) * bmapwidth*bmapheight;
// blocklinks = Z_Malloc (count,PU_LEVEL, 0);
// memset (blocklinks, 0, count);

blockmap_t bmap{.width = info.width,
.height = info.height,
.map = map,
.x = core::units(info.x),
.y = core::units(info.y)};

// Initialize block links to nullptr
bmap.links.resize(bmap.width * bmap.height, nullptr);

return bmap;
}

vertices_t load_vertices(core::game_data& data, const size_t lump)
Expand Down Expand Up @@ -165,59 +171,41 @@ namespace game

void group_lines(level_t& level)
{
// look up sector number for each subsector
// Assign sector to each subsector
for (auto& s : level.sub_sectors)
s.sector = level.segs[s.first_line].side_def->sector;

// count number of lines in each sector
// Count number of lines in each sector
for (auto& l : level.lines)
{
if (l.front_sector != nullptr) l.front_sector->lines.push_back(&l);

if (l.back_sector != nullptr && l.front_sector != l.back_sector) l.back_sector->lines.push_back(&l);
if (l.back_sector != nullptr && l.front_sector != l.back_sector)
l.back_sector->lines.push_back(&l);
}

/*todo

// Generate bounding boxes for sectors

sector = sectors;
for (i = 0; i < numsectors; i++, sector++)
// Generate bounding boxes and sound origins for sectors
for (auto& sec : level.sectors)
{
M_ClearBox(bbox);

for (j = 0; j < sector->linecount; j++)
bounding_box_t bbox{};
if (!sec.lines.empty())
{
li = sector->lines[j];

M_AddToBox(bbox, li->v1->x, li->v1->y);
M_AddToBox(bbox, li->v2->x, li->v2->y);
bbox.left = bbox.bottom = std::numeric_limits<core::units>::max();
bbox.right = bbox.top = std::numeric_limits<core::units>::min();

for (auto* l : sec.lines)
{
bbox.left = std::min({bbox.left, l->v1->x, l->v2->x});
bbox.right = std::max({bbox.right, l->v1->x, l->v2->x});
bbox.bottom = std::min({bbox.bottom, l->v1->y, l->v2->y});
bbox.top = std::max({bbox.top, l->v1->y, l->v2->y});
}
}

// set the degenmobj_t to the middle of the bounding box
sector->soundorg.x = (bbox[BOXRIGHT] + bbox[BOXLEFT]) / 2;
sector->soundorg.y = (bbox[BOXTOP] + bbox[BOXBOTTOM]) / 2;

// adjust bounding box to map blocks
block = (bbox[BOXTOP] - bmaporgy + MAXRADIUS) >> MAPBLOCKSHIFT;
block = block >= bmapheight ? bmapheight - 1 : block;
sector->blockbox[BOXTOP] = block;

block = (bbox[BOXBOTTOM] - bmaporgy - MAXRADIUS) >> MAPBLOCKSHIFT;
block = block < 0 ? 0 : block;
sector->blockbox[BOXBOTTOM] = block;

block = (bbox[BOXRIGHT] - bmaporgx + MAXRADIUS) >> MAPBLOCKSHIFT;
block = block >= bmapwidth ? bmapwidth - 1 : block;
sector->blockbox[BOXRIGHT] = block;

block = (bbox[BOXLEFT] - bmaporgx - MAXRADIUS) >> MAPBLOCKSHIFT;
block = block < 0 ? 0 : block;
sector->blockbox[BOXLEFT] = block;
sec.bbox = bbox;
sec.sound_origin = {(bbox.left + bbox.right) / 2, (bbox.bottom + bbox.top) / 2};
}
*/
}
}
} // namespace

level_t load_level(core::game_data& data, const rndr::system& renderer, const std::string& lump_name,
const std::string& sky_name)
Expand All @@ -231,14 +219,12 @@ namespace game
lvl.vertices = load_vertices(data, lump_num + static_cast<size_t>(core::map_lump::vertices));
lvl.sectors = load_sectors(data, lump_num + static_cast<size_t>(core::map_lump::sectors));
lvl.sides = load_sides(data, renderer, lvl.sectors, lump_num + static_cast<size_t>(core::map_lump::side_defs));
lvl.lines =
load_lines(data, lvl.vertices, lvl.sides, lump_num + static_cast<size_t>(core::map_lump::line_defs));
lvl.lines = load_lines(data, lvl.vertices, lvl.sides, lump_num + static_cast<size_t>(core::map_lump::line_defs));
lvl.sub_sectors = load_sub_sectors(data, lump_num + static_cast<size_t>(core::map_lump::sub_sectors));
lvl.nodes = load_nodes(data, lump_num + static_cast<size_t>(core::map_lump::nodes));
lvl.segs =
load_segs(data, lvl.vertices, lvl.lines, lvl.sides, lump_num + static_cast<size_t>(core::map_lump::segs));
lvl.segs = load_segs(data, lvl.vertices, lvl.lines, lvl.sides, lump_num + static_cast<size_t>(core::map_lump::segs));
group_lines(lvl);

return lvl;
}
}
} // namespace game