Files
c-evo-evo/game/localplayer/Term.MainScreen.Move.cpp
T

671 lines
30 KiB
C++

// TMainScreen.MoveUnit, MoveToLoc and MoveOnScreen (Term.pas): moving the
// focused unit, path finding and the unit move animation.
//
// The animation draws the moving unit into m_moveTile (a copy of the map
// rectangle it travels through), which paintEvent() puts on top of the map
// layer; the original blitted the same buffer directly onto the screen.
#include "Term.MainScreen.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "Global.h"
#include "Messg.h"
#include "Sound.h"
#include <QApplication>
#include <QElapsedTimer>
#include <QThread>
#include <cstdlib>
namespace Cevo {
namespace Term {
namespace {
constexpr int MoveTime = 300; // time for moving a unit, in ms
QString movementPhrase(int movement)
{
return movementToString(movement);
}
} // namespace
int MainScreen::moveUnit(int dx, int dy, int options)
{
using ClientTools::me;
using ClientTools::myCity;
using ClientTools::myMap;
using ClientTools::myModel;
using ClientTools::myRO;
using ClientTools::myUn;
using ClientTools::server;
const int lx = ClientTools::g.lx;
const int ly = ClientTools::g.ly;
int result = eInvalid;
const int unFocus0 = IsoEngine::unFocus;
const int fromLoc = myUn[IsoEngine::unFocus].Loc;
const int toLoc = ClientTools::dLoc(fromLoc, dx, dy);
if (toLoc < 0 || toLoc >= lx * ly)
return eInvalid;
if (myMap[toLoc] & fStealthUnit) {
soundMessage(phrases[QStringLiteral("ATTACKSTEALTH")], QString());
return result;
}
if (myMap[toLoc] & fHiddenUnit) {
soundMessage(phrases[QStringLiteral("ATTACKSUB")], QString());
return result;
}
int euix = -1;
if ((myMap[toLoc] & (fUnit | fOwned)) == static_cast<std::uint32_t>(fUnit)) { // attack -- search enemy unit
const TUn &un = myUn[IsoEngine::unFocus];
if (myModel[un.mix].Attack == 0 && !(myModel[un.mix].Cap[mcBombs] > 0 && (un.Flags & unBombsLoaded) != 0)) {
soundMessage(phrases[QStringLiteral("NOATTACKER")], QString());
return result;
}
euix = myRO->nEnemyUn - 1;
while (euix >= 0 && (*myRO->EnemyUn)[euix].Loc != toLoc)
--euix;
}
const int dirCode = ((dx & 7) << 4) + ((dy & 7) << 7);
result = server(sMoveUnit - sExecute + dirCode, me, IsoEngine::unFocus, nullptr);
if (result < rExecuted && myUn[IsoEngine::unFocus].Job > jNone)
server(sStartJob + (jNone << 4), me, IsoEngine::unFocus, nullptr);
if (result < rExecuted && result != eNoTime_Move) {
const TUn &un = myUn[IsoEngine::unFocus];
switch (result) {
case eNoTime_Load:
if (myModel[un.mix].Domain == dAir)
soundMessage(phrases[QStringLiteral("NOTIMELOADAIR")], QStringLiteral("NOMOVE_TIME"));
else
soundMessage(formatPhrase(QStringLiteral("NOTIMELOADGROUND"), { movementPhrase(myModel[un.mix].Speed) }),
QStringLiteral("NOMOVE_TIME"));
break;
case eNoTime_Bombard:
soundMessage(phrases[QStringLiteral("NOTIMEBOMBARD")], QStringLiteral("NOMOVE_TIME"));
break;
case eNoTime_Expel:
soundMessage(phrases[QStringLiteral("NOTIMEEXPEL")], QStringLiteral("NOMOVE_TIME"));
break;
case eNoRoad:
soundMessage(phrases[QStringLiteral("NOROAD")], QStringLiteral("NOMOVE_DEFAULT"));
break;
case eNoNav:
soundMessage(phrases[QStringLiteral("NONAV")], QStringLiteral("NOMOVE_DEFAULT"));
break;
case eNoCapturer:
soundMessage(phrases[QStringLiteral("NOCAPTURER")], QStringLiteral("NOMOVE_DEFAULT"));
break;
case eNoBombarder:
soundMessage(phrases[QStringLiteral("NOBOMBARDER")], QStringLiteral("NOMOVE_DEFAULT"));
break;
case eZOC:
MessgEx::contextMessage(phrases[QStringLiteral("ZOC")], QStringLiteral("NOMOVE_ZOC"),
Help::LinkCategory::Text, helpDlg().textIndex(QStringLiteral("MOVEMENT")));
break;
case eTreaty:
if ((myMap[toLoc] & (fUnit | fOwned)) != static_cast<std::uint32_t>(fUnit)) // no enemy unit -- move
soundMessage(Tribes::tribe[static_cast<std::size_t>((*myRO->Territory)[toLoc])]->tPhrase(
QStringLiteral("PEACE_NOMOVE")),
QStringLiteral("NOMOVE_TREATY"));
else
soundMessage(Tribes::tribe[static_cast<std::size_t>((*myRO->EnemyUn)[euix].Owner)]->tPhrase(
QStringLiteral("PEACE_NOATTACK")),
QStringLiteral("NOMOVE_TREATY"));
break;
case eDomainMismatch:
if (myModel[un.mix].Domain < dSea && (myMap[toLoc] & (fUnit | fOwned)) == static_cast<std::uint32_t>(fUnit | fOwned)) {
// false load attempt
bool toShip = false;
bool toTransport = false;
for (int uix = 0; uix < myRO->nUn; ++uix) {
if (myUn[uix].Loc == toLoc && myModel[myUn[uix].mix].Domain == dSea) {
toShip = true;
if (myModel[myUn[uix].mix].Cap[mcSeaTrans] > 0)
toTransport = true;
}
}
if (toTransport)
soundMessage(phrases[QStringLiteral("FULLTRANSPORT")], QStringLiteral("NOMOVE_DEFAULT"));
else if (toShip)
soundMessage(phrases[QStringLiteral("NOTRANSPORT")], QStringLiteral("NOMOVE_DEFAULT"));
else
Sound::play(QStringLiteral("NOMOVE_DOMAIN"));
} else {
Sound::play(QStringLiteral("NOMOVE_DOMAIN"));
}
break;
default:
Sound::play(QStringLiteral("NOMOVE_DEFAULT"));
break;
}
return result;
}
if ((result == eWon || result == eLost || result == eBloody) && myUn[IsoEngine::unFocus].Movement < 100
&& myModel[myUn[IsoEngine::unFocus].mix].Cap[mcWill] == 0) {
if (MessgEx::simpleQuery(MessageKind::YesNo,
formatPhrase(QStringLiteral("FASTATTACK"),
{ QString::number(myUn[IsoEngine::unFocus].Movement) }),
QStringLiteral("NOMOVE_TIME"))
!= Dpi::ModalResult::Ok)
return eInvalid;
updateNow(); // remove message box from screen
}
bool oldUnrest = false;
bool newUnrest = false;
if (result >= rExecuted && (result & rUnitRemoved) == 0
&& (myMap[toLoc] & (fUnit | fOwned)) != static_cast<std::uint32_t>(fUnit)) {
oldUnrest = ClientTools::unrestAtLoc(IsoEngine::unFocus, fromLoc);
newUnrest = ClientTools::unrestAtLoc(IsoEngine::unFocus, toLoc);
if (newUnrest > oldUnrest) {
QString queryItem;
int p1;
if (myRO->Government == gDemocracy) {
queryItem = QStringLiteral("UNREST_NOTOWN");
p1 = me;
} else {
queryItem = QStringLiteral("UNREST_FOREIGN");
p1 = (*myRO->Territory)[toLoc];
}
MessgEx::MessgExDlg &dlg = messgExDlg();
dlg.messageText = Tribes::tribe[static_cast<std::size_t>(p1)]->tPhrase(queryItem).arg(
phrases.lookup(QStringLiteral("GOVERNMENT"), myRO->Government));
dlg.kind = MessageKind::YesNo;
dlg.iconKind = MessgEx::MessageIconKind::Imp;
dlg.iconIndex = imPalace;
if (dlg.showModal() != Dpi::ModalResult::Ok)
return eInvalid;
updateNow(); // remove message box from screen
}
}
if (result >= rExecuted && myModel[myUn[IsoEngine::unFocus].mix].Domain == dAir
&& (myUn[IsoEngine::unFocus].Status & usToldNoReturn) == 0) { // can plane return?
TPlaneReturnData planeReturnData;
planeReturnData.Fuel = myUn[IsoEngine::unFocus].Fuel;
if ((myMap[toLoc] & (fUnit | fOwned)) == static_cast<std::uint32_t>(fUnit)
|| (myMap[toLoc] & (fCity | fOwned)) == static_cast<std::uint32_t>(fCity)) {
// attack/expel/bombard -> 100MP
planeReturnData.Loc = fromLoc;
planeReturnData.Movement = std::max(0, myUn[IsoEngine::unFocus].Movement - 100);
} else { // move
planeReturnData.Loc = toLoc;
planeReturnData.Movement = myUn[IsoEngine::unFocus].Movement - ((dx & 1) != 0 ? 100 : 150);
}
if (server(sGetPlaneReturn, me, IsoEngine::unFocus, &planeReturnData) == eNoWay) {
const QString queryItem = myModel[myUn[IsoEngine::unFocus].mix].Kind == mkSpecial_Glider
? QStringLiteral("LOWFUEL_GLIDER")
: QStringLiteral("LOWFUEL");
if (MessgEx::simpleQuery(MessageKind::YesNo, phrases[queryItem], QStringLiteral("WARNING_LOWSUPPORT"))
!= Dpi::ModalResult::Ok)
return eInvalid;
updateNow(); // remove message box from screen
myUn[IsoEngine::unFocus].Status |= usToldNoReturn;
}
}
int mission = -1;
if (result == eMissionDone) {
modalSelectDlg().showNewContent(WindowMode::Modal, Select::ListKind::Mission);
updateNow(); // dialog still on screen
mission = modalSelectDlg().result;
if (mission < 0)
return result;
server(sSetSpyMission + (mission << 4), me, 0, nullptr);
}
bool cityCaptured = false;
bool needEcoUpdate = false;
bool needRepaintPanel = false;
bool isAttack = false;
if (result == eNoTime_Move) {
Sound::play(QStringLiteral("NOMOVE_TIME"));
} else {
IsoEngine::destinationMarkOn = false;
paintDestination();
if ((result & rUnitRemoved) != 0)
ClientTools::cityOptimizer_BeforeRemoveUnit(IsoEngine::unFocus);
isAttack = result == eBombarded
|| (result != eMissionDone && (myMap[toLoc] & (fUnit | fOwned)) == static_cast<std::uint32_t>(fUnit));
if (!isAttack) { // move
int cix = myRO->nCity - 1; // look for own city at dest location
while (cix >= 0 && myCity[cix].Loc != toLoc)
--cix;
if (result != eMissionDone && (myMap[toLoc] & fCity) != 0 && cix < 0)
cityCaptured = true;
result = server(sMoveUnit + dirCode, me, IsoEngine::unFocus, nullptr);
if (result == eHiddenUnit) {
Sound::play(QStringLiteral("NOMOVE_SUBMARINE"));
paintLoc(toLoc);
} else if (result == eStealthUnit) {
Sound::play(QStringLiteral("NOMOVE_STEALTH"));
paintLoc(toLoc);
} else if (result == eZOC_EnemySpotted) {
Sound::play(QStringLiteral("NOMOVE_ZOC"));
paintLoc(toLoc, 1);
} else if (result >= rExecuted) {
if ((result & rUnitRemoved) != 0) {
IsoEngine::unFocus = -1; // unit died
} else {
Q_ASSERT(IsoEngine::unFocus >= 0);
TUn &un = myUn[IsoEngine::unFocus];
un.Status &= ~(IsoEngine::usStay | IsoEngine::usRecover);
for (int uix = 0; uix < myRO->nUn; ++uix) {
if (myUn[uix].Master == IsoEngine::unFocus)
myUn[uix].Status &= ~usWaiting;
}
const bool democratic =
myRO->Government == gRepublic || myRO->Government == gDemocracy || myRO->Government == gFuture;
if (cityCaptured && democratic) {
// borders have moved, unrest might have changed in any city
ClientTools::cityOptimizer_BeginOfTurn();
needEcoUpdate = true;
} else {
if (oldUnrest != newUnrest) {
ClientTools::cityOptimizer_CityChange(un.Home);
for (int uix = 0; uix < myRO->nUn; ++uix) {
if (myUn[uix].Master == IsoEngine::unFocus)
ClientTools::cityOptimizer_CityChange(myUn[uix].Home);
}
needEcoUpdate = true;
}
if (myRO->Government == gDespotism && myModel[un.mix].Kind == mkSpecial_TownGuard) {
const auto reoptimizeCityAt = [&](int loc) {
int cixChanged = myRO->nCity - 1;
while (cixChanged >= 0 && myCity[cixChanged].Loc != loc)
--cixChanged;
Q_ASSERT(cixChanged >= 0);
if (cixChanged >= 0) {
ClientTools::cityOptimizer_CityChange(cixChanged);
needEcoUpdate = true;
}
};
// town guard moved out of / into a city in despotism -- reoptimize!
if ((myMap[fromLoc] & fCity) != 0)
reoptimizeCityAt(fromLoc);
if ((myMap[toLoc] & fCity) != 0 && !cityCaptured)
reoptimizeCityAt(toLoc);
}
}
}
} else {
Q_ASSERT(false);
}
setTroopLoc(toLoc);
} else { // enemy unit -- attack
int defender;
if (result == eBombarded)
defender = (*myRO->Territory)[toLoc];
else
defender = (*myRO->EnemyUn)[euix].Owner;
if ((options & muNoSuicideCheck) == 0 && (result & rUnitRemoved) != 0 && result != eMissionDone) {
// suicide query
Battle::BattleDlg &battle = battleDlg();
const TUn &un = myUn[IsoEngine::unFocus];
battle.forecast.pAtt = me;
battle.forecast.mixAtt = un.mix;
battle.forecast.HealthAtt = un.Health;
battle.forecast.ExpAtt = un.Exp;
battle.forecast.FlagsAtt = static_cast<std::int32_t>(un.Flags);
battle.forecast.Movement = un.Movement;
server(sGetBattleForecastEx, me, toLoc, &battle.forecast);
battle.uix = IsoEngine::unFocus;
battle.toLoc = toLoc;
battle.isSuicideQuery = true;
if (battle.showModal() != Dpi::ModalResult::Ok)
return result;
}
int cixChanged = -1;
if ((result & rUnitRemoved) != 0 && myRO->Government == gDespotism
&& myModel[myUn[IsoEngine::unFocus].mix].Kind == mkSpecial_TownGuard && (myMap[fromLoc] & fCity) != 0) {
// town guard died in city in despotism -- reoptimize!
cixChanged = myRO->nCity - 1;
while (cixChanged >= 0 && myCity[cixChanged].Loc != fromLoc)
--cixChanged;
Q_ASSERT(cixChanged >= 0);
}
for (int i = 0; i < myRO->nEnemyModel; ++i)
lostArmy[static_cast<std::size_t>(i)] = (*myRO->EnemyModel)[i].Lost;
const std::uint32_t oldToTile = myMap[toLoc];
result = server(sMoveUnit + dirCode, me, IsoEngine::unFocus, nullptr);
nLostArmy = 0;
for (int i = 0; i < myRO->nEnemyModel; ++i) {
lostArmy[static_cast<std::size_t>(i)] = (*myRO->EnemyModel)[i].Lost - lostArmy[static_cast<std::size_t>(i)];
nLostArmy += lostArmy[static_cast<std::size_t>(i)];
}
if ((result & rUnitRemoved) != 0) {
IsoEngine::unFocus = -1;
setTroopLoc(fromLoc);
}
const bool democratic =
myRO->Government == gRepublic || myRO->Government == gDemocracy || myRO->Government == gFuture;
if ((oldToTile & ~myMap[toLoc] & fCity) != 0 && democratic) {
// city was destroyed, borders have moved, unrest might have changed in any city
ClientTools::cityOptimizer_BeginOfTurn();
needEcoUpdate = true;
} else {
if (cixChanged >= 0) {
ClientTools::cityOptimizer_CityChange(cixChanged);
needEcoUpdate = true;
}
if (result == eWon || result == eBloody || result == eExpelled) {
ClientTools::cityOptimizer_TileBecomesAvailable(toLoc);
needEcoUpdate = true;
}
}
if (nLostArmy > 1) {
MessgEx::MessgExDlg &dlg = messgExDlg();
dlg.kind = MessageKind::Ok;
dlg.iconKind = MessgEx::MessageIconKind::EnemyArmy;
dlg.messageText = Tribes::tribe[static_cast<std::size_t>(defender)]->tString(
phraseToQtFormat(phrases.lookup(QStringLiteral("ARMYLOST"),
(*myRO->EnemyModel)[(*myRO->EnemyUn)[euix].emix].Domain)));
dlg.showModal();
}
}
if ((result & rUnitRemoved) != 0) {
ClientTools::cityOptimizer_AfterRemoveUnit();
listDlg().removeUnit();
needEcoUpdate = true;
}
if (needEcoUpdate) {
updateViews(true);
updateNow();
}
}
if (result == eMissionDone) {
const int p1 = (*myRO->Territory)[toLoc];
switch (mission) {
case smStealMap:
m_mapValid = false;
paintAllMaps();
break;
case smStealCivilReport:
MessgEx::tribeMessage(p1, Tribes::tribe[static_cast<std::size_t>(p1)]->tPhrase(QStringLiteral("DOSSIER_PREPARED")),
QString());
break;
case smStealMilReport:
listDlg().showNewContentMilReport(WindowMode::Persistent, p1);
break;
default:
break;
}
}
if (IsoEngine::unFocus >= 0)
checkToldNoReturn(IsoEngine::unFocus);
if (result >= rExecuted) {
if (cityCaptured && (myMap[toLoc] & fCity) == 0) { // city destroyed
for (int i = 0; i < nWonder; ++i) { // tell about destroyed wonders
if (myRO->Wonder[i].CityID == WonderDestroyed && myData->toldWonders[static_cast<std::size_t>(i)].CityID != WonderDestroyed) {
MessgEx::MessgExDlg &dlg = messgExDlg();
if (m_wondersDlg && m_wondersDlg->isVisible())
m_wondersDlg->smartUpdateContent(false);
dlg.openSound = QStringLiteral("WONDER_DESTROYED");
dlg.messageText = formatPhrase(QStringLiteral("WONDERDEST"),
{ phrases.lookup(QStringLiteral("IMPROVEMENTS"), i) });
dlg.kind = MessageKind::OkHelp;
dlg.helpKind = Help::LinkCategory::Imp;
dlg.helpNo = i;
dlg.iconKind = MessgEx::MessageIconKind::Imp;
dlg.iconIndex = i;
dlg.showModal();
std::memcpy(&myData->toldWonders[static_cast<std::size_t>(i)], &myRO->Wonder[i], sizeof(myRO->Wonder[i]));
}
}
}
if (cityCaptured && (myMap[toLoc] & fCity) != 0) { // city captured
listDlg().addCity();
for (int i = 0; i < nWonder; ++i) { // tell about capture of wonders
if (myCity[myRO->nCity - 1].Built[i] > 0) {
MessgEx::MessgExDlg &dlg = messgExDlg();
if (m_wondersDlg && m_wondersDlg->isVisible())
m_wondersDlg->smartUpdateContent(false);
dlg.openSound = QStringLiteral("WONDER_CAPTURED");
dlg.messageText = Tribes::tribe[static_cast<std::size_t>(me)]->tPhrase(QStringLiteral("WONDERCAPTOWN"))
.arg(phrases.lookup(QStringLiteral("IMPROVEMENTS"), i));
dlg.kind = MessageKind::OkHelp;
dlg.helpKind = Help::LinkCategory::Imp;
dlg.helpNo = i;
dlg.iconKind = MessgEx::MessageIconKind::Imp;
dlg.iconIndex = i;
dlg.showModal();
}
}
if ((myRO->Happened & phStealTech) != 0) { // Temple of Zeus -- choose advance to steal
modalSelectDlg().showNewContent(WindowMode::Modal, Select::ListKind::StealTech);
server(sStealTech, me, modalSelectDlg().result, nullptr);
}
tellNewModels();
int cix = myRO->nCity - 1;
while (cix >= 0 && myCity[cix].Loc != toLoc)
--cix;
Q_ASSERT(cix >= 0);
myCity[cix].Status = (myCity[cix].Status & ~csResourceWeightsMask) | (3 << 4);
// captured city, set to maximum growth
int newTiles = 1 << 13; // exploit central tile only
server(sSetCityTiles, me, cix, &newTiles);
} else {
needRepaintPanel = true;
}
}
tellNewContacts();
if (IsoEngine::unFocus >= 0 && myUn[IsoEngine::unFocus].Master >= 0) {
TUn &master = myUn[myUn[IsoEngine::unFocus].Master];
if ((master.Status & IsoEngine::usStay) != 0) {
master.Status &= ~IsoEngine::usStay;
if (master.Movement >= 100 && (master.Status & (IsoEngine::usRecover | IsoEngine::usGoto)) == 0)
master.Status |= usWaiting;
}
}
if ((options & (muAutoNoWait | muAutoNext)) != 0) {
if (IsoEngine::unFocus >= 0
&& (result == eNoTime_Move || ClientTools::unitExhausted(IsoEngine::unFocus)
|| myUn[IsoEngine::unFocus].Master >= 0
|| (myModel[myUn[IsoEngine::unFocus].mix].Domain == dAir
&& ((myMap[myUn[IsoEngine::unFocus].Loc] & fCity) != 0
|| (myMap[myUn[IsoEngine::unFocus].Loc] & fTerImp) == static_cast<std::uint32_t>(tiBase))))) {
// aircrafts stop in cities
myUn[IsoEngine::unFocus].Status &= ~usWaiting;
if ((options & muAutoNext) != 0) {
if (cityCaptured && (myMap[toLoc] & fCity) != 0) {
IsoEngine::unFocus = -1;
paintLoc(toLoc); // don't show unit in city if not selected
} else {
nextUnit(unStartLoc, true);
}
}
} else if (IsoEngine::unFocus < 0 && (options & muAutoNext) != 0) {
nextUnit(unStartLoc, result != eMissionDone);
}
}
if (needRepaintPanel && IsoEngine::unFocus == unFocus0) {
if (isAttack) {
panelPaint();
} else {
Q_ASSERT(result != eMissionDone);
checkTerrainBtnVisible();
focusOnLoc(toLoc, flRepaintPanel | flImmUpdate);
}
}
if (result >= rExecuted && cityCaptured && (myMap[toLoc] & fCity) != 0)
zoomToCity(toLoc, IsoEngine::unFocus < 0, chCaptured); // show captured city
return result;
}
void MainScreen::moveToLoc(int loc, bool checkSuicide)
{
using ClientTools::myMap;
using ClientTools::myRO;
using ClientTools::myUn;
enum class StopReason { None, Arrived, Dead, NoTime, EnemySpotted, MoveError };
if (myUn[IsoEngine::unFocus].Job > jNone)
ClientTools::server(sStartJob + (jNone << 4), ClientTools::me, IsoEngine::unFocus, nullptr);
TMoveAdviceData moveAdviceData;
if (ClientTools::getMoveAdvice(IsoEngine::unFocus, loc, moveAdviceData) < rExecuted)
return;
const int uix = IsoEngine::unFocus;
StopReason stopReason = StopReason::None;
for (;;) {
for (int i = 0; i < moveAdviceData.nStep; ++i) {
int moveOptions = i == moveAdviceData.nStep - 1 ? muAutoNext : 0;
const int nextLoc = ClientTools::dLoc(myUn[uix].Loc, moveAdviceData.dx[i], moveAdviceData.dy[i]);
if (nextLoc == loc || (loc == maNextCity && (myMap[nextLoc] & fCity) != 0))
stopReason = StopReason::Arrived;
if (!checkSuicide && nextLoc == loc)
moveOptions |= muNoSuicideCheck;
const int moveResult = moveUnit(moveAdviceData.dx[i], moveAdviceData.dy[i], moveOptions);
if (moveResult < rExecuted)
stopReason = StopReason::MoveError;
else if ((moveResult & rUnitRemoved) != 0)
stopReason = StopReason::Dead;
else if (stopReason == StopReason::None && (moveResult & rEnemySpotted) != 0)
stopReason = StopReason::EnemySpotted;
if (stopReason != StopReason::None)
break;
}
if (stopReason == StopReason::None && (moveAdviceData.nStep < 25 || myRO->Wonder[woShinkansen].EffectiveOwner != ClientTools::me))
stopReason = StopReason::NoTime;
if (stopReason != StopReason::None)
break;
if (ClientTools::getMoveAdvice(IsoEngine::unFocus, loc, moveAdviceData) < rExecuted) {
Q_ASSERT(false);
break;
}
}
switch (stopReason) {
case StopReason::None:
Q_ASSERT(false);
break;
case StopReason::Arrived:
myUn[uix].Status &= 0xFFFF - IsoEngine::usGoto;
break;
case StopReason::Dead:
if (IsoEngine::unFocus < 0)
nextUnit(unStartLoc, false);
break;
default: // multi-turn goto
myUn[uix].Status = (myUn[uix].Status & (0xFFFF - IsoEngine::usStay - IsoEngine::usRecover)) | IsoEngine::usGoto
| ((loc == maNextCity ? 0x7FFF : loc) << 16);
paintLoc(myUn[uix].Loc);
if (stopReason == StopReason::NoTime && IsoEngine::unFocus == uix) {
myUn[uix].Status &= ~usWaiting;
nextUnit(unStartLoc, true);
}
break;
}
}
void MainScreen::moveOnScreen(const TShowMove &showMove, int step0, int step1, int nStep, bool restore)
{
const int lx = ClientTools::g.lx;
const int xxt = m_mainMap->xxt;
const int yyt = m_mainMap->yyt;
m_timer->stop();
TUnitInfo unitInfo{};
unitInfo.Owner = static_cast<std::uint8_t>(showMove.Owner);
unitInfo.mix = static_cast<std::int16_t>(showMove.mix);
unitInfo.Health = static_cast<std::int8_t>(showMove.Health);
unitInfo.Job = jNone;
unitInfo.Flags = static_cast<std::uint16_t>(showMove.Flags);
if (showMove.Owner != ClientTools::me)
unitInfo.emix = static_cast<std::int16_t>(showMove.emix);
const int toLoc = ClientTools::dLoc(showMove.FromLoc, showMove.dx, showMove.dy);
int xToLoc = toLoc % lx;
const int yToLoc = toLoc / lx;
const int xFromLoc = showMove.FromLoc % lx;
const int yFromLoc = showMove.FromLoc / lx;
if (xToLoc > xFromLoc + 2)
xToLoc -= lx;
else if (xToLoc < xFromLoc - 2)
xToLoc += lx;
int xw1 = m_xw + lx;
// ((xFromLoc-xw1)*2+yFromLoc and 1+1)*xxt+dx*xxt/2-MapWidth/2 -> min
while (std::abs(((xFromLoc - xw1 + lx) * 2 + (yFromLoc & 1) + 1) * xxt * 2 + showMove.dx * xxt - m_mapWidth)
< std::abs(((xFromLoc - xw1) * 2 + (yFromLoc & 1) + 1) * xxt * 2 + showMove.dx * xxt - m_mapWidth))
xw1 -= lx;
const int xTo = (xToLoc - xw1) * (xxt * 2) + (yToLoc & 1) * xxt + (xxt - IsoEngine::xxu);
const int yTo = (yToLoc - m_yw) * yyt + (yyt - IsoEngine::yyuAnchor);
const int xFrom = (xFromLoc - xw1) * (xxt * 2) + (yFromLoc & 1) * xxt + (xxt - IsoEngine::xxu);
const int yFrom = (yFromLoc - m_yw) * yyt + (yyt - IsoEngine::yyuAnchor);
const int xMin = std::min(xFrom, xTo);
const int yMin = std::min(yFrom, yTo);
const int xRange = std::abs(xTo - xFrom) + xxt * 2;
const int yRange = std::abs(yTo - yFrom) + yyt * 3;
mainOffscreenPaint();
if (m_moveTile.width() != xRange || m_moveTile.height() != yRange)
m_moveTile.setSize(xRange, yRange);
m_noMap->setOutput(&m_moveTile);
m_noMap->setPaintBounds(0, 0, xRange, yRange);
m_moveTilePos = QPoint(xMin, yMin);
const QRect area(xMin, yMin, xRange, yRange);
const int steps = std::abs(step1 - step0);
QElapsedTimer clock;
for (int step = 0; step <= steps; ++step) {
// the map rectangle the unit moves through, then the unit on top
m_moveTile.image() = m_offscreen.image().copy(area).convertToFormat(QImage::Format_ARGB32);
m_moveTile.canvas().setDevice(&m_moveTile.image());
int xMoving = xFrom;
int yMoving = yFrom;
if (step1 != step0) {
const int pos = step0 + step * (step1 - step0) / std::abs(step1 - step0);
xMoving = xFrom + static_cast<int>(std::lround(pos * static_cast<double>(xTo - xFrom) / nStep));
yMoving = yFrom + static_cast<int>(std::lround(pos * static_cast<double>(yTo - yFrom) / nStep));
}
m_noMap->paintUnit(xMoving - xMin, yMoving - yMin, unitInfo, 0);
m_moveTile.canvas().end();
m_moveTileValid = true;
invalidateRect(m_mapOffset + xMin, TopBarHeight + yMin, m_mapOffset + xMin + xRange, TopBarHeight + yMin + yRange);
updateNow();
// wait for MoveTime/12 ms per step, keeping the application responsive
clock.start();
const qint64 stepTime = MoveTime / 12;
do {
if (!m_idle || gameMode == cMovie)
processEventsWithDisabledKeyboard();
QThread::msleep(1);
} while (clock.elapsed() < stepTime);
}
if (restore) {
m_moveTileValid = false;
invalidateRect(m_mapOffset + xMin, TopBarHeight + yMin, m_mapOffset + xMin + xRange, TopBarHeight + yMin + yRange);
updateNow();
}
m_blinkTime = -1;
m_timer->start();
}
void MainScreen::processEventsWithDisabledKeyboard()
{
m_keyboardDisabled = true;
QCoreApplication::processEvents();
m_keyboardDisabled = false;
}
} // namespace Term
} // namespace Cevo