Revert "feat: 自动战斗关卡名忽略大小写,顺便格式化一下泰迪的代码"

This reverts commit b858435739.
This commit is contained in:
MistEO
2022-09-04 16:34:44 +08:00
parent ef6d23b397
commit 2e4730a236

View File

@@ -1,13 +1,13 @@
#pragma once
#include <string>
#include <vector>
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <meojson/json.hpp>
#include <opencv2/core.hpp>
#include <meojson/json.hpp>
namespace Map
{
@@ -27,10 +27,9 @@ namespace Map
Tile get_item(int y, int x) const { return tiles[y][x]; }
std::vector<cv::Point3d> view;
std::string stageId;
std::string code;
std::string levelId;
std::string name;
std::string code;
std::string levelId;
std::string name;
private:
int height = 0;
int width = 0;
@@ -40,9 +39,7 @@ namespace Map
{
public:
TileCalc(int width, int height, const std::string& dir);
bool run(const std::string& code_or_name, bool side, std::vector<std::vector<cv::Point2d>>& out_pos,
std::vector<std::vector<Tile>>& out_tiles) const;
bool run(const std::string& code_or_name, bool side, std::vector<std::vector<cv::Point2d>>& out_pos, std::vector<std::vector<Tile>>& out_tiles) const;
private:
int width = 0;
int height = 0;
@@ -54,7 +51,7 @@ namespace Map
bool adapter(double& x, double& y) const;
};
inline void InitMat4x4(cv::Mat& m, double (*num)[4])
inline void InitMat4x4(cv::Mat& m, double(*num)[4])
{
for (int i = 0; i < m.rows; i++)
for (int j = 0; j < m.cols; j++)
@@ -81,8 +78,10 @@ namespace Map
std::vector<Tile> tmp;
tmp.reserve(Level::width);
for (const json::value& tile : row.as_array()) {
tmp.emplace_back(Tile { tile.at("heightType").as_integer(), tile.at("buildableType").as_integer(),
tile.get("tileKey", std::string()) });
tmp.emplace_back(Tile{
tile.at("heightType").as_integer(),
tile.at("buildableType").as_integer(),
tile.get("tileKey", std::string()) });
}
tiles.emplace_back(std::move(tmp));
}
@@ -93,20 +92,26 @@ namespace Map
TileCalc::width = width;
TileCalc::height = height;
double ratio = static_cast<double>(height) / width;
double matrixP[4][4] { { ratio / tan(20 * degree), 0, 0, 0 },
{ 0, 1 / tan(20 * degree), 0, 0 },
{ 0, 0, -(1000 + 0.3) / (1000 - 0.3), -(1000 * 0.3 * 2) / (1000 - 0.3) },
{ 0, 0, -1, 0 } };
double matrixP[4][4]{
{ ratio / tan(20 * degree), 0, 0, 0},
{ 0, 1 / tan(20 * degree), 0, 0},
{ 0, 0, -(1000 + 0.3) / (1000 - 0.3), -(1000 * 0.3 * 2) / (1000 - 0.3)},
{ 0, 0, -1, 0 }
};
InitMat4x4(TileCalc::MatrixP, matrixP);
double matrixX[4][4] { { 1, 0, 0, 0 },
{ 0, cos(30 * degree), -sin(30 * degree), 0 },
{ 0, -sin(30 * degree), -cos(30 * degree), 0 },
{ 0, 0, 0, 1 } };
double matrixX[4][4]{
{ 1, 0, 0, 0},
{ 0, cos(30 * degree), -sin(30 * degree), 0},
{ 0, -sin(30 * degree), -cos(30 * degree), 0},
{ 0, 0, 0, 1}
};
InitMat4x4(TileCalc::MatrixX, matrixX);
double matrixY[4][4] { { cos(10 * degree), 0, sin(10 * degree), 0 },
{ 0, 1, 0, 0 },
{ -sin(10 * degree), 0, cos(10 * degree), 0 },
{ 0, 0, 0, 1 } };
double matrixY[4][4]{
{ cos(10 * degree), 0, sin(10 * degree), 0},
{ 0, 1, 0, 0},
{ -sin(10 * degree), 0, cos(10 * degree), 0},
{ 0, 0, 0, 1}
};
InitMat4x4(TileCalc::MatrixY, matrixY);
std::ifstream ifs(dir, std::ios::in);
if (!ifs.is_open()) {
@@ -143,27 +148,23 @@ namespace Map
return true;
}
inline bool TileCalc::run(const std::string& any_id, bool side, std::vector<std::vector<cv::Point2d>>& out_pos,
std::vector<std::vector<Tile>>& out_tiles) const
inline bool TileCalc::run(const std::string& code_or_name, bool side, std::vector<std::vector<cv::Point2d>>& out_pos, std::vector<std::vector<Tile>>& out_tiles) const
{
auto make_lower = [](const std::string& str) -> std::string {
std::string result;
std::transform(str.cbegin(), str.cend(), result.begin(),
[](char c) { return static_cast<char>(std::tolower(c)); });
return result;
};
std::string any_id_lower = make_lower(any_id);
for (const Map::Level& level : TileCalc::levels) {
if (make_lower(level.code) != any_id_lower && make_lower(level.name) != any_id_lower &&
make_lower(level.levelId) != any_id_lower && make_lower(level.stageId) != any_id_lower) {
if (level.code != code_or_name &&
level.name != code_or_name &&
level.levelId != code_or_name &&
level.stageId != code_or_name) {
continue;
}
auto [x, y, z] = level.view[side ? 1 : 0];
double adapter_y = 0, adapter_z = 0;
TileCalc::adapter(adapter_y, adapter_z);
double matrix[4][4] {
{ 1, 0, 0, -x }, { 0, 1, 0, -y - adapter_y }, { 0, 0, 1, -z - adapter_z }, { 0, 0, 0, 1 }
double matrix[4][4]{
{ 1, 0, 0, -x},
{ 0, 1, 0, -y - adapter_y},
{ 0, 0, 1, -z - adapter_z},
{ 0, 0, 0, 1}
};
auto raw = cv::Mat(cv::Size(4, 4), CV_64F);
auto Finall_Matrix = cv::Mat(cv::Size(4, 4), CV_64F);
@@ -189,8 +190,7 @@ namespace Map
cv::Mat view_point = Finall_Matrix * map_point;
view_point = view_point / view_point.at<double>(3, 0);
view_point = (view_point + 1) / 2;
tmp_pos[j] = cv::Point2d(view_point.at<double>(0, 0) * TileCalc::width,
(1 - view_point.at<double>(1, 0)) * TileCalc::height);
tmp_pos[j] = cv::Point2d(view_point.at<double>(0, 0) * TileCalc::width, (1 - view_point.at<double>(1, 0)) * TileCalc::height);
}
out_pos.emplace_back(tmp_pos);
out_tiles.emplace_back(tmp_tiles);
@@ -199,4 +199,4 @@ namespace Map
}
return false;
}
} // namespace Map
}