#include "Morault_UDMF.h"
#include <string>
#include "Morault_Utils.h"
#include <sstream>
#include <fstream>
#include <filesystem>
void Morault::UDMF::UDMFMap::LoadMap(std::string textmap)
{
	std::stringstream text(textmap);
	std::string previousLine;
	std::string line;
	std::string blockType;
	bool inblock = false;

	Linedef linedef;
	Sidedef sidedef;
	Vertex vertex;
	Sector sector;

	while (std::getline(text, line)) {

		auto [key, val] = Morault::Utils::Strings::ParseCLineIntoKeyVal(line);
		if (key == "namespace") udmfNamespace = val;
		if (key == "") {
			if (line == "{") {
				blockType = Morault::Utils::Strings::SingleWordTrimmer(previousLine);
				if (blockType == "linedef") linedef = Linedef();
				if (blockType == "sidedef") sidedef = Sidedef();
				if (blockType == "vertex") vertex = Vertex();
				if (blockType == "sector") sector = Sector();
				inblock = true;
				if (blockType != "linedef" && blockType != "sidedef" && blockType != "vertex" && blockType != "sector") {
					std::cout << "Unsupported block type " << blockType << "\n";
					inblock = false;
				}
			}
			if (line == "}") {
				inblock = false;
				if (blockType == "linedef") linedefs.push_back(linedef);
				if (blockType == "sidedef") sidedefs.push_back(sidedef);
				if (blockType == "vertex") vertexes.push_back(vertex);
				if (blockType == "sector") sectors.push_back(sector);
			}
		}
		else {
			if (inblock) {
				if (blockType == "linedef") {
					if (key == "v1") linedef.v1 = std::stoi(val);
					if (key == "v2") linedef.v2 = std::stoi(val);
					if (key == "sidefront") linedef.sidefront = std::stoi(val);
					
					if (key == "sideback") linedef.sideback = std::stoi(val);
			//was	if (key == "sideback") linedef.sidefront = std::stoi(val);

					
					if (key == "blocking") linedef.blocking = val == "true";
				}
				if (blockType == "sidedef") {
					if (key == "sector") sidedef.sector = std::stoi(val);
					if (key == "texturemiddle") sidedef.texturemiddle = val;
				}
				if (blockType == "vertex") {
					if (key == "x") vertex.x = std::stof(val);
					if (key == "y") vertex.y = std::stof(val);
				}
				if (blockType == "sector") {
					if (key == "texturefloor") sector.texturefloor = val;
					if (key == "textureceiling") sector.textureceiling = val;
					if (key == "heightceiling") sector.heightceiling = std::stoi(val);
					if (key == "heightfloor") sector.heightfloor = std::stoi(val);
				}
			}
		}

		// std::cout << "Key " << key << " as " << val << std::endl;
		previousLine = line;
	}
	std::cout << "Linedefs: " << linedefs.size() << "\n";
	std::cout << "Sidedefs: " << sidedefs.size() << "\n";
	std::cout << "Vertexes: " << vertexes.size() << "\n";
	std::cout << "Sectors: " << sectors.size() << "\n";

}

Model Morault::UDMF::LoadModelUDMFWalls(UDMFMap map)
{
	std::string vertices = "";
	int verticeCount = 0;
	std::string vertexnormals = "";
	int vertexNormalsCount = 0;
	std::string faces = "";


	for (int i = 0; i < map.linedefs.size(); i++) {
		// 	DrawLine(map.vertexes[map.linedefs[i].v1].x + offset.x, map.vertexes[map.linedefs[i].v1].y + offset.y, map.vertexes[map.linedefs[i].v2].x + offset.x, map.vertexes[map.linedefs[i].v2].y + offset.y, YELLOW);

		bool valuesUninitialized = true;
		float lowestPoint = 0;
		float midLowestPoint = 0;
		float midHighestPoint = 0;
		float highestPoint = 0;
		if (map.linedefs[i].sidefront != -1) {
			lowestPoint = map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightfloor;
			highestPoint = map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightceiling;
			midLowestPoint = lowestPoint;
			midHighestPoint = highestPoint;
			valuesUninitialized = false;
		}
		if (map.linedefs[i].sideback != -1) {
			if (valuesUninitialized) {
				// std::cout << "values uninit\n";
				lowestPoint = map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightfloor;
				highestPoint = map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightceiling;
				midLowestPoint = lowestPoint;
				midHighestPoint = highestPoint;
			}
			else {
				// lowestPoint = map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightfloor < lowestPoint ? map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightfloor : lowestPoint;
				// highestPoint = map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightceiling > highestPoint ? map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightceiling : highestPoint;
				int lowPoint = map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightfloor;
				int highPoint = map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightceiling;

				if (lowPoint < lowestPoint) lowestPoint = lowPoint;
				else midLowestPoint = lowPoint;


				if (highPoint > highestPoint) highestPoint = highPoint;
				else midHighestPoint = highPoint;

			}
			valuesUninitialized = false;

		}
		// std::cout << lowestPoint << " " << midLowestPoint << " " << midHighestPoint << " " << highestPoint << "\n";
		if (map.linedefs[i].sidefront != -1) {



			if (map.sidedefs[map.linedefs[i].sidefront].texturemiddle != "") {
				/*DrawTriangle3D(
					{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightceiling,
						map.vertexes[map.linedefs[i].v1].y
					},
						{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightfloor,
						map.vertexes[map.linedefs[i].v1].y
						},
						{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightfloor,
						map.vertexes[map.linedefs[i].v2].y
						},
					ColorLerp({ 255,0,0,255 }, { 0,0,255,255 }, (float)i / (float)map.linedefs.size())

				);*/

				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v1].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightceiling,
					map.vertexes[map.linedefs[i].v1].y);
				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v1].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightfloor,
					map.vertexes[map.linedefs[i].v1].y);
				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v2].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightfloor,
					map.vertexes[map.linedefs[i].v2].y);


				vertexNormalsCount++;
				vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

				faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
				verticeCount += 3;

				/*DrawTriangle3D(
					{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightceiling,
						map.vertexes[map.linedefs[i].v1].y
					},
						{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightfloor,
						map.vertexes[map.linedefs[i].v2].y
						},
						{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightceiling,
						map.vertexes[map.linedefs[i].v2].y
						},
					ColorLerp({ 255,0,0,255 }, { 0,255,0,255 }, (float)i / (float)map.linedefs.size())

				);*/


				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v1].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightceiling,
					map.vertexes[map.linedefs[i].v1].y);
				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v2].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightfloor,
					map.vertexes[map.linedefs[i].v2].y);
				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v2].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sidefront].sector].heightceiling,
					map.vertexes[map.linedefs[i].v2].y);

				vertexNormalsCount++;
				vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

				faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
				verticeCount += 3;

			}

			/*DrawTriangle3D(
				{
					-1 * map.vertexes[map.linedefs[i].v1].x,
					highestPoint,
					map.vertexes[map.linedefs[i].v1].y
				},
					{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						midHighestPoint,
						map.vertexes[map.linedefs[i].v1].y
					},
					{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						midHighestPoint,
						map.vertexes[map.linedefs[i].v2].y
					},
				ColorLerp({ 255,0,0,255 }, { 0,0,255,255 }, (float)i / (float)map.linedefs.size())

			);*/

			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				highestPoint,
				map.vertexes[map.linedefs[i].v1].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				midHighestPoint,
				map.vertexes[map.linedefs[i].v1].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				midHighestPoint,
				map.vertexes[map.linedefs[i].v2].y);

			vertexNormalsCount++;
			vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

			faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
			verticeCount += 3;




			/*DrawTriangle3D(
				{
					-1 * map.vertexes[map.linedefs[i].v1].x,
					highestPoint,
					map.vertexes[map.linedefs[i].v1].y
				},
						{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						midHighestPoint,
						map.vertexes[map.linedefs[i].v2].y
						},
						{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						highestPoint,
						map.vertexes[map.linedefs[i].v2].y
						},
				ColorLerp({ 255,0,0,255 }, { 0,255,0,255 }, (float)i / (float)map.linedefs.size())

			);*/


			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				highestPoint,
				map.vertexes[map.linedefs[i].v1].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				midHighestPoint,
				map.vertexes[map.linedefs[i].v2].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				highestPoint,
				map.vertexes[map.linedefs[i].v2].y);

			vertexNormalsCount++;
			vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

			faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
			verticeCount += 3;


			// ---
			/*DrawTriangle3D(
				{
					-1 * map.vertexes[map.linedefs[i].v1].x,
					midLowestPoint,
					map.vertexes[map.linedefs[i].v1].y
				},
					{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						lowestPoint,
						map.vertexes[map.linedefs[i].v1].y
					},
					{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						lowestPoint,
						map.vertexes[map.linedefs[i].v2].y
					},
				ColorLerp({ 255,0,0,255 }, { 0,0,255,255 }, (float)i / (float)map.linedefs.size())

			);*/

			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				midLowestPoint,
				map.vertexes[map.linedefs[i].v1].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				lowestPoint,
				map.vertexes[map.linedefs[i].v1].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				lowestPoint,
				map.vertexes[map.linedefs[i].v2].y);

			vertexNormalsCount++;
			vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

			faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
			verticeCount += 3;

			/*DrawTriangle3D(
				{
					-1 * map.vertexes[map.linedefs[i].v1].x,
					midLowestPoint,
					map.vertexes[map.linedefs[i].v1].y
				},
						{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						lowestPoint,
						map.vertexes[map.linedefs[i].v2].y
						},
						{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						midLowestPoint,
						map.vertexes[map.linedefs[i].v2].y
						},
				ColorLerp({ 255,0,0,255 }, { 0,255,0,255 }, (float)i / (float)map.linedefs.size())

			);*/

			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				midLowestPoint,
				map.vertexes[map.linedefs[i].v1].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				lowestPoint,
				map.vertexes[map.linedefs[i].v2].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				midLowestPoint,
				map.vertexes[map.linedefs[i].v2].y);

			vertexNormalsCount++;
			vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

			faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
			verticeCount += 3;

		}
		if (map.linedefs[i].sideback != -1) {




			if (map.sidedefs[map.linedefs[i].sideback].texturemiddle != "") {
				/*DrawTriangle3D(
					{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightceiling,
						map.vertexes[map.linedefs[i].v2].y
					},
						{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightfloor,
						map.vertexes[map.linedefs[i].v2].y
						},
						{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightfloor,
						map.vertexes[map.linedefs[i].v1].y
						},
					ColorLerp({ 255,0,0,255 }, { 0,0,255,255 }, i / map.linedefs.size())

				);*/


				vertices += TextFormat("v %f %f %f\n", // it's at this point i realized I should've probably made this a function
					-1 * map.vertexes[map.linedefs[i].v2].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightceiling,
					map.vertexes[map.linedefs[i].v2].y);
				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v2].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightfloor,
					map.vertexes[map.linedefs[i].v2].y);
				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v1].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightfloor,
					map.vertexes[map.linedefs[i].v1].y);

				vertexNormalsCount++;
				vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

				faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
				verticeCount += 3;

				/*DrawTriangle3D(
					{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightceiling,
						map.vertexes[map.linedefs[i].v2].y
					},
						{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightfloor,
						map.vertexes[map.linedefs[i].v1].y
						},
						{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightceiling,
						map.vertexes[map.linedefs[i].v1].y
						},
					ColorLerp({ 255,0,0,255 }, { 0,0,255,255 }, (float)i / (float)map.linedefs.size())

				);*/

				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v2].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightceiling,
					map.vertexes[map.linedefs[i].v2].y);
				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v1].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightfloor,
					map.vertexes[map.linedefs[i].v1].y);
				vertices += TextFormat("v %f %f %f\n",
					-1 * map.vertexes[map.linedefs[i].v1].x,
					(float)map.sectors[map.sidedefs[map.linedefs[i].sideback].sector].heightceiling,
					map.vertexes[map.linedefs[i].v1].y);

				vertexNormalsCount++;
				vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

				faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
				verticeCount += 3;

			}

			/*DrawTriangle3D(
				{
					-1 * map.vertexes[map.linedefs[i].v2].x,
					midHighestPoint,
					map.vertexes[map.linedefs[i].v2].y
				},

					{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						midHighestPoint,
						map.vertexes[map.linedefs[i].v1].y
					},
					{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						highestPoint,
						map.vertexes[map.linedefs[i].v1].y
					},
				ColorLerp({ 255,0,0,255 }, { 0,0,255,255 }, (float)i / (float)map.linedefs.size())

			);*/

			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				midHighestPoint,
				map.vertexes[map.linedefs[i].v2].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				midHighestPoint,
				map.vertexes[map.linedefs[i].v1].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				highestPoint,
				map.vertexes[map.linedefs[i].v1].y);

			vertexNormalsCount++;
			vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

			faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
			verticeCount += 3;

			/*DrawTriangle3D(
				{
					-1 * map.vertexes[map.linedefs[i].v2].x,
					highestPoint,
					map.vertexes[map.linedefs[i].v2].y
				},

					{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						midHighestPoint,
						map.vertexes[map.linedefs[i].v2].y
					},
					{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						highestPoint,
						map.vertexes[map.linedefs[i].v1].y
					},
				ColorLerp({ 255,0,0,255 }, { 0,255,0,255 }, (float)i / (float)map.linedefs.size())

			);*/

			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				highestPoint,
				map.vertexes[map.linedefs[i].v2].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				midHighestPoint,
				map.vertexes[map.linedefs[i].v2].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				highestPoint,
				map.vertexes[map.linedefs[i].v1].y);

			vertexNormalsCount++;
			vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

			faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
			verticeCount += 3;

			// ---
			/*DrawTriangle3D(
				{
					-1 * map.vertexes[map.linedefs[i].v2].x,
					lowestPoint,
					map.vertexes[map.linedefs[i].v2].y
				},
					{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						lowestPoint,
						map.vertexes[map.linedefs[i].v1].y
					},

					{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						midLowestPoint,
						map.vertexes[map.linedefs[i].v1].y
					},
				ColorLerp({ 255,0,0,255 }, { 0,0,255,255 }, (float)i / (float)map.linedefs.size())

			);*/
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				lowestPoint,
				map.vertexes[map.linedefs[i].v2].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				lowestPoint,
				map.vertexes[map.linedefs[i].v1].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				midLowestPoint,
				map.vertexes[map.linedefs[i].v1].y);

			vertexNormalsCount++;
			vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

			faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
			verticeCount += 3;

			/*DrawTriangle3D(
				{
					-1 * map.vertexes[map.linedefs[i].v2].x,
					midLowestPoint,
					map.vertexes[map.linedefs[i].v2].y
				},

					{
						-1 * map.vertexes[map.linedefs[i].v2].x,
						lowestPoint,
						map.vertexes[map.linedefs[i].v2].y
					},
					{
						-1 * map.vertexes[map.linedefs[i].v1].x,
						midLowestPoint,
						map.vertexes[map.linedefs[i].v1].y
					},
				ColorLerp({ 255,0,0,255 }, { 0,255,0,255 }, (float)i / (float)map.linedefs.size())

			);*/
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				midLowestPoint,
				map.vertexes[map.linedefs[i].v2].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v2].x,
				lowestPoint,
				map.vertexes[map.linedefs[i].v2].y);
			vertices += TextFormat("v %f %f %f\n",
				-1 * map.vertexes[map.linedefs[i].v1].x,
				midLowestPoint,
				map.vertexes[map.linedefs[i].v1].y);

			vertexNormalsCount++;
			vertexnormals += TextFormat("vn %f %f %f\n", (map.vertexes[map.linedefs[i].v2].y - map.vertexes[map.linedefs[i].v1].y), 0, -(map.vertexes[map.linedefs[i].v2].x - map.vertexes[map.linedefs[i].v1].x));

			faces += TextFormat("f %i//%i %i//%i %i//%i\n", verticeCount + 1, vertexNormalsCount, verticeCount + 2, vertexNormalsCount, verticeCount + 3, vertexNormalsCount);
			verticeCount += 3;
		}


	}

	SetRandomSeed(time(NULL));


	std::string randomNumber = std::to_string(GetRandomValue(0, INT_MAX));
	std::string appdata = ".";//  getenv("LOCALAPPDATA");
	std::filesystem::create_directory(appdata + "\\ProjectMorault");
	std::filesystem::create_directory(appdata + "\\ProjectMorault\\__udmfCache");
	std::ofstream cacheModel(appdata + "\\ProjectMorault\\__udmfCache\\" + randomNumber + ".obj");
	std::cout << "Outputting cache model to " << appdata + "\\ProjectMorault\\__udmfCache\\" + randomNumber + ".obj" << "\n";

	cacheModel << vertices;
	cacheModel << vertexnormals;
	cacheModel << faces;
	cacheModel.close();

	std::string p = appdata + "\\ProjectMorault\\__udmfCache\\" + randomNumber + ".obj";

	Model retval = LoadModel(p.c_str());

	return retval;
}

Model Morault::UDMF::LoadModelUDMFFlats(UDMFMap map)
{
	int vertexCount = 0; // number of vertices
	std::string vertices = ""; // straight wavefront data for the vertices
	int vertexNormalsCount = 0; // number of vertex normals
	std::string vertexNormals = ""; // straight wavefront data for vertexNormals
	std::string faces; // straight wavefront data for faces


	for (int sc = 0; sc < map.sectors.size(); sc++) {
		std::vector<Vertex>* verticesVector = new std::vector<Vertex>();
		for (int sd = 0; sd < map.sidedefs.size(); sd++) {
			if(map.sidedefs[sd].sector == sc){
				for (int ld = 0; ld < map.linedefs.size(); ld++) {
					
					if (map.linedefs[ld].sidefront == sd) {
						verticesVector->push_back(map.vertexes[map.linedefs[ld].v1]);
						verticesVector->push_back(map.vertexes[map.linedefs[ld].v2]);
					}
					else if (map.linedefs[ld].sideback == sd) { // you, in theory, shouldn't be able to have both the back and front of a linedef be the same.
						verticesVector->push_back(map.vertexes[map.linedefs[ld].v2]);// invert vertices if sideback
						verticesVector->push_back(map.vertexes[map.linedefs[ld].v1]);
					}
				}
			}
		}
		
		// calculating ceilings
		/*
		vertexNormalsCount++;
		vertexNormals += "vn 0 1 0\n";*/


		/*faces += "f ";
		for (int i = 0; i < verticesVector->size(); i++) {
			vertices += TextFormat("v %f %f %f\n", -1 * verticesVector->at(i).x, (float)map.sectors[sc].heightceiling, verticesVector->at(i).y);
			faces += TextFormat("%i//%i ", vertexCount + i + 1, vertexNormalsCount);
			if (i % 3 == 0 && i != 0) {
				faces.pop_back();
				faces += "\nf ";
			}
		}

		faces.pop_back();
		if (faces.at(faces.size() - 1) == 'f') {
			faces.pop_back();
		}
		else {
			faces += TextFormat(" %i//%i", vertexCount + 1, vertexNormalsCount);
		}
		faces += "\n";

		vertexCount += verticesVector->size();*/


		// calculating floors
		vertexNormalsCount++;
		vertexNormals += "vn 0 1 0\n";


		faces += "f ";
		for (int i = 0; i < verticesVector->size(); i++) {
			vertices += TextFormat("v %f %f %f\n", -1 * verticesVector->at(verticesVector->size() - 1 - i).x, (float)map.sectors[sc].heightfloor, verticesVector->at(verticesVector->size() - 1 - i).y);
			faces += TextFormat("%i//%i ", vertexCount + i + 1, vertexNormalsCount);
			if (i % 3 == 0 && i != 0) {
				faces.pop_back();
				faces += "\nf ";
			}
		}
		
		faces.pop_back();
		if (faces.at(faces.size() - 1) == 'f') {
			faces.pop_back();
		}
		else {
			faces += TextFormat(" %i//%i", vertexCount + 1, vertexNormalsCount);
		}
		faces += "\n";

		vertexCount += verticesVector->size();
		delete verticesVector;
	}

	std::string randomNumber = std::to_string(GetRandomValue(0, INT_MAX));
	std::string appdata = ".";//  getenv("LOCALAPPDATA");
	std::filesystem::create_directory(appdata + "\\ProjectMorault");
	std::filesystem::create_directory(appdata + "\\ProjectMorault\\__udmfCache");
	std::ofstream cacheModel(appdata + "\\ProjectMorault\\__udmfCache\\" + randomNumber + ".obj");
	std::cout << "Outputting cache model to " << appdata + "\\ProjectMorault\\__udmfCache\\" + randomNumber + ".obj" << "\n";

	cacheModel << vertices;
	cacheModel << vertexNormals;
	cacheModel << faces;
	cacheModel.close();

	std::string p = appdata + "\\ProjectMorault\\__udmfCache\\" + randomNumber + ".obj";

	Model retval = LoadModel(p.c_str());

	return retval;
}
