2021-12-26 12:09:45 +00:00
|
|
|
/*
|
|
|
|
** usermap.cpp
|
|
|
|
** Management for the user maps menu
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2020-2021 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "razemenu.h"
|
|
|
|
#include "version.h"
|
|
|
|
#include "m_png.h"
|
|
|
|
#include "filesystem.h"
|
|
|
|
#include "v_text.h"
|
|
|
|
#include "gstrings.h"
|
|
|
|
#include "serializer.h"
|
|
|
|
#include "vm.h"
|
|
|
|
#include "i_system.h"
|
|
|
|
#include "v_video.h"
|
|
|
|
#include "findfile.h"
|
|
|
|
#include "v_draw.h"
|
|
|
|
#include "usermap.h"
|
2021-12-26 13:38:36 +00:00
|
|
|
#include "gamecontrol.h"
|
2021-12-26 16:45:26 +00:00
|
|
|
#include "mapinfo.h"
|
2021-12-26 18:58:52 +00:00
|
|
|
#include "v_draw.h"
|
2021-12-26 13:38:36 +00:00
|
|
|
|
2021-12-26 16:45:26 +00:00
|
|
|
static FUsermapDirectory root;
|
2021-12-26 13:38:36 +00:00
|
|
|
|
|
|
|
void InsertMap(int lumpnum)
|
|
|
|
{
|
|
|
|
FString filename = fileSystem.GetFileFullName(lumpnum);
|
|
|
|
auto path = filename.Split("/");
|
|
|
|
|
|
|
|
auto current = &root;
|
|
|
|
for (unsigned i = 0; i < path.Size() - 1; i++)
|
|
|
|
{
|
2021-12-26 16:45:26 +00:00
|
|
|
unsigned place = current->subdirectories.FindEx([=](const FUsermapDirectory& entry) { return entry.dirname.CompareNoCase(path[i]) == 0; });
|
2021-12-26 13:38:36 +00:00
|
|
|
if (place == current->subdirectories.Size())
|
|
|
|
{
|
|
|
|
place = current->subdirectories.Reserve(1);
|
2021-12-26 16:45:26 +00:00
|
|
|
current->subdirectories.Last().dirname = path[i];
|
2021-12-26 13:38:36 +00:00
|
|
|
}
|
|
|
|
current = ¤t->subdirectories[place];
|
|
|
|
}
|
|
|
|
current->entries.Reserve(1);
|
|
|
|
current->entries.Last().displayname = path.Last();
|
|
|
|
current->entries.Last().filename = fileSystem.GetFileFullName(lumpnum);
|
|
|
|
current->entries.Last().container = fileSystem.GetResourceFileName(fileSystem.GetFileContainer(lumpnum));
|
|
|
|
current->entries.Last().size = fileSystem.FileLength(lumpnum);
|
2023-10-08 07:15:32 +00:00
|
|
|
auto mapinfo = FindMapByName(StripExtension(path.Last().GetChars()).GetChars());
|
2021-12-26 16:45:26 +00:00
|
|
|
if (mapinfo) current->entries.Last().info = mapinfo->name;
|
2021-12-26 13:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidateMap(int lumpnum)
|
|
|
|
{
|
|
|
|
FString filename = fileSystem.GetFileFullName(lumpnum);
|
|
|
|
|
2023-10-08 07:15:32 +00:00
|
|
|
if (fileSystem.FindFile(filename.GetChars()) != lumpnum) return false;
|
2021-12-26 13:38:36 +00:00
|
|
|
auto fr = fileSystem.OpenFileReader(lumpnum);
|
|
|
|
uint8_t check[4];
|
|
|
|
fr.Read(&check, 4);
|
|
|
|
if (!isBlood())
|
|
|
|
{
|
|
|
|
if (check[0] < 5 || check[0] > 9 || check[1] || check[2] || check[3]) return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (memcmp(check, "BLM\x1a", 4)) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-26 16:45:26 +00:00
|
|
|
void SortEntries(FUsermapDirectory& dir)
|
2021-12-26 13:38:36 +00:00
|
|
|
{
|
|
|
|
qsort(dir.subdirectories.Data(), dir.subdirectories.Size(), sizeof(dir.subdirectories[0]), [](const void* a, const void* b) -> int
|
|
|
|
{
|
2021-12-26 16:45:26 +00:00
|
|
|
auto A = (FUsermapDirectory*)a;
|
|
|
|
auto B = (FUsermapDirectory*)b;
|
2021-12-26 13:38:36 +00:00
|
|
|
|
2021-12-26 16:45:26 +00:00
|
|
|
return A->dirname.CompareNoCase(B->dirname);
|
2021-12-26 13:38:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
qsort(dir.entries.Data(), dir.entries.Size(), sizeof(dir.entries[0]), [](const void* a, const void* b) -> int
|
|
|
|
{
|
2021-12-26 16:45:26 +00:00
|
|
|
auto A = (FUsermapEntry*)a;
|
|
|
|
auto B = (FUsermapEntry*)b;
|
2021-12-26 13:38:36 +00:00
|
|
|
|
|
|
|
return A->displayname.CompareNoCase(B->displayname);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto& subdir : dir.subdirectories)
|
|
|
|
{
|
|
|
|
subdir.parent = &dir;
|
|
|
|
SortEntries(subdir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReadUserMaps()
|
|
|
|
{
|
2021-12-26 16:45:26 +00:00
|
|
|
static bool didit = false;
|
|
|
|
if (didit) return;
|
|
|
|
didit = true;
|
|
|
|
|
2021-12-26 13:38:36 +00:00
|
|
|
int numfiles = fileSystem.GetNumEntries();
|
|
|
|
|
|
|
|
for (int i = 0; i < numfiles; i++)
|
|
|
|
{
|
|
|
|
auto fn1 = fileSystem.GetFileFullName(i);
|
|
|
|
if (!fn1 || !*fn1) continue;
|
2021-12-26 16:57:17 +00:00
|
|
|
FString lowfn = fn1;
|
|
|
|
if (lowfn.Right(4).CompareNoCase(".map")) continue;
|
2021-12-26 13:38:36 +00:00
|
|
|
if (!ValidateMap(i)) continue;
|
|
|
|
InsertMap(i);
|
|
|
|
}
|
|
|
|
SortEntries(root);
|
|
|
|
}
|
|
|
|
|
2021-12-26 18:58:52 +00:00
|
|
|
void LoadMapPreview(FUsermapEntry* entry)
|
|
|
|
{
|
|
|
|
if (entry->wallsread) return;
|
2023-10-08 07:15:32 +00:00
|
|
|
entry->walls = loadMapWalls(entry->filename.GetChars());
|
2021-12-26 18:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnloadMapPreviews(FUsermapDirectory* dir)
|
|
|
|
{
|
|
|
|
for (auto& entry : dir->entries)
|
|
|
|
{
|
|
|
|
if (entry.walls.Size() > 0) entry.wallsread = false;
|
|
|
|
entry.walls.Reset();
|
|
|
|
}
|
|
|
|
for (auto& sub : dir->subdirectories)
|
|
|
|
{
|
|
|
|
UnloadMapPreviews(&sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 16:45:26 +00:00
|
|
|
DEFINE_FIELD(FUsermapEntry, filename);
|
|
|
|
DEFINE_FIELD(FUsermapEntry, container);
|
|
|
|
DEFINE_FIELD(FUsermapEntry, displayname);
|
|
|
|
DEFINE_FIELD(FUsermapEntry, info);
|
|
|
|
DEFINE_FIELD(FUsermapEntry, size);
|
|
|
|
|
|
|
|
DEFINE_FIELD(FUsermapDirectory, dirname);
|
|
|
|
DEFINE_FIELD(FUsermapDirectory, parent);
|
|
|
|
|
|
|
|
|
|
|
|
DEFINE_ACTION_FUNCTION(FUsermapDirectory, ReadData)
|
2021-12-26 13:38:36 +00:00
|
|
|
{
|
|
|
|
ReadUserMaps();
|
2021-12-26 16:45:26 +00:00
|
|
|
ACTION_RETURN_POINTER(&root);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_ACTION_FUNCTION(FUsermapDirectory, GetNumEntries)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FUsermapDirectory);
|
|
|
|
ACTION_RETURN_INT(self->entries.Size());
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_ACTION_FUNCTION(FUsermapDirectory, GetNumDirectories)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FUsermapDirectory);
|
|
|
|
ACTION_RETURN_INT(self->subdirectories.Size());
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_ACTION_FUNCTION(FUsermapDirectory, GetEntry)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FUsermapDirectory);
|
|
|
|
|
|
|
|
PARAM_UINT(num);
|
|
|
|
if (num >= self->entries.Size()) ACTION_RETURN_POINTER(nullptr);
|
|
|
|
ACTION_RETURN_POINTER(&self->entries[num]);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_ACTION_FUNCTION(FUsermapDirectory, GetDirectory)
|
|
|
|
{
|
|
|
|
PARAM_SELF_STRUCT_PROLOGUE(FUsermapDirectory);
|
|
|
|
|
|
|
|
PARAM_UINT(num);
|
|
|
|
if (num >= self->subdirectories.Size()) ACTION_RETURN_POINTER(nullptr);
|
|
|
|
ACTION_RETURN_POINTER(&self->subdirectories[num]);
|
|
|
|
}
|
|
|
|
|
2021-12-26 18:58:52 +00:00
|
|
|
DEFINE_ACTION_FUNCTION(_UserMapMenu, DrawPreview)
|
2021-12-26 16:45:26 +00:00
|
|
|
{
|
2021-12-26 18:58:52 +00:00
|
|
|
PARAM_PROLOGUE;
|
|
|
|
PARAM_POINTER(entry, FUsermapEntry);
|
2021-12-26 16:45:26 +00:00
|
|
|
PARAM_INT(left);
|
|
|
|
PARAM_INT(top);
|
|
|
|
PARAM_INT(width);
|
|
|
|
PARAM_INT(height);
|
2021-12-26 18:58:52 +00:00
|
|
|
if (!entry) return 0;
|
|
|
|
LoadMapPreview(entry);
|
|
|
|
if (entry->walls.Size() == 0) return 0;
|
2022-01-30 12:03:06 +00:00
|
|
|
double minx = INT_MAX, miny = INT_MAX, maxx = INT_MIN, maxy = INT_MIN;
|
2021-12-26 18:58:52 +00:00
|
|
|
for (auto& wal : entry->walls)
|
|
|
|
{
|
2022-01-30 12:03:06 +00:00
|
|
|
if (wal.pos.X < minx) minx = wal.pos.X;
|
|
|
|
if (wal.pos.X > maxx) maxx = wal.pos.X;
|
|
|
|
if (wal.pos.Y < miny) miny = wal.pos.Y;
|
|
|
|
if (wal.pos.Y > maxy) maxy = wal.pos.Y;
|
2021-12-26 18:58:52 +00:00
|
|
|
}
|
2022-01-30 12:03:06 +00:00
|
|
|
float scalex = float(width / (maxx - minx));
|
|
|
|
float scaley = float(height / (maxy - miny));
|
2021-12-26 18:58:52 +00:00
|
|
|
float scale = min(scalex, scaley);
|
2022-09-02 08:39:01 +00:00
|
|
|
|
|
|
|
DVector2 center = { (minx + maxx) * 0.5, (miny + maxy) * 0.5 };
|
|
|
|
DVector2 dcenter = { left + (width * 0.5), top + (height * 0.5) };
|
2021-12-26 18:58:52 +00:00
|
|
|
|
|
|
|
for (auto& wal : entry->walls)
|
|
|
|
{
|
|
|
|
if (wal.nextwall < 0) continue;
|
|
|
|
auto point2 = &entry->walls[wal.point2];
|
2022-09-02 08:39:01 +00:00
|
|
|
twod->AddLine(dcenter + (wal.pos - center) * scale, dcenter + (point2->pos - center) * scale, nullptr, 0xff808080);
|
2021-12-26 18:58:52 +00:00
|
|
|
}
|
|
|
|
for (auto& wal : entry->walls)
|
|
|
|
{
|
|
|
|
if (wal.nextwall >= 0) continue;
|
|
|
|
auto point2 = &entry->walls[wal.point2];
|
2022-09-02 08:39:01 +00:00
|
|
|
twod->AddLine(dcenter + (wal.pos - center) * scale, dcenter + (point2->pos - center) * scale, nullptr, 0xffffffff);
|
2021-12-26 18:58:52 +00:00
|
|
|
}
|
2021-12-26 16:45:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_ACTION_FUNCTION(_UsermapMenu, StartMap)
|
|
|
|
{
|
|
|
|
PARAM_PROLOGUE;
|
|
|
|
PARAM_POINTER(entry, FUsermapEntry);
|
|
|
|
|
|
|
|
if (DMenu::InMenu == 0)
|
|
|
|
{
|
|
|
|
ThrowAbortException(X_OTHER, "Attempt to start user map outside of menu code");
|
|
|
|
}
|
2021-12-27 08:26:56 +00:00
|
|
|
NewGameStartupInfo.Episode = -1;
|
|
|
|
NewGameStartupInfo.Level = -1;
|
2023-10-08 07:15:32 +00:00
|
|
|
NewGameStartupInfo.Map = SetupUserMap(entry->filename.GetChars(), g_gameType & GAMEFLAG_DUKE ? "dethtoll.mid" : nullptr);
|
2021-12-27 08:26:56 +00:00
|
|
|
M_SetMenu(NAME_Skillmenu, INT_MAX);
|
2021-12-26 16:45:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|