card rotation

This commit is contained in:
Asuro
2025-03-14 20:10:04 +01:00
parent de88190c47
commit f14add70b6
10 changed files with 258 additions and 50 deletions

View File

@@ -1,8 +1,10 @@
#include "Global.h"
#include "Instance.h"
#include "Log.h"
#include "Puzzle.h"
#include "bx/string.h"
#include "bx/bx.h"
#include "bx/string.h"
#include "imgui.h"
#include <cassert>
#include <cstdio>
@@ -19,8 +21,11 @@ namespace
Generated::StaticPuzzleData StaticData;
} // namespace
namespace Generated
namespace Puzzle
{
constexpr float UIPuzBoxSize = 26;
using namespace Generated;
void Setup()
{
LOG("Setting up static puzzle data");
@@ -54,10 +59,10 @@ namespace Generated
}
ser.Finish();
}
const StaticPuzzleCard& GetCard(const StaticPuzzleData& data, StaticPuzzleCardHandle H)
const StaticPuzzleCard& GetCard(StaticPuzzleCardHandle H)
{
assert(IsValid(H));
return data.Cards[H.Idx];
return GetStaticPuzzleData().Cards[H.Idx];
}
bool IsValid(StaticPuzzleCardHandle h)
@@ -65,6 +70,11 @@ namespace Generated
return h.Idx != UINT16_MAX;
}
const char* GetShortNodeName(const PuzzleElementType::Enum node)
{
return PuzzleElementType::ShortName[node];
}
uint8_t GetRemainingCount(const PuzzleCardStack& stack)
{
assert(stack.MaxAvailableCount >= stack.UsedCount);
@@ -84,30 +94,63 @@ namespace Generated
if (offsetX >= 0 && offsetX < Puzzle::Config::CardSize && offsetY >= 0 &&
offsetY < Puzzle::Config::CardSize)
{
PuzzleElementType::Enum cardVal =
GetCardNodeAt(GetCard(GetStaticPuzzleData(), card.RefCard), offsetX, offsetY);
PuzzleElementType::Enum cardVal = GetCardNodeAt(GetCard(card.RefCard), card.Rotation, offsetX, offsetY);
if (cardVal != PuzzleElementType::None) return cardVal;
}
}
return puz.BackgroundTiles[pos.Y * Puzzle::Config::MaxPuzzleSizeCards + pos.X];
}
PuzzleElementType::Enum GetCardNodeAt(const StaticPuzzleCard& card, int8_t x, int8_t y)
PuzzleElementType::Enum GetCardNodeAt(const StaticPuzzleCard& card, uint8_t rotation, int8_t x, int8_t y)
{
assert(x >= 0 && x < Puzzle::Config::CardSize);
assert(y >= 0 && y < Puzzle::Config::CardSize);
// TODO: account for card rotation
return card.Elements[y * Puzzle::Config::CardSize + x];
uint8_t arrX = x;
uint8_t arrY = y;
if (rotation == 1)
{
arrX = y;
arrY = Config::CardSize - x - 1;
}
else if (rotation == 2)
{
arrX = Config::CardSize - x - 1;
arrY = Config::CardSize - y - 1;
}
else if (rotation == 3)
{
arrX = Config::CardSize - y - 1;
arrY = x;
}
return card.Elements[arrY * Puzzle::Config::CardSize + arrX];
}
PuzzleElementType::Enum& EditCardNodeAt(StaticPuzzleCard& card, int8_t x, int8_t y)
PuzzleElementType::Enum& EditCardNodeAt(StaticPuzzleCard& card, uint8_t rotation, int8_t x, int8_t y)
{
assert(x >= 0 && x < Puzzle::Config::CardSize);
assert(y >= 0 && y < Puzzle::Config::CardSize);
// TODO: account for card rotation
return card.Elements[y * Puzzle::Config::CardSize + x];
uint8_t arrX = x;
uint8_t arrY = y;
if (rotation == 1)
{
arrX = y;
arrY = Config::CardSize - x - 1;
}
else if (rotation == 2)
{
arrX = Config::CardSize - x - 1;
arrY = Config::CardSize - y - 1;
}
else if (rotation == 3)
{
arrX = Config::CardSize - y - 1;
arrY = x;
}
return card.Elements[arrY * Puzzle::Config::CardSize + arrX];
}
bool PuzzleSolver::IsPuzzleSolved(const PuzzleData& puzzle)
@@ -182,23 +225,41 @@ namespace Generated
return (sourceType == PuzzleElementType::WaterIn && goalType == PuzzleElementType::WaterGoal) ||
(sourceType == PuzzleElementType::ElectricIn && goalType == PuzzleElementType::ElectricGoal);
}
} // namespace Generated
namespace Generated
{
constexpr float UIPuzBoxSize = 26;
void DrawCard(const StaticPuzzleCard& card, uint8_t rotation, ImVec2 pos)
{
auto& drawList = *ImGui::GetWindowDrawList();
for (int32_t y = 0; y < Puzzle::Config::CardSize; ++y)
{
for (int32_t x = 0; x < Puzzle::Config::CardSize; ++x)
{
ImGui::SetCursorScreenPos({pos.x + x * UIPuzBoxSize, pos.y + y * UIPuzBoxSize});
ImGui::Text("%s", GetShortNodeName(GetCardNodeAt(card, rotation, x, y)));
}
}
for (int32_t y = 0; y <= Puzzle::Config::CardSize; ++y)
{
ImVec2 linePos = {pos.x, pos.y + y * UIPuzBoxSize};
drawList.AddLine(linePos,
{linePos.x + Puzzle::Config::CardSize * UIPuzBoxSize, linePos.y},
y % Puzzle::Config::CardSize == 0 ? 0xFFFFFFFF : 0x11FFFFFF);
}
for (int32_t x = 0; x <= Puzzle::Config::CardSize; ++x)
{
ImVec2 linePos = {pos.x + x * UIPuzBoxSize, pos.y};
drawList.AddLine(linePos,
{linePos.x, linePos.y + Puzzle::Config::CardSize * UIPuzBoxSize},
x % Puzzle::Config::CardSize == 0 ? 0xFFFFFFFF : 0x11FFFFFF);
}
ImGui::SetCursorScreenPos(pos);
ImGui::InvisibleButton("cardbn",
{Puzzle::Config::CardSize * UIPuzBoxSize, Puzzle::Config::CardSize * UIPuzBoxSize});
}
void WritePuzzleFilePath(char* buf, int32_t bufSize, uint16_t puzID)
{
bx::snprintf(buf, bufSize, "%s/%u.pzl", Puzzle::PuzzleFileDir, puzID);
}
const char* GetShortNodeName(const PuzzleElementType::Enum node)
{
return PuzzleElementType::ShortName[node];
}
bool RenderDebugUI(PuzzleData& obj)
{
bool dataChanged = false;
@@ -215,6 +276,11 @@ namespace Generated
ImGui::End();
return false;
}
ImGui::SameLine();
if (ImGui::Button("Reload"))
{
// TODO
}
int32_t val = obj.ID;
if (ImGui::InputInt("ID", &val))
{
@@ -268,6 +334,7 @@ namespace Generated
{
uint32_t CardIdx = *reinterpret_cast<uint32_t*>(payload->Data);
obj.PlacedCards[obj.PlacedCardCount].RefCard = {(uint16_t)CardIdx};
obj.PlacedCardCount++;
}
ImGui::EndDragDropTarget();
}
@@ -297,8 +364,55 @@ namespace Generated
ImGui::SetCursorScreenPos(pos);
ImGui::NewLine();
if (ImGui::TreeNodeEx("Available Cards", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
bool bAvailOpen =
ImGui::TreeNodeEx("Available Cards", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed);
if (ImGui::BeginDragDropTarget())
{
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("cardtype", 0))
{
uint32_t CardIdx = *reinterpret_cast<uint32_t*>(payload->Data);
bool found = false;
for (int32_t i = 0; i < obj.AvailableCardCount; ++i)
{
if (obj.AvailableCards[i].RefCard.Idx == CardIdx)
{
obj.AvailableCards[i].MaxAvailableCount++;
found = true;
break;
}
}
if (!found)
{
if (obj.AvailableCardCount == BX_COUNTOF(obj.AvailableCards))
{
LOG_ERROR("Available card limit reached!");
}
else
{
auto& Card = obj.AvailableCards[obj.AvailableCardCount];
Card.RefCard.Idx = CardIdx;
Card.MaxAvailableCount = 1;
obj.AvailableCardCount++;
}
}
dataChanged = true;
}
ImGui::EndDragDropTarget();
}
if (bAvailOpen)
{
uint8_t debugRot = Game::GetInstance().DebugData.DebugCardRotation;
for (uint32_t i = 0; i < obj.AvailableCardCount; ++i)
{
auto& card = obj.AvailableCards[i];
DrawCard(GetCard(card.RefCard), debugRot, ImGui::GetCursorScreenPos());
if (ImGui::BeginDragDropSource())
{
ImGui::SetDragDropPayload("availcard", &i, sizeof(i));
DrawCard(GetCard(card.RefCard), debugRot, ImGui::GetCursorScreenPos());
ImGui::EndDragDropSource();
}
}
ImGui::TreePop();
}
}
@@ -323,7 +437,10 @@ namespace Generated
return isVisible;
}
} // namespace Puzzle
namespace Generated
{
PuzPos operator+=(PuzPos lhs, const PuzPos& rhs)
{
lhs.X += rhs.X;