Skip to content

Commit

Permalink
Merge pull request #78 from ntut-Tu/AI_Update
Browse files Browse the repository at this point in the history
Ai update
  • Loading branch information
jonylu7 authored May 24, 2024
2 parents f595301 + e838103 commit dbd88e1
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 136 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ set(SRC_FILES

${SRC_DIR}/Enemy/Enemy.cpp

${SRC_DIR}/AI/EnemyScripts.cpp

${SRC_DIR}/Structure/Structure.cpp
${SRC_DIR}/Structure/WayPoint.cpp
${SRC_DIR}/Structure/HighLight.cpp
Expand All @@ -99,7 +101,7 @@ set(SRC_FILES

${SRC_DIR}/Scene/MapScene.cpp
${SRC_DIR}/Scene/TutorialScene.cpp

${SRC_DIR}/Scene/SandboxScene.cpp

${SRC_DIR}/Avatar/Runner.cpp
${SRC_DIR}/Avatar/Avatar.cpp
Expand Down Expand Up @@ -220,6 +222,8 @@ set(INCLUDE_FILES

${INCLUDE_DIR}/Scene/Scene.hpp
${INCLUDE_DIR}/Scene/TutorialScene.hpp
${INCLUDE_DIR}/Scene/SandboxScene.hpp


${INCLUDE_DIR}/Enemy/Enemy.hpp
${INCLUDE_DIR}/Enemy/EnemyScripts.hpp
Expand Down
4 changes: 2 additions & 2 deletions include/AI/Enemy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_ENEMY_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_ENEMY_HPP

#include "Mechanics/GameObjectID.hpp"
//#include "Mechanics/GameObjectID.hpp"
#include "Mechanics/Player.hpp"
enum class EnemyMode{
DEFAULT,
Expand Down Expand Up @@ -34,7 +34,7 @@ class EnemyPlayer:public Player{
int getAvatarCount(){
return unitCount[UnitType::INFANTRY];
}
void setAvatarCount(UnitType type,int value){
void addAvatarCount(UnitType type,int value){
unitCount[type] += value;
}
protected:
Expand Down
116 changes: 43 additions & 73 deletions include/AI/EnemyScripts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,94 +8,64 @@
#include "Mechanics/UnitManager.hpp"

#define SPACE 4
class EnemyScripts {


enum class SpawnMode { BUILDINGS,AVATAR};
class EnemyScripts{

private:
std::shared_ptr<EnemyPlayer> m_Enemy;
std::shared_ptr<UnitManager> m_GameObjectManager;
std::shared_ptr<UnitManager> m_EnemyObjectManager;
std::shared_ptr<MapClass> m_Map;

glm::vec2 m_baseCell = {20, 20};
int constructCountX = 0;
int constructCountY = 0;

float CDTime = 0;
std::chrono::high_resolution_clock::time_point m_StartTime;
double m_lastElapsed = 0.F;

float m_buildingCDTime=0;
float m_AvatarCDTime=0;
float m_buildingCost = 0.F;
float m_avatarCost = 0.F;

UnitType m_selectedBuildingType = UnitType::NONE;
UnitType m_selectedAvatarType = UnitType::NONE;

float m_buildDeltaTime = 0;
float m_avatarDeltaTime = 0;
float m_mainDeltaTime = 0;
Util::Time m_Time;

bool m_active;
public:
EnemyScripts(){};
~EnemyScripts(){};
void Start(std::shared_ptr<EnemyPlayer> enemyPlayer,
std::shared_ptr<UnitManager> GameObjectManager,
std::shared_ptr<MapClass> map) {
m_StartTime = std::chrono::high_resolution_clock::now();
m_Enemy = enemyPlayer;
m_GameObjectManager = GameObjectManager;
m_Map = map;
void Start(std::shared_ptr<UnitManager> GameObjectManager,std::shared_ptr<UnitManager> EnemyObjectManager,std::shared_ptr<MapClass> map,bool active = true);
void Update();
void modeUpdate();
void offensiveUpdate(){
// if(m_EnemyObjectManager->getOffensiveTroopSize()>0){
// m_EnemyObjectManager->setOffensiveTroopAttack(m_GameObjectManager->getMostValuableTarget());
// }
}
void Update() {

std::chrono::high_resolution_clock::time_point m_currentTime =
std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = m_currentTime - m_StartTime;
if (elapsed.count() - m_lastElapsed >= CDTime) { // update when CD over
m_lastElapsed = elapsed.count();
UpdateSpawnScript();
}
void updateAllTroopStates(){
// m_EnemyObjectManager->setAllTroopToAttackMode();
// m_EnemyObjectManager->setDefensiveTroopSize(0);
}
void UpdateSpawnScript() {
if (m_Enemy->getUnitConstructCount(UnitType::POWER_PLANT) < 1 &&
m_Enemy->getTotalCurrency() > 300) {
m_GameObjectManager->spawn(m_Map, UnitType::POWER_PLANT,
HouseType::ENEMY,
{m_baseCell.x + constructCountX,
m_baseCell.y + constructCountY});
constructCountX += SPACE;
CDTime = 15.f * CHEAT;
} else if (m_Enemy->getUnitConstructCount(UnitType::ORE_REF) < 1 &&
m_Enemy->getTotalCurrency() > 2000) {
m_GameObjectManager->spawn(m_Map, UnitType::ORE_REF,
HouseType::ENEMY,
{m_baseCell.x + constructCountX,
m_baseCell.y + constructCountY});
constructCountX += SPACE;
CDTime = 100.f * CHEAT;
} else if (m_Enemy->getUnitConstructCount(UnitType::BARRACKS) < 1 &&
m_Enemy->getTotalCurrency() > 300) {
m_GameObjectManager->spawn(m_Map, UnitType::BARRACKS,
HouseType::ENEMY,
{m_baseCell.x + constructCountX,
m_baseCell.y + constructCountY});
constructCountX += SPACE;
CDTime = 15.f * CHEAT;
} else if (m_Enemy->getAvatarCount() <= 25 &&
m_Enemy->getTotalCurrency() > 100) {
m_GameObjectManager->spawnToWayPoint(m_Map, UnitType::INFANTRY,
HouseType::ENEMY);
CDTime = 5.f * CHEAT;
} else if (m_Enemy->getUnitConstructCount(UnitType::WAR_FACT) < 1 &&
m_Enemy->getTotalCurrency() > 2000) {
m_GameObjectManager->spawn(m_Map, UnitType::WAR_FACT,
HouseType::ENEMY,
{m_baseCell.x + constructCountX,
m_baseCell.y + constructCountY});
constructCountX += SPACE;
CDTime = 100.f * CHEAT;
} else if (m_Enemy->getUnitConstructCount(UnitType::ADV_POWER_PLANT) <
1 &&
m_Enemy->getTotalCurrency() > 500) {
m_GameObjectManager->spawn(m_Map, UnitType::ADV_POWER_PLANT,
HouseType::ENEMY,
{m_baseCell.x + constructCountX,
m_baseCell.y + constructCountY});
constructCountX += SPACE;
CDTime = 25.f * CHEAT;
}

if (constructCountX >= 15) {
constructCountY += SPACE;
constructCountX = 0;
}
void setCDTime(float time,SpawnMode spawnMode,bool cheat = true);
void setCost(float cost,SpawnMode spawnMode);

bool ifBuiltBasic(){
return m_EnemyObjectManager->getUnitConstructCount(UnitType::POWER_PLANT)>=1 && m_EnemyObjectManager->getUnitConstructCount(UnitType::ORE_REF)>=1 && m_EnemyObjectManager->getUnitConstructCount(UnitType::BARRACKS)>=1 ;
}
bool ifBuiltADV(){
return m_EnemyObjectManager->getUnitConstructCount(UnitType::WAR_FACT)>=1 && m_EnemyObjectManager->getUnitConstructCount(UnitType::ADV_POWER_PLANT)>=1 ;
}

void buildBasic();
void buildADV();
void spawnUnit();
void UpdateSpawnScript(SpawnMode spawnMode);
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_ENEMYSCRIPTS_HPP
25 changes: 10 additions & 15 deletions include/Mechanics/BuiltStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,20 @@ class BuiltStructure {
m_BuiltStructure.push_back(structure);
}

void updateAvatarSpawnLocation(
std::vector<std::shared_ptr<Structure>> structure) {
for (auto i : structure) {
void updateAvatarSpawnLocation() {
for (auto i : m_BuiltStructure) {
if (std::dynamic_pointer_cast<Barracks>(i)) {
if (i->getHouseType() == HouseType::ENEMY) {
m_EnemyBarrackCell = i->GetObjectLocation();
m_EnemyWayPointCell = std::dynamic_pointer_cast<Barracks>(i)
->GetWayPointLocation();
} else {
m_PlayerBarrackCell = i->GetObjectLocation();
m_PlayerWayPointCell =
std::dynamic_pointer_cast<Barracks>(i)
->GetWayPointLocation();
}
m_PlayerBarrackCell = MapUtil::GlobalCoordToCellCoord(i->GetObjectLocation());
m_PlayerWayPointCell =MapUtil::GlobalCoordToCellCoord(
std::dynamic_pointer_cast<Barracks>(i)
->GetWayPointLocation());
}
}
}

glm::vec2 getPlayerBarrackCell() { return {m_PlayerBarrackCell.x-1,m_PlayerBarrackCell.y-1}; }
glm::vec2 getPlayerWayPointCell() { return m_PlayerWayPointCell; }

public:
glm::vec2 getEnemyBarrackCell() { return m_EnemyBarrackCell; }
glm::vec2 getEnemyWayPointCell() { return m_EnemyWayPointCell; }
Expand Down Expand Up @@ -126,11 +122,10 @@ class BuiltStructure {
return false;
}


private:
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
std::vector<std::shared_ptr<Structure>> m_BuiltStructure;
glm::vec2 m_EnemyBarrackCell = {-1, -1};
glm::vec2 m_EnemyWayPointCell = {-1, -1};
glm::vec2 m_PlayerBarrackCell = {-1, -1};
glm::vec2 m_PlayerWayPointCell = {-1, -1};
};
Expand Down
4 changes: 2 additions & 2 deletions include/Mechanics/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Player {
Player() {}
~Player() {}
void setTotalCurrency(int value) { m_TotalCurrency = value; }
void addTotalCurrency(int value) { m_TotalCurrency += value; };
void addTotalCurrency(float value) { m_TotalCurrency += value; };

void setFixedPower(int value) { m_FixedPower = value; }
void addFixedPower(int value) { m_FixedPower += value; }
Expand All @@ -33,6 +33,6 @@ class Player {
protected:
int m_MaxTroopSize = 200;
int m_FixedPower = 0;
int m_TotalCurrency = 0;
float m_TotalCurrency = 0;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_PLAYER_HPP
63 changes: 22 additions & 41 deletions include/Mechanics/UnitManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class UnitManager : public Player {
switch (unit) {
case UnitType::INFANTRY: {
auto avatar = std::make_shared<Infantry>(house);

m_StructureManager->getStructureArray()->updateAvatarSpawnLocation();
if (m_StructureManager->getStructureArray()
->getPlayerBarrackCell()
.x == -1) {
Expand All @@ -88,11 +88,13 @@ class UnitManager : public Player {
avatar->Start(m_StructureManager->getStructureArray()
->getPlayerBarrackCell());

m_AvatarManager->assignMoveOrderToAvatar(avatar,{m_StructureManager->getStructureArray()->getPlayerWayPointCell()});
// assign order
m_AvatarManager->assignMoveOrderToAvatar(
avatar, m_StructureManager->getStructureArray()
->getPlayerWayPointCell());
m_AvatarManager->AppendAvatar(avatar);
m_troopSize+=1;
}

default: {
Expand All @@ -109,6 +111,7 @@ class UnitManager : public Player {
auto structure = std::make_shared<Barracks>(house);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
structure->SetWayPointLocationByCellCoord({cellPos.x+2,cellPos.y-2});
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
Expand All @@ -133,6 +136,7 @@ class UnitManager : public Player {
auto structure = std::make_shared<WarFactory>(house);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
structure->SetWayPointLocationByCellCoord({cellPos.x+2,cellPos.y-2});
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
Expand All @@ -149,7 +153,9 @@ class UnitManager : public Player {
auto avatar = std::make_shared<Infantry>(house);
avatar->Start(cellPos);
// avatar ->setNewDestination(cellPos);
m_AvatarManager->assignMoveOrderToAvatar(avatar,{cellPos.x+1,cellPos.y+1});
m_AvatarManager->AppendAvatar(avatar);
m_troopSize+=1;
break;
}
case UnitType::NONE: {
Expand All @@ -163,47 +169,21 @@ class UnitManager : public Player {
}
}

// bool ifClosestEnemyInRange(glm::vec2 cell,HouseType myHouse,int
// range){
// float closestDistance=500.f;
// if(myHouse==HouseType::MY){
// for(auto i:m_EnemyUnitArray){
// if(MapUtil::findDistance(cell,i->getCurrentCell())<closestDistance){
// closestDistance=MapUtil::findDistance(cell,i->getCurrentCell());
// }
// }
// }else{
// for(auto i:m_PlayerUnitArray){
// if(MapUtil::findDistance(cell,i->getCurrentCell())<closestDistance){
// closestDistance=MapUtil::findDistance(cell,i->getCurrentCell());
// }
// }
// }
// return closestDistance<=range;
// }
//
// std::shared_ptr<AttackAndDamage> findInRangeEnemy(glm::vec2
// cell,HouseType myHouse,int range){
// float closestDistance=500.f;
// std::shared_ptr<AttackAndDamage> ansUnit;
// if(myHouse==HouseType::MY){
// for(auto i:m_EnemyUnitArray){
// if(MapUtil::findDistance(cell,i->getCurrentCell())<closestDistance){
// closestDistance=MapUtil::findDistance(cell,i->getCurrentCell());
// ansUnit = i;
// }
// }
// }else{
// for(auto i:m_PlayerUnitArray){
// if(MapUtil::findDistance(cell,i->getCurrentCell())<closestDistance){
// closestDistance=MapUtil::findDistance(cell,i->getCurrentCell());
// ansUnit = i;
// }
// }
// }
// return ansUnit;
// }
void addUnitConstructCount(UnitType type, int value) {
unitCount[type] += value;
}
int getUnitConstructCount(UnitType type){
return unitCount[type];
}
int getAvatarCount(){
return unitCount[UnitType::INFANTRY];
}
void addAvatarCount(UnitType type,int value){
unitCount[type] += value;
}

private:
std::unordered_map<UnitType, unsigned int> unitCount;
std::shared_ptr<CursorSelection> m_CursorSelection =
std::make_shared<CursorSelection>();
std::shared_ptr<StructureManager> m_StructureManager =
Expand All @@ -213,6 +193,7 @@ class UnitManager : public Player {
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
std::chrono::high_resolution_clock::time_point m_StartTime;
double m_lastElapsed = 0.F;
int m_troopSize = 0;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UNITMANAGER_HPP
Loading

0 comments on commit dbd88e1

Please sign in to comment.