Files
c-evo-evo/ai/src/AiModuleHost.cpp
T

130 lines
5.1 KiB
C++

// This file is part of c-evo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#include "Cevo/AiModuleHost.h"
#include "Cevo/Geometry.h"
namespace Cevo {
bool AiModuleHost::handle(int command, int player, void *data)
{
switch (command) {
case cmInitModule: {
auto &init = *static_cast<TInitModuleData *>(data);
CustomAI::setServerCall(init.Server);
init.DataSize = m_rwDataSize;
return true;
}
case cmNewGame:
case cmLoadGame: {
m_game = *static_cast<TNewGameData *>(data);
InitGeometry(m_game);
for (int p = nPl - 1; p >= 0; --p) {
if (m_game.RO[p]) {
m_ai[static_cast<size_t>(p)] = m_factory(p);
m_ai[static_cast<size_t>(p)]->bind(m_game.RO[p]);
m_ai[static_cast<size_t>(p)]->setDataDefaults();
} else {
m_ai[static_cast<size_t>(p)].reset();
}
}
m_defender = -1;
return true;
}
case cmGetReady:
for (auto &a : m_ai)
if (a)
a->setDataRandom();
return true;
case cmBreakGame:
for (auto &a : m_ai)
a.reset();
return true;
case cmShowAttacking:
case cmShowCapturing: {
const auto &move = *static_cast<const TShowMove *>(data);
const int y0 = move.FromLoc / m_game.lx;
const int toLoc =
(move.FromLoc + ((move.dx + (y0 & 1) + m_game.lx + m_game.lx) >> 1)) % m_game.lx +
m_game.lx * (y0 + move.dy);
if (player >= 0 && player < nPl && m_game.RO[player] && m_game.RO[player]->Map &&
((*m_game.RO[player]->Map)[toLoc] & fOwned) != 0) {
TUnitInfo unitInfo{};
unitInfo.Loc = move.FromLoc;
unitInfo.mix = static_cast<std::uint16_t>(move.mix);
unitInfo.emix = static_cast<std::uint16_t>(move.emix);
unitInfo.Owner = static_cast<std::uint8_t>(move.Owner);
unitInfo.Health = static_cast<std::int8_t>(move.Health);
unitInfo.Fuel = static_cast<std::int8_t>(move.Fuel);
unitInfo.Job = jNone;
unitInfo.Exp = static_cast<std::uint8_t>(move.Exp);
unitInfo.Load = static_cast<std::uint8_t>(move.Load);
unitInfo.Flags = static_cast<std::uint16_t>(move.Flags);
if (m_ai[static_cast<size_t>(player)]) {
if (command == cmShowAttacking)
m_ai[static_cast<size_t>(player)]->onBeforeEnemyAttack(unitInfo, toLoc, move.EndHealth,
move.EndHealthDef);
else
m_ai[static_cast<size_t>(player)]->onBeforeEnemyCapture(unitInfo, toLoc);
}
m_defender = player;
}
return true;
}
case cmShowAfterAttack:
if (player == m_defender) {
if (m_ai[static_cast<size_t>(player)])
m_ai[static_cast<size_t>(player)]->onAfterEnemyAttack();
m_defender = -1;
}
return true;
case cmShowAfterMove:
if (player == m_defender) {
if (m_ai[static_cast<size_t>(player)])
m_ai[static_cast<size_t>(player)]->onAfterEnemyCapture();
m_defender = -1;
}
return true;
default:
break;
}
if (player < 0 || player >= nPl || !m_ai[static_cast<size_t>(player)])
return false;
if (command == cmTurn || command == cmContinue || command == cmShowNego || command == cmShowEndContact ||
(command >= scContact && command <= scDipBreak)) {
m_ai[static_cast<size_t>(player)]->process(command, data);
return true;
}
return false;
}
} // namespace Cevo