Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Robosturm committed Oct 12, 2023
1 parent 524d696 commit 8b9e997
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 15 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -729,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/heavyAiEnums.h
ai/heavyai/situationevaluator.h ai/heavyai/situationevaluator.cpp
ai/influencefrontmap.cpp ai/influencefrontmap.h
ai/dummyai.h ai/dummyai.cpp
Expand Down
15 changes: 15 additions & 0 deletions ai/coreai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ CoreAI::CoreAI(GameMap* pMap, GameEnums::AiTypes aiType, QString jsName)
{
m_flareInfo.unfogRange = erg.toNumber();
}


const QString funcGetDamage = "getDamage";
erg = pInterpreter->doFunction(ACTION_EXPLODE, funcGetDamage);
if (erg.isNumber())
{
m_explodeInfo.damage = erg.toNumber();
}
const QString funcGetRange = "getRange";
erg = pInterpreter->doFunction(ACTION_EXPLODE, funcGetRange);
if (erg.isNumber())
{
m_explodeInfo.range = erg.toNumber();
}

CONSOLE_PRINT("Creating core ai", GameConsole::eDEBUG);
}

Expand Down
6 changes: 6 additions & 0 deletions ai/coreai.h
Original file line number Diff line number Diff line change
Expand Up @@ -853,5 +853,11 @@ public slots:
qint32 unfogRange{0};
};
FlareInfo m_flareInfo;
struct ExplodeInfo
{
qint32 range{0};
qint32 damage{0};
};
ExplodeInfo m_explodeInfo;
QStringList m_iniFiles;
};
4 changes: 2 additions & 2 deletions ai/coreai_predefinedai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ bool CoreAI::moveBlackBombs(spQmlVectorUnit & pUnits, spQmlVectorUnit & pEnemyUn
{
UnitPathFindingSystem turnPfs(m_pMap, pUnit);
turnPfs.explore();
spQmlVectorPoint pPoints = GlobalUtils::getSpCircle(1, 3);
spQmlVectorPoint pPoints = GlobalUtils::getSpCircle(1, m_explodeInfo.range);
auto targets = turnPfs.getAllNodePointsFast();
qint32 maxDamage = 0;
std::vector<QPoint> bestTargets;
spGameAction pAction = MemoryManagement::create<GameAction>(ACTION_EXPLODE, m_pMap);
pAction->setTarget(QPoint(pUnit->Unit::getX(), pUnit->Unit::getY()));
for (auto & target : targets)
{
qint32 damageDone = m_pPlayer->getRocketTargetDamage(target.x(), target.y(), pPoints.get(), 5, 1.2f, GameEnums::RocketTarget_Money, true);
qint32 damageDone = m_pPlayer->getRocketTargetDamage(target.x(), target.y(), pPoints.get(), m_explodeInfo.damage, 1.2f, GameEnums::RocketTarget_Money, true);
if (damageDone > maxDamage)
{
bestTargets.clear();
Expand Down
9 changes: 9 additions & 0 deletions ai/heavyai/heavyAiEnums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

enum class AiCache
{
MovementPoints,
MinFirerange,
MaxFirerange,
Max,
};
101 changes: 101 additions & 0 deletions ai/heavyai/heavyai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "coreengine/globalutils.h"

#include "ai/heavyai/heavyai.h"
#include "ai/heavyai/heavyAiEnums.h"

#include "game/player.h"
#include "game/gameaction.h"
Expand All @@ -28,7 +29,107 @@ HeavyAi::HeavyAi(GameMap* pMap, QString type, GameEnums::AiTypes aiType)
CONSOLE_PRINT("Creating heavy ai", GameConsole::eDEBUG);
}

void HeavyAi::toggleAiPause()
{
m_pause = !m_pause;
}

void HeavyAi::showIslandMap(QString unitId)
{
Unit unit(unitId, m_pPlayer, false, m_pMap);
qint32 unitIslandIdx = getIslandIndex(&unit);
if (unitIslandIdx >= 0 && unitIslandIdx < m_IslandMaps.size())
{
m_IslandMaps[unitIslandIdx]->show();
}
}

void HeavyAi::hideIslandMap(QString unitId)
{
Unit unit(unitId, m_pPlayer, false, m_pMap);
qint32 unitIslandIdx = getIslandIndex(&unit);
if (unitIslandIdx >= 0 && unitIslandIdx < m_IslandMaps.size())
{
m_IslandMaps[unitIslandIdx]->hide();
}
}

void HeavyAi::process()
{
AI_CONSOLE_PRINT("NormalAi::process()", GameConsole::eDEBUG);
if (m_pause)
{
m_timer.start(1000);
return;
}
else
{
m_timer.stop();
}

spQmlVectorBuilding pBuildings = m_pPlayer->getSpBuildings();
pBuildings->randomize();
spQmlVectorUnit pUnits = m_pPlayer->getSpUnits();
spQmlVectorUnit pEnemyUnits = m_pPlayer->getSpEnemyUnits();
pEnemyUnits->pruneEnemies(pUnits.get(), m_enemyPruneRange);
qint32 cost = 0;
m_pPlayer->getSiloRockettarget(2, 3, cost);
m_missileTarget = (cost >= m_minSiloDamage);
updateUnitCache(pUnits);
updateUnitCache(pEnemyUnits);
if (useBuilding(pBuildings, pUnits))
{
clearUnitCache(pUnits);
clearUnitCache(pEnemyUnits);
}
else
{
m_IslandMaps.clear();
m_turnMode = GameEnums::AiTurnMode_EndOfDay;
if (useCOPower(pUnits, pEnemyUnits))
{
m_usedTransportSystem = false;
m_usedPredefinedAi = false;
m_turnMode = GameEnums::AiTurnMode_DuringDay;
}
else
{
m_turnMode = GameEnums::AiTurnMode_StartOfDay;
finishTurn();
}
}
}

void HeavyAi::updateUnitCache(spQmlVectorUnit & pUnits)
{
for(auto & pUnit : pUnits->getVector())
{
auto & cache = pUnit->getAiCache();
if (cache.size() != static_cast<qint32>(AiCache::Max))
{
QPoint pos = pUnit->getPosition();
cache.resize(static_cast<qint32>(AiCache::Max));
cache[static_cast<qint32>(AiCache::MovementPoints)] = pUnit->getMovementpoints(pos);
cache[static_cast<qint32>(AiCache::MinFirerange)] = pUnit->getMinRange(pos);
cache[static_cast<qint32>(AiCache::MaxFirerange)] = pUnit->getMaxRange(pos);
}
}
rebuildIsland(pUnits);
}

void HeavyAi::clearUnitCache(spQmlVectorUnit & pUnits)
{
for(auto & pUnit : pUnits->getVector())
{
pUnit->getAiCache().clear();
}
}

void HeavyAi::finishTurn()
{
auto pUnits = m_pPlayer->getSpUnits();
clearUnitCache(pUnits);
pUnits = m_pPlayer->getSpEnemyUnits();
clearUnitCache(pUnits);
CoreAI::finishTurn();
}
36 changes: 36 additions & 0 deletions ai/heavyai/heavyai.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,47 @@ class HeavyAi final : public CoreAI

explicit HeavyAi(GameMap* pMap, QString type, GameEnums::AiTypes aiType);
virtual ~HeavyAi() = default;
/*******************************************************************/
// debugging section
/*******************************************************************/
/**
* @brief toggleAiPause for debugging
*/
Q_INVOKABLE void toggleAiPause();
/**
* @brief showIslandMap
* @param unitId
*/
Q_INVOKABLE void showIslandMap(QString unitId);
/**
* @brief showIslandMap
* @param unitId
*/
Q_INVOKABLE void hideIslandMap(QString unitId);

public slots:
/**
* @brief process
*/
virtual void process() override;
protected:
/**
* @brief finishTurn
*/
virtual void finishTurn() override;
/**
* @brief clearUnitCache
*/
void clearUnitCache(spQmlVectorUnit & pUnits);
/**
* @brief updateUnitCache
* @param pUnits
*/
void updateUnitCache(spQmlVectorUnit & pUnits);
private:
double m_minSiloDamage{7000};
QTimer m_timer;
bool m_pause{false};
};

Q_DECLARE_INTERFACE(HeavyAi, "HeavyAi");
2 changes: 1 addition & 1 deletion ai/heavyai/situationevaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void SituationEvaluator::getUnitsInRange(GameMap* pMap, const QPoint & searchPoi
if (pTerrain != nullptr)
{
Unit* pUnit = pTerrain->getUnit();
if (pUnit != nullptr)
if (pUnit != nullptr && pUnit->getHpRounded() > 0)
{
if (m_pOwner->isEnemyUnit(pUnit))
{
Expand Down
3 changes: 1 addition & 2 deletions ai/normalai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ void NormalAi::process()
}
spQmlVectorBuilding pBuildings = m_pPlayer->getSpBuildings();
pBuildings->randomize();
spQmlVectorUnit pUnits;
pUnits = m_pPlayer->getSpUnits();
spQmlVectorUnit pUnits = m_pPlayer->getSpUnits();
spQmlVectorUnit pEnemyUnits;
spQmlVectorBuilding pEnemyBuildings;
qint32 cost = 0;
Expand Down
2 changes: 1 addition & 1 deletion ai/targetedunitpathfindingsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ TargetedUnitPathFindingSystem::TargetedUnitPathFindingSystem(GameMap* pMap, Unit
if (m_Targets.size() > maxTargets)
{
m_Targets.resize(maxTargets);
}
}
setMovepoints(m_pUnit->getFuel() * 2);
}

Expand Down
5 changes: 5 additions & 0 deletions game/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,11 @@ qint32 Unit::getCoBonus(QPoint position, const QString & function, qint32(Player
return bonus;
}

QVector<qint32> & Unit::getAiCache()
{
return m_aiCache;
}

GameMap *Unit::getMap() const
{
return m_pMap;
Expand Down
10 changes: 9 additions & 1 deletion game/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ class Unit final : public Tooltip, public FileSerializable, public JsThis
* @return
*/
bool isStatusStealthedAndInvisible(Player* pPlayer, bool & terrainHide);

/**
* @brief getAiCache
* @return
*/
QVector<qint32> & getAiCache();
// js invokable functions
/**
* @brief getX
Expand Down Expand Up @@ -1223,6 +1227,10 @@ class Unit final : public Tooltip, public FileSerializable, public JsThis
* @brief m_sortValues values sto
*/
QVector<qint32> m_sortValues;
/**
* @brief m_aiCache
*/
QVector<qint32> m_aiCache;
};

Q_DECLARE_INTERFACE(Unit, "Unit");
Expand Down
1 change: 0 additions & 1 deletion menue/gamemenue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,6 @@ void GameMenue::disconnected(quint64 socketID)
{
CONSOLE_PRINT("Handling player GameMenue::disconnect()", GameConsole::eDEBUG);
bool showDisconnect = !m_pNetworkInterface->getIsServer();
removeSyncSocket(socketID);
auto & observer = m_pMap->getGameRules()->getObserverList();
bool observerDisconnect = observer.contains(socketID);
if (observerDisconnect)
Expand Down
17 changes: 14 additions & 3 deletions resources/scripts/actions/ACTION_EXPLODE.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,23 @@ var Constructor = function()
ACTION_EXPLODE.postAnimationTargetX = action.readDataInt32();
ACTION_EXPLODE.postAnimationTargetY = action.readDataInt32();
};


this.getDamage = function()
{
return 4;
};
this.getRange = function()
{
return 2;
};

this.performPostAnimation = function(postAnimation, map)
{
var owner = ACTION_EXPLODE.postAnimationUnit.getOwner();
var x = ACTION_EXPLODE.postAnimationTargetX;
var y = ACTION_EXPLODE.postAnimationTargetY;
var fields = globals.getCircle(1, 2);
var fields = globals.getCircle(1, ACTION_EXPLODE.getRange());
var size = fields.size();
for (var i = 0; i < size; i++)
{
Expand All @@ -85,7 +96,7 @@ var Constructor = function()
var unit = terrain.getUnit();
if (unit !== null)
{
unit.setHp(unit.getHpRounded() - 4);
unit.setHp(unit.getHpRounded() - ACTION_EXPLODE.getDamage());
if (unit.getHp() <= 0)
{
// we destroyed a unit
Expand All @@ -95,7 +106,7 @@ var Constructor = function()
}
}
}
var animation = GameAnimationFactory.createAnimation(map, x - 2, y - 3);
var animation = GameAnimationFactory.createAnimation(map, x - 2, y - 2);
animation.addSprite("explosion+black_bomb", map.getImageSize() / 2, map.getImageSize() / 2, 0, 1.875);
animation.setSound("explosion+land.wav");
// we destroyed a unit
Expand Down
7 changes: 3 additions & 4 deletions resources/scripts/actions/ACTION_FLARE.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ var Constructor = function()
this.getMinRange = function()
{
return 1;
}

};
this.getMaxRange = function()
{
return 5;
}
};
this.getUnfogRange = function()
{
return 2;
}
};

this.getStepData = function(action, data, map)
{
Expand Down

0 comments on commit 8b9e997

Please sign in to comment.