Files
Tereis/src/source/TetrisLogic.cpp
T
2026-04-26 09:55:21 +08:00

947 lines
29 KiB
C++

#include "stdafx.h"
#include "Tetris.h"
#include "TetrisLogicInternal.h"
int nType = 0;
int type = 0;
int state = 0;
int tScore = 0;
bool gameOverFlag = false;
bool suspendFlag = false;
bool targetFlag = false;
int workRegion[20][10] = { 0 };
Point point = { 0, 0 };
Point target = { 0, 0 };
MenuState menuState = { 0, 2 };
PlayerStats classicStats = { 0, 1, 0, 0, 0 };
PlayerStats rogueStats = { 0, 1, 0, 30, 0, 100, 100, 0 };
UpgradeUiState upgradeUiState = { 0, 0, 0, 0, {} };
FeedbackState feedbackState = { 0, _T(""), _T("") };
int currentScreen = SCREEN_MENU;
int currentMode = MODE_CLASSIC;
int currentFallInterval = 500;
int nextTypes[3] = { 0, 0, 0 };
int holdType = -1;
bool holdUsedThisTurn = false;
bool currentPieceIsExplosive = false;
bool currentPieceIsLaser = false;
bool currentPieceIsCross = false;
bool currentPieceIsRainbow = false;
Point pendingChainBombCenter = { 0, 0 };
bool pendingChainBombFollowup = false;
int bricks[7][4][4][4] =
{
{
{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
{{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}},
{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
{{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}}
},
{
{{0, 0, 0, 0}, {0, 0, 2, 0}, {0, 2, 2, 2}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 0, 2, 0}, {0, 0, 2, 2}, {0, 0, 2, 0}},
{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 2, 2, 2}, {0, 0, 2, 0}},
{{0, 0, 0, 0}, {0, 0, 2, 0}, {0, 2, 2, 0}, {0, 0, 2, 0}}
},
{
{{0, 3, 0, 0}, {0, 3, 0, 0}, {0, 3, 3, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {3, 3, 3, 0}, {3, 0, 0, 0}, {0, 0, 0, 0}},
{{3, 3, 0, 0}, {0, 3, 0, 0}, {0, 3, 0, 0}, {0, 0, 0, 0}},
{{0, 0, 3, 0}, {3, 3, 3, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
},
{
{{0, 0, 4, 0}, {0, 0, 4, 0}, {0, 4, 4, 0}, {0, 0, 0, 0}},
{{0, 4, 0, 0}, {0, 4, 4, 4}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 0, 4, 4}, {0, 0, 4, 0}, {0, 0, 4, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 4, 4, 4}, {0, 0, 0, 4}, {0, 0, 0, 0}}
},
{
{{0, 0, 0, 0}, {0, 5, 5, 0}, {0, 5, 5, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 5, 5, 0}, {0, 5, 5, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 5, 5, 0}, {0, 5, 5, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 5, 5, 0}, {0, 5, 5, 0}, {0, 0, 0, 0}}
},
{
{{0, 6, 0, 0}, {0, 6, 6, 0}, {0, 0, 6, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 6, 6, 0}, {6, 6, 0, 0}, {0, 0, 0, 0}},
{{6, 0, 0, 0}, {6, 6, 0, 0}, {0, 6, 0, 0}, {0, 0, 0, 0}},
{{0, 6, 6, 0}, {6, 6, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
},
{
{{0, 0, 7, 0}, {0, 7, 7, 0}, {0, 7, 0, 0}, {0, 0, 0, 0}},
{{0, 7, 7, 0}, {0, 0, 7, 7}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 7}, {0, 0, 7, 7}, {0, 0, 7, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 7, 7, 0}, {0, 0, 7, 7}, {0, 0, 0, 0}}
}
};
COLORREF BrickColor[7] =
{
RGB(244, 144, 165),
RGB(255, 181, 197),
RGB(170, 215, 255),
RGB(134, 230, 220),
RGB(255, 187, 143),
RGB(255, 223, 146),
RGB(197, 170, 255)
};
/**
* @brief 计算指定方块在指定旋转状态下的最小包围盒边界。
*
* 该函数会遍历 4x4 形状矩阵,找出所有非空单元的上下左右边界,
* 供后续统一计算生成位置和对齐方式时使用。
*
* @param brickType 方块类型编号。
* @param brickState 方块旋转状态编号。
* @param minRow 返回最上方非空行号。
* @param maxRow 返回最下方非空行号。
* @param minCol 返回最左侧非空列号。
* @param maxCol 返回最右侧非空列号。
*/
static void GetBrickBounds(int brickType, int brickState, int& minRow, int& maxRow, int& minCol, int& maxCol)
{
minRow = 4;
maxRow = -1;
minCol = 4;
maxCol = -1;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (bricks[brickType][brickState][i][j] != 0)
{
if (i < minRow)
{
minRow = i;
}
if (i > maxRow)
{
maxRow = i;
}
if (j < minCol)
{
minCol = j;
}
if (j > maxCol)
{
maxCol = j;
}
}
}
}
}
/**
* @brief 计算指定方块的统一生成位置。
*
* 该函数会根据方块在初始旋转状态下的最小包围盒,
* 自动把方块水平居中到游戏区附近,并将顶部非空行对齐到可视区域顶部。
* 这样不同形状的方块在生成时看起来会更加统一。
*
* @param brickType 方块类型编号。
* @return Point 计算得到的生成坐标。
*/
Point GetSpawnPoint(int brickType)
{
int minRow, maxRow, minCol, maxCol;
GetBrickBounds(brickType, 0, minRow, maxRow, minCol, maxCol);
int brickWidth = maxCol - minCol + 1;
int brickHeight = maxRow - minRow + 1;
Point spawnPoint;
spawnPoint.x = (nGameWidth - brickWidth) / 2 - minCol;
spawnPoint.y = -brickHeight;
return spawnPoint;
}
void ResetPlayerStats(PlayerStats& stats, bool useRogueRules)
{
stats.score = 0;
stats.level = 1;
stats.exp = 0;
stats.requiredExp = useRogueRules ? 30 : 0;
stats.totalLinesCleared = 0;
stats.scoreMultiplierPercent = 100;
stats.expMultiplierPercent = 100;
stats.slowFallStacks = 0;
stats.comboBonusStacks = 0;
stats.comboChain = 0;
stats.previewCount = 1;
stats.lastChanceCount = 0;
stats.scoreUpgradeLevel = 0;
stats.expUpgradeLevel = 0;
stats.previewUpgradeLevel = 0;
stats.lastChanceUpgradeLevel = 0;
stats.holdUnlocked = 0;
stats.pressureReliefLevel = 0;
stats.sweeperLevel = 0;
stats.sweeperCharge = 0;
stats.explosiveLevel = 0;
stats.explosivePieceCounter = 0;
stats.chainBlastLevel = 0;
stats.chainBombLevel = 0;
stats.laserLevel = 0;
stats.thunderTetrisLevel = 0;
stats.thunderLaserLevel = 0;
stats.feverLevel = 0;
stats.rageStackLevel = 0;
stats.infiniteFeverLevel = 0;
stats.feverLineCharge = 0;
stats.feverTicks = 0;
stats.screenBombLevel = 0;
stats.screenBombCharge = 0;
stats.screenBombCount = 0;
stats.terminalClearLevel = 0;
stats.dualChoiceLevel = 0;
stats.destinyWheelLevel = 0;
stats.perfectRotateLevel = 0;
stats.timeDilationLevel = 0;
stats.timeDilationTicks = 0;
stats.highPressureLevel = 0;
stats.tetrisGambleLevel = 0;
stats.extremePlayerLevel = 0;
stats.extremeSlowTicks = 0;
stats.extremeDangerTicks = 30;
stats.extremeDangerLevel = 0;
stats.upgradeShockwaveLevel = 0;
stats.evolutionImpactLevel = 0;
stats.controlMasterLevel = 0;
stats.holdSlowTicks = 0;
stats.blockStormLevel = 0;
stats.blockStormPiecesRemaining = 0;
stats.blackHoleLevel = 0;
stats.blackHoleCharges = 0;
stats.reshapeLevel = 0;
stats.reshapeCharges = 0;
stats.rainbowPieceLevel = 0;
stats.voidCoreLevel = 0;
stats.pendingRainbowPieceCount = 0;
stats.stableStructureLevel = 0;
stats.doubleGrowthLevel = 0;
stats.gamblerLevel = 0;
stats.difficultyElapsedMs = 0;
stats.difficultyLevel = 0;
stats.lockedRows = 0;
for (int i = 0; i < 7; i++)
{
stats.pieceTuningLevels[i] = 0;
}
}
void SetFeedbackMessage(const TCHAR* title, const TCHAR* detail, int ticks)
{
feedbackState.visibleTicks = ticks;
lstrcpyn(feedbackState.title, title, sizeof(feedbackState.title) / sizeof(TCHAR));
lstrcpyn(feedbackState.detail, detail, sizeof(feedbackState.detail) / sizeof(TCHAR));
}
bool IsPiecePlacementValid(int pieceType, int pieceState, Point position)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (bricks[pieceType][pieceState][i][j] == 0)
{
continue;
}
int checkY = position.y + i;
int checkX = position.x + j;
if (checkX < 0 || checkX >= nGameWidth || checkY >= GetRoguePlayableHeight())
{
return false;
}
if (checkY >= 0 && workRegion[checkY][checkX] != 0)
{
return false;
}
}
}
return true;
}
/**
* @brief 判断当前方块是否可以继续向下移动。
*
* 遍历当前处于活动状态下方块的 4x4 矩阵,计算其下落一步(Y 坐标加 1)后的位置,
* 并检查每个非空方块单元:
* 1. 是否超出了游戏工作区的底部边界(对应数组索引 >= 20)。
* 2. 是否与工作区下方已经固定的其他方块发生碰撞(即对应位置的值不为 0)。
* 如果遇到以上任意一种情况,则认为方块受到阻挡,无法继续下落。
*
* @return bool 如果可以继续安全下落返回 true,否则返回 false。
*/
bool CanMoveDown()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (bricks[type][state][i][j] != 0)
{
int nextY = point.y + i + 1;
int nextX = point.x + j;
// 检查是否到达底部边界
if (nextY >= GetRoguePlayableHeight())
{
return false;
}
// 检查下方是否有其他固定方块
if (nextY >= 0 && workRegion[nextY][nextX] != 0)
{
return false;
}
}
}
}
return true;
}
/**
* @brief 判断当前方块是否可以继续向左移动。
*
* 遍历当前处于活动状态下方块的 4x4 矩阵,计算其向左移动一步(X 坐标减 1)后的位置,
* 并检查每个非空方块单元:
* 1. 是否超出了游戏工作区的左侧边界(对应数组索引 < 0)。
* 2. 是否与工作区左侧已经固定的其他方块发生碰撞(即对应位置的值不为 0)。
* 如果遇到以上任意一种情况,则认为方块受到阻挡,无法继续左移。
*
* @return bool 如果可以继续安全左移返回 true,否则返回 false。
*/
bool CanMoveLeft()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (bricks[type][state][i][j] != 0)
{
int nextY = point.y + i;
int nextX = point.x + j - 1;
// 检查是否到达左侧边界
if (nextX < 0)
{
return false;
}
// 检查左侧是否有其他固定方块
if (nextY >= 0 && workRegion[nextY][nextX] != 0)
{
return false;
}
}
}
}
return true;
}
/**
* @brief 判断当前方块是否可以继续向右移动。
*
* 遍历当前处于活动状态下方块的 4x4 矩阵,计算其向右移动一步(X 坐标加 1)后的位置,
* 并检查每个非空方块单元:
* 1. 是否超出了游戏工作区的右侧边界(对应数组索引 >= 10)。
* 2. 是否与工作区右侧已经固定的其他方块发生碰撞(即对应位置的值不为 0)。
* 如果遇到以上任意一种情况,则认为方块受到阻挡,无法继续右移。
*
* @return bool 如果可以继续安全右移返回 true,否则返回 false。
*/
bool CanMoveRight()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (bricks[type][state][i][j] != 0)
{
int nextY = point.y + i;
int nextX = point.x + j + 1;
// 检查是否到达右侧边界
if (nextX >= nGameWidth)
{
return false;
}
// 检查右侧是否有其他固定方块
if (nextY >= 0 && workRegion[nextY][nextX] != 0)
{
return false;
}
}
}
}
return true;
}
static bool TryRotateWithOffset(int nextState, int offsetX)
{
Point rotatedPoint = point;
rotatedPoint.x += offsetX;
return IsPiecePlacementValid(type, nextState, rotatedPoint);
}
/**
* @brief 将当前活动方块向下移动一格。
*
* 该函数只负责修改当前活动方块的纵坐标,将其在工作区中的位置向下推进 1 格。
* 是否允许下移由外部配合 CanMoveDown 函数提前判断。
*/
void MoveDown()
{
// 当前方块下移一格
point.y++;
}
/**
* @brief 将当前活动方块向左移动一格。
*
* 该函数只负责修改当前活动方块的横坐标,将其在工作区中的位置向左推进 1 格。
* 是否允许左移由外部配合 CanMoveLeft 函数提前判断。
*/
void MoveLeft()
{
// 当前方块左移一格
point.x--;
}
/**
* @brief 将当前活动方块向右移动一格。
*
* 该函数只负责修改当前活动方块的横坐标,将其在工作区中的位置向右推进 1 格。
* 是否允许右移由外部配合 CanMoveRight 函数提前判断。
*/
void MoveRight()
{
// 当前方块右移一格
point.x++;
}
/**
* @brief 旋转当前活动方块到下一种朝向。
*
* 游戏中的每种方块都预置了 4 种旋转状态,该函数会先尝试切换到下一状态,
* 然后检查旋转后的方块是否越界或与固定方块重叠。
* 如果旋转后的状态非法,则恢复到旋转前的状态。
*/
void Rotate()
{
int nextState = (state + 1) % 4;
if (IsPiecePlacementValid(type, nextState, point))
{
state = nextState;
return;
}
if (currentMode == MODE_ROGUE && rogueStats.perfectRotateLevel > 0)
{
if (TryRotateWithOffset(nextState, -1))
{
state = nextState;
point.x--;
return;
}
if (TryRotateWithOffset(nextState, 1))
{
state = nextState;
point.x++;
return;
}
}
}
/**
* @brief 让当前活动方块快速下落到当前位置能够到达的最低点。
*
* 该函数会持续检查当前方块是否还能继续下移,只要可以下移就重复调用 MoveDown,
* 直到方块到达底部或被其他固定方块阻挡为止。
*/
void DropDown()
{
// 只要还能继续下落,就不断下移
while (CanMoveDown())
{
MoveDown();
}
}
/**
* @brief 将当前活动方块固定到工作区,并生成下一个活动方块。
*
* 遍历当前方块 4x4 形状矩阵,把其中所有非空单元写入工作区数组,
* 表示该方块已经落地并转为固定状态。
* 如果固定时仍有任意非空单元位于可视区域顶部之外,则判定游戏结束。
* 此时当前方块在可视区域内的部分仍会保留在工作区中。
* 若未超出顶部,再将“下一方块”切换为新的当前方块,重置旋转状态,
* 并把新方块生成到工作区上方的初始位置,同时刷新预测落点。
*/
void Fixing()
{
bool overflowTop = false;
Point explosiveCells[4] = {};
int explosiveCellCount = 0;
int rainbowFilledCount = 0;
pendingChainBombFollowup = false;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (bricks[type][state][i][j] != 0)
{
int fixY = point.y + i;
int fixX = point.x + j;
// 只要当前方块任意非空单元仍超出顶部,就标记为结束
if (fixY < 0)
{
overflowTop = true;
}
// 将当前方块在可视区域内的部分写入工作区
if (fixY >= 0 && fixY < GetRoguePlayableHeight() && fixX >= 0 && fixX < nGameWidth)
{
workRegion[fixY][fixX] = currentPieceIsRainbow ? 8 : bricks[type][state][i][j];
if (currentPieceIsExplosive && explosiveCellCount < 4)
{
explosiveCells[explosiveCellCount].x = fixX;
explosiveCells[explosiveCellCount].y = fixY;
explosiveCellCount++;
}
}
}
}
}
if (!overflowTop && currentPieceIsRainbow)
{
rainbowFilledCount = TriggerRainbowRowCompletion(point.y, point.y + 3);
if (rainbowFilledCount > 0)
{
TCHAR rainbowDetail[128];
_stprintf_s(rainbowDetail, _T("\u8865\u5168 %d \u4e2a\u884c\u5185\u7f3a\u53e3\uff0c\u66f4\u5bb9\u6613\u8fbe\u6210\u6d88\u884c\u3002"), rainbowFilledCount);
SetFeedbackMessage(_T("\u5f69\u8679\u65b9\u5757\u751f\u6548"), rainbowDetail, 10);
}
}
if (overflowTop)
{
if (currentMode == MODE_ROGUE && rogueStats.terminalClearLevel > 0 && rogueStats.lastChanceCount > 0 && rogueStats.screenBombCount > 0)
{
rogueStats.lastChanceCount--;
rogueStats.screenBombCount--;
int clearedByTerminal = TriggerScreenBomb();
rogueStats.feverTicks = 10;
currentFallInterval = GetRogueFallInterval();
TCHAR terminalDetail[128];
_stprintf_s(
terminalDetail,
_T("\u7ec8\u672b\u6e05\u573a\u751f\u6548\uff0c\u6e05\u9664 %d \u683c\uff0c\u5e76\u8fdb\u5165 10 \u79d2\u72c2\u70ed\u3002"),
clearedByTerminal);
SetFeedbackMessage(_T("\u7ec8\u672b\u6e05\u573a\u89e6\u53d1"), terminalDetail, 14);
}
else if (currentMode == MODE_ROGUE && rogueStats.lastChanceCount > 0)
{
rogueStats.lastChanceCount--;
for (int i = 0; i < 3; i++)
{
DeleteOneLine(GetRoguePlayableHeight() - 1);
}
SetFeedbackMessage(
_T("\u6700\u540e\u4e00\u640f\u89e6\u53d1"),
_T("\u81ea\u52a8\u6e05\u9664\u5e95\u90e8 3 \u884c\uff0c\u672c\u5c40\u7ee7\u7eed\u3002"),
14);
}
else
{
gameOverFlag = true;
return;
}
}
if (currentPieceIsExplosive)
{
int explosiveScoreGain = 0;
for (int i = 0; i < explosiveCellCount; i++)
{
explosiveScoreGain += ClearExplosiveAreaAt(explosiveCells[i].y, explosiveCells[i].x);
}
if (currentMode == MODE_ROGUE && explosiveScoreGain > 0)
{
explosiveScoreGain = explosiveScoreGain * rogueStats.scoreMultiplierPercent / 100;
rogueStats.score += explosiveScoreGain;
tScore = rogueStats.score;
}
TCHAR explosiveDetail[128];
_stprintf_s(
explosiveDetail,
_T("\u6e05\u9664 %d \u4e2a\u683c\u5b50 +%d Score"),
explosiveScoreGain > 0 ? explosiveScoreGain * 100 / rogueStats.scoreMultiplierPercent : 0,
explosiveScoreGain);
SetFeedbackMessage(_T("\u7206\u7834\u65b9\u5757\u89e6\u53d1"), explosiveDetail, 12);
if (rogueStats.chainBombLevel > 0 && explosiveCellCount > 0)
{
pendingChainBombCenter = explosiveCells[0];
pendingChainBombFollowup = true;
}
}
if (currentPieceIsLaser)
{
int laserColumn = point.x + 1;
if (laserColumn < 0)
{
laserColumn = 0;
}
if (laserColumn >= nGameWidth)
{
laserColumn = nGameWidth - 1;
}
int laserCellsCleared = ClearColumnAt(laserColumn);
if (currentMode == MODE_ROGUE && laserCellsCleared > 0)
{
int laserScore = laserCellsCleared * rogueStats.scoreMultiplierPercent / 100;
if (laserScore < laserCellsCleared)
{
laserScore = laserCellsCleared;
}
rogueStats.score += laserScore;
tScore = rogueStats.score;
TCHAR laserDetail[128];
_stprintf_s(laserDetail, _T("\u6e05\u9664 %d \u683c +%d Score"), laserCellsCleared, laserScore);
SetFeedbackMessage(_T("\u6fc0\u5149\u65b9\u5757\u89e6\u53d1"), laserDetail, 12);
}
}
if (currentPieceIsCross)
{
int crossRow = point.y + 1;
int crossColumn = point.x + 1;
if (crossRow < 0)
{
crossRow = 0;
}
if (crossRow >= GetRoguePlayableHeight())
{
crossRow = GetRoguePlayableHeight() - 1;
}
if (crossColumn < 0)
{
crossColumn = 0;
}
if (crossColumn >= nGameWidth)
{
crossColumn = nGameWidth - 1;
}
int crossCellsCleared = ClearRowAt(crossRow);
int columnCellsCleared = ClearColumnAt(crossColumn);
if (workRegion[crossRow][crossColumn] == 0 && columnCellsCleared > 0)
{
// center cell may already be counted by row clear
}
int totalCrossCleared = crossCellsCleared + columnCellsCleared;
if (currentMode == MODE_ROGUE && totalCrossCleared > 0)
{
int crossScore = totalCrossCleared * rogueStats.scoreMultiplierPercent / 100;
if (crossScore < totalCrossCleared)
{
crossScore = totalCrossCleared;
}
rogueStats.score += crossScore;
tScore = rogueStats.score;
TCHAR crossDetail[128];
_stprintf_s(crossDetail, _T("\u6e05\u9664 %d \u683c +%d Score"), totalCrossCleared, crossScore);
SetFeedbackMessage(_T("\u5341\u5b57\u65b9\u5757\u89e6\u53d1"), crossDetail, 12);
}
}
if (TryStabilizeBoard() > 0)
{
SetFeedbackMessage(_T("\u7a33\u5b9a\u7ed3\u6784\u751f\u6548"), _T("\u81ea\u52a8\u586b\u8865\u4e86\u4e00\u4e2a\u90bb\u8fd1\u7a7a\u6d1e\u3002"), 10);
}
if (currentMode == MODE_ROGUE)
{
currentFallInterval = GetRogueFallInterval();
}
// 生成下一个活动方块
type = ConsumeNextType();
nType = nextTypes[0];
state = 0;
holdUsedThisTurn = false;
RollCurrentPieceSpecialFlags(true);
point = GetSpawnPoint(type);
target = point;
ComputeTarget();
}
/**
* @brief 删除指定行,并让其上方所有行整体下移一格。
*
* 该函数会先将目标行上方的所有数据逐行向下复制,
* 再把最顶端一行清空,从而完成一次标准的消行下移操作。
*
* @param number 需要被删除的目标行号。
*/
void DeleteOneLine(int number)
{
for (int i = number; i > 0; i--)
{
for (int j = 0; j < nGameWidth; j++)
{
workRegion[i][j] = workRegion[i - 1][j];
}
}
// 清空最顶端一行
for (int j = 0; j < nGameWidth; j++)
{
workRegion[0][j] = 0;
}
}
/**
* @brief 检查并删除所有已满的行,同时更新当前得分。
*
* 该函数会从底部向上遍历工作区,判断每一行是否被完全填满。
* 如果某一行全部非 0,则调用 DeleteOneLine 删除该行,
* 并将该行上方的内容整体下移。为了避免连续满行被漏检,
* 删除后会继续检查当前行号。每成功消除 1 行,当前得分增加 100 分。
*/
int DeleteLines()
{
int clearedLines = 0;
bool clearedWithRainbow = false;
int playableHeight = GetRoguePlayableHeight();
for (int i = playableHeight - 1; i >= 0; i--)
{
bool fullLine = true;
for (int j = 0; j < nGameWidth; j++)
{
if (workRegion[i][j] == 0)
{
fullLine = false;
break;
}
}
if (fullLine)
{
for (int j = 0; j < nGameWidth; j++)
{
if (IsRainbowBoardCell(workRegion[i][j]))
{
clearedWithRainbow = true;
break;
}
}
DeleteOneLine(i);
clearedLines++;
i++;
}
}
ApplyLineClearResult(clearedLines);
if (pendingChainBombFollowup && clearedLines > 0)
{
pendingChainBombFollowup = false;
int followupCleared = 0;
int centerY = pendingChainBombCenter.y;
int centerX = pendingChainBombCenter.x;
for (int y = centerY - 1; y <= centerY + 1; y++)
{
for (int x = centerX - 1; x <= centerX + 1; x++)
{
if (y >= 0 && y < GetRoguePlayableHeight() && x >= 0 && x < nGameWidth && workRegion[y][x] != 0)
{
workRegion[y][x] = 0;
followupCleared++;
}
}
}
if (currentMode == MODE_ROGUE && followupCleared > 0)
{
int followupScore = followupCleared * rogueStats.scoreMultiplierPercent / 100;
if (followupScore < followupCleared)
{
followupScore = followupCleared;
}
rogueStats.score += followupScore;
tScore = rogueStats.score;
TCHAR followupDetail[128];
_stprintf_s(
followupDetail,
_T("\u8ffd\u52a0\u5c0f\u7206\u70b8\u6e05\u9664 %d \u683c +%d Score"),
followupCleared,
followupScore);
SetFeedbackMessage(_T("\u8fde\u73af\u70b8\u5f39\u89e6\u53d1"), followupDetail, 12);
}
}
else
{
pendingChainBombFollowup = false;
}
if (currentMode == MODE_ROGUE && clearedWithRainbow && rogueStats.voidCoreLevel > 0)
{
int miniBlackHoleCleared = TriggerMiniBlackHole(5);
if (miniBlackHoleCleared > 0)
{
int miniScore = miniBlackHoleCleared * rogueStats.scoreMultiplierPercent / 100;
if (miniScore < miniBlackHoleCleared)
{
miniScore = miniBlackHoleCleared;
}
rogueStats.score += miniScore;
tScore = rogueStats.score;
TCHAR miniDetail[128];
_stprintf_s(miniDetail, _T("\u5f69\u8679\u6d88\u884c\u89e6\u53d1\u5c0f\u578b\u9ed1\u6d1e\uff0c\u6e05\u9664 %d \u683c +%d Score"), miniBlackHoleCleared, miniScore);
SetFeedbackMessage(_T("\u865a\u7a7a\u6838\u5fc3\u89e6\u53d1"), miniDetail, 12);
}
}
return clearedLines;
}
/**
* @brief 计算当前活动方块的预测落点位置。
*
* 该函数以当前活动方块的位置为起点,使用虚拟下落的方式不断尝试向下移动,
* 直到方块无法继续下落为止。最终得到的最低可达位置会写入 target,
* 供后续界面绘制瞄准器或落点提示时使用。
*
* 计算过程中不会改变当前方块的真实位置 point。
*/
void ComputeTarget()
{
Point originalPoint = point;
// 从当前方块位置开始向下试探
target = point;
while (CanMoveDown())
{
point.y++;
target = point;
}
// 恢复当前方块的真实位置
point = originalPoint;
}
/**
* @brief 重置整个游戏状态,开始新的一局。
*
* 该函数会清空工作区中的所有固定方块数据,重置分数、结束标记和暂停标记,
* 并重新初始化当前方块、下一方块、旋转状态以及生成位置。
* 最后会重新计算一次当前方块的预测落点。
*/
void Restart()
{
for (int i = 0; i < nGameHeight; i++)
{
for (int j = 0; j < nGameWidth; j++)
{
workRegion[i][j] = 0;
}
}
gameOverFlag = false;
suspendFlag = false;
targetFlag = true;
currentFallInterval = 500;
ResetPlayerStats(classicStats, false);
ResetPlayerStats(rogueStats, true);
upgradeUiState.selectedIndex = 0;
upgradeUiState.optionCount = 0;
upgradeUiState.pendingCount = 0;
upgradeUiState.totalChosenCount = 0;
upgradeUiState.picksRemaining = 0;
feedbackState.visibleTicks = 0;
feedbackState.title[0] = _T('\0');
feedbackState.detail[0] = _T('\0');
holdType = -1;
holdUsedThisTurn = false;
RollCurrentPieceSpecialFlags(false);
tScore = 0;
ResetNextQueue();
type = ConsumeNextType();
nType = nextTypes[0];
state = 0;
holdUsedThisTurn = false;
RollCurrentPieceSpecialFlags(true);
point = GetSpawnPoint(type);
target = point;
ComputeTarget();
}
void StartGameWithMode(int mode)
{
currentMode = mode;
currentScreen = SCREEN_PLAYING;
Restart();
currentFallInterval = (currentMode == MODE_ROGUE) ? GetRogueFallInterval() : 500;
tScore = (currentMode == MODE_CLASSIC) ? classicStats.score : rogueStats.score;
}
void ReturnToMainMenu()
{
currentScreen = SCREEN_MENU;
suspendFlag = false;
gameOverFlag = false;
menuState.optionCount = 3;
upgradeUiState.pendingCount = 0;
upgradeUiState.picksRemaining = 0;
if (menuState.selectedIndex < 0 || menuState.selectedIndex >= menuState.optionCount)
{
menuState.selectedIndex = 0;
}
}
void OpenRulesScreen()
{
currentScreen = SCREEN_RULES;
suspendFlag = false;
}