Skip to content

Commit

Permalink
heavy ai preparation work
Browse files Browse the repository at this point in the history
  • Loading branch information
Robosturm committed Oct 8, 2023
1 parent 6944b3d commit c560bfa
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 3rd_party/opennn
Submodule opennn updated 2 files
+0 −23 README.md
+0 −5 eigen/README.md
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "-Wa,-mbig-obj ${CMAKE_CXX_FLAGS}")
set(CMAKE_C_FLAGS "-Wa,-mbig-obj ${CMAKE_C_FLAGS}")
add_definitions(-DWIN32_LEAN_AND_MEAN
)
endif()
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
if ("${ANDROID_ABI}" STREQUAL armeabi-v7a)
Expand Down Expand Up @@ -727,6 +729,7 @@ set(${PROJECT_NAME}_SRCS
ai/proxyai.cpp ai/proxyai.h
ai/normalai.cpp ai/normalai.h
ai/heavyai/heavyai.cpp ai/heavyai/heavyai.h
ai/heavyai/situationevaluator.h ai/heavyai/situationevaluator.cpp
ai/influencefrontmap.cpp ai/influencefrontmap.h
ai/dummyai.h ai/dummyai.cpp
ai/aiprocesspipe.h ai/aiprocesspipe.cpp
Expand Down
34 changes: 34 additions & 0 deletions ai/heavyai/situationevaluator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "ai/heavyai/situationevaluator.h"

#include "coreengine/globalutils.h"
#include "game/gamemap.h"

SituationEvaluator::SituationEvaluator()
: m_inputVector(1, UNIT_COUNT * UNIT_COUNT * static_cast<qint32>(Features::Max)),
m_searchRange(GlobalUtils::getSpCircle(0, SEARCH_RANGE))
{
}

void SituationEvaluator::updateInputVector(GameMap* pMap, const QPoint & searchPoint)
{
std::vector<Unit*> units;
getUnitsInRange(units, pMap, searchPoint);

}

void SituationEvaluator::getUnitsInRange(std::vector<Unit*> & units, GameMap* pMap, const QPoint & searchPoint)
{
for (const auto & point : m_searchRange->getVector())
{
QPoint mapPoint = point + searchPoint;
Terrain* pTerrain = pMap->getTerrain(mapPoint);
if (pTerrain != nullptr)
{
Unit* pUnit = pTerrain->getUnit();
if (pUnit != nullptr)
{
units.push_back(pUnit);
}
}
}
}
43 changes: 43 additions & 0 deletions ai/heavyai/situationevaluator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <QObject>

#include "3rd_party/opennn/opennn/opennn.h"

class Unit;
class GameMap;
class QmlVectorPoint;
using spQmlVectorPoint = std::shared_ptr<QmlVectorPoint>;

class SituationEvaluator : public QObject
{
Q_OBJECT
public:
static constexpr qint32 UNIT_COUNT = 40;
static constexpr qint32 SEARCH_RANGE = 30;
enum class Features
{
HP,
HpDamage,
FundsDamage,
MovementPoints,
Distance,
HasMoved,
Defense,
RepairsOnPosition,
Max,
};


explicit SituationEvaluator();

void updateInputVector(GameMap* pMap, const QPoint & searchPoint);

private:
void getUnitsInRange(std::vector<Unit*> & units, GameMap* pMap, const QPoint & searchPoint);

private:
opennn::Tensor<opennn::type, 2> m_inputVector;
spQmlVectorPoint m_searchRange;
};

3 changes: 2 additions & 1 deletion coreengine/workerthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,14 @@ void WorkerThread::start()
MovementPlannerAddInManager::getInstance()->loadAll();
pLoadingScreen->setProgress(tr("Loading Ui scripts..."), Mainapp::SCRIPT_PROCESS + 26);
UiManager::getInstance()->loadAll();

Userdata::getInstance()->changeUser();
pLoadingScreen->setProgress(tr("Loading Achievements..."), Mainapp::SCRIPT_PROCESS + 28);
// achievements should be loaded last
AchievementManager* pAchievementManager = AchievementManager::getInstance();
pAchievementManager->loadAll();
Player::getNeutralTableAnim();
pUnitspritemanager->createBaseDamageTable();

const QString obj = "Global";
const QString func = "finalizeLoading";
pInterpreter->doFunction(obj, func);
Expand Down
30 changes: 30 additions & 0 deletions game/gamemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,18 @@ Terrain* GameMap::getTerrain(qint32 x, qint32 y) const
}
}

Terrain* GameMap::getTerrain(QPoint pos) const
{
if (onMap(pos.x(), pos.y()))
{
return m_fields[pos.y()][pos.x()].get();
}
else
{
return nullptr;
}
}

QString GameMap::getMapTagsText()
{
QString ret = tr("Tags\n\n");
Expand Down Expand Up @@ -1152,6 +1164,24 @@ bool GameMap::onMap(qint32 x, qint32 y) const
}
}

bool GameMap::onMap(const QPoint & point) const
{

qint32 heigth = getMapHeight();
qint32 width = getMapWidth();
if ((point.x() >= 0) &&
(point.y() >= 0) &&
(point.x() < width) &&
(point.y() < heigth))
{
return true;
}
else
{
return false;
}
}

void GameMap::centerMap(qint32 x, qint32 y, bool updateMinimapPosition)
{
if (onMap(x, y))
Expand Down
12 changes: 12 additions & 0 deletions game/gamemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,12 @@ class GameMap final : public QObject, public FileSerializable, public oxygine::A
* @return true if it's still on the map
*/
Q_INVOKABLE bool onMap(qint32 x, qint32 y) const;
/**
* @brief onMap returns if the given coordinates are on the map
* @param point
* @return true if it's still on the map
*/
Q_INVOKABLE bool onMap(const QPoint & point) const;
/**
* @brief centerMap centers the view point to the given location
* @param x
Expand Down Expand Up @@ -681,6 +687,12 @@ class GameMap final : public QObject, public FileSerializable, public oxygine::A
* @return the real pointer to the given terrain
*/
Q_INVOKABLE Terrain* getTerrain(qint32 x, qint32 y) const;
/**
* @brief getTerrain only use this for js scripts
* @param pos
* @return the real pointer to the given terrain
*/
Q_INVOKABLE Terrain* getTerrain(QPoint pos) const;
/**
* @brief canBePlaced
* @param terrainID the terrain id you want to place
Expand Down
2 changes: 2 additions & 0 deletions game/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Unit::Unit(const QString & unitID, Player* pOwner, bool aquireId, GameMap* pMap)
setupJsThis(this);
if (!m_UnitID.isEmpty())
{
m_unitIdx = UnitSpriteManager::getInstance()->getIndex(m_UnitID);
initUnit();
setFuel(m_maxFuel);
setAmmo1(m_maxAmmo1);
Expand Down Expand Up @@ -4096,6 +4097,7 @@ void Unit::deserializer(QDataStream& pStream, bool fast)
pStream >> dummy2;
}
}
m_unitIdx = UnitSpriteManager::getInstance()->getIndex(m_UnitID);
}

void Unit::showRanges()
Expand Down
10 changes: 10 additions & 0 deletions game/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,15 @@ class Unit final : public Tooltip, public FileSerializable, public JsThis
* @return
*/
Q_INVOKABLE static QString getUnitRangName(qint32 rang);
/**
* @brief getUnitIdx
* @return
*/
Q_INVOKABLE qint32 getUnitIdx() const
{
return m_unitIdx;
}

protected:
/**
* @brief updateIconTweens creates the visibility toogle tweens for all icons
Expand Down Expand Up @@ -1121,6 +1130,7 @@ class Unit final : public Tooltip, public FileSerializable, public JsThis
*/
qint32 getCoBonus(QPoint position, const QString & function, qint32(Player::*pBonusFunction)(QPoint, Unit*, const QString &));
private:
qint32 m_unitIdx{-1};
QVector<oxygine::spSprite> m_pUnitWaitSprites;
QVector<oxygine::spSprite> m_pUnitSprites;
/**
Expand Down
25 changes: 25 additions & 0 deletions resource_management/unitspritemanager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "resource_management/unitspritemanager.h"

#include "game/unit.h"
#include "game/player.h"

UnitSpriteManager::UnitSpriteManager()
: RessourceManagement<UnitSpriteManager>("/images/units/res.xml",
"/scripts/units")
Expand Down Expand Up @@ -117,3 +120,25 @@ void UnitSpriteManager::removeRessource(QString id)
}
}
}

void UnitSpriteManager::createBaseDamageTable()
{
auto pPlayer = MemoryManagement::create<Player>(nullptr);
pPlayer->init();
auto size = m_loadedRessources.size();
QVector<spUnit> matchups;
matchups.reserve(size);
for (const auto & unitId : m_loadedRessources)
{
spUnit pUnit = MemoryManagement::create<Unit>(unitId, pPlayer.get(), false, nullptr);
matchups.append(pUnit);
}
for (qint32 attacker = 0; attacker < size; ++attacker)
{
for (qint32 defender = 0; defender < size; ++defender)
{
float damage = matchups[attacker]->getBaseDamage(matchups[defender].get());
m_baseDamgeTable.insert_or_assign(static_cast<qint32>(size * attacker + defender), damage);
}
}
}
12 changes: 12 additions & 0 deletions resource_management/unitspritemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
#define UNITSPRITEMANAGER_H

#include "game/GameEnums.h"
#include "game/unit.h"

#include "resource_management/ressourcemanagement.h"

class UnitSpriteManager final : public QObject, public RessourceManagement<UnitSpriteManager>
{
Q_OBJECT
public:
/**
* @brief createBaseDamageTable
*/
void createBaseDamageTable();

qint32 getBaseDamage(Unit* pAttacker, Unit* pDefender)
{
return m_baseDamgeTable[pAttacker->getUnitIdx() * m_loadedRessources.size() + pDefender->getUnitIdx()];
}
/**
* @brief getUnitType
* @param i
Expand Down Expand Up @@ -53,6 +63,8 @@ public slots:
UnitSpriteManager();
private:
virtual ~UnitSpriteManager() = default;

std::map<qint32, float> m_baseDamgeTable;
};

Q_DECLARE_INTERFACE(UnitSpriteManager, "UnitSpriteManager");
Expand Down

0 comments on commit c560bfa

Please sign in to comment.