|
| 1 | +// Copyright (C) Team13. All rights reserved. |
| 2 | + |
| 3 | +#include "ETVAI.h" |
| 4 | +#include "ETVGameModeBase.h" |
| 5 | +#include "ETVActionTarget_Fire.h" |
| 6 | +#include "ETVActionTarget_Move.h" |
| 7 | + |
| 8 | +// TODO Returns duplicate objects of all the ships on the board for AI to calculate the next move |
| 9 | +TArray<AETVShip*> GetBoardStateClone(TArray<AETVShip*> BoardState) |
| 10 | +{ |
| 11 | + TArray<AETVShip*> Clones; |
| 12 | + for(auto ShipRef : BoardState) |
| 13 | + { |
| 14 | + // Check for class? |
| 15 | + |
| 16 | + // Cast and create clone |
| 17 | + |
| 18 | + // Get clone's reference |
| 19 | + |
| 20 | + // Push reference to clones |
| 21 | + |
| 22 | + } |
| 23 | + return Clones; |
| 24 | +} |
| 25 | + |
| 26 | +// TODO Calculates maximum score and the coresponding board state if the given ship makes a move |
| 27 | +TArray<AETVShip*> GetNextBoardStateAndScore(TArray<AETVShip*> BoardState, int32 ShipIndex, TArray<TArray<int32>> &MoveInstructions) |
| 28 | +{ |
| 29 | + return TArray<AETVShip*>(); |
| 30 | +} |
| 31 | + |
| 32 | +// TODO Returns the 5 best moves for the given boardState |
| 33 | +TArray<TArray<AETVShip*>> GetTop5Moves(TArray<AETVShip*> BoardState, TArray<TArray<int32>> &MoveInstructions) |
| 34 | +{ |
| 35 | + return TArray<TArray<AETVShip*>>(); |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +UETVAI::UETVAI() |
| 40 | +{ |
| 41 | + |
| 42 | +} |
| 43 | +// TODO Better Implementation |
| 44 | +TArray<int32> UETVAI::GetMove(TArray<AETVShip*> Ships) |
| 45 | +{ |
| 46 | + TArray<int32> MoveInstructions; |
| 47 | + |
| 48 | + // Get gamemode |
| 49 | + AETVGameModeBase* GameMode = Cast<AETVGameModeBase>(Ships[0]->GetWorld()->GetAuthGameMode()); |
| 50 | + |
| 51 | + int32 MostImportantPlayerShip = -1; |
| 52 | + int32 MostImportantAIShip = -1; |
| 53 | + int32 RandomAIShip = -1; |
| 54 | + // Find most important ships |
| 55 | + int index = 0; |
| 56 | + for (auto ShipReference : Ships) |
| 57 | + { |
| 58 | + // AI Ship |
| 59 | + if (ShipReference->IsEnemy()) |
| 60 | + { |
| 61 | + if (MostImportantAIShip != -1) |
| 62 | + { |
| 63 | + // This will always be the capital |
| 64 | + if (ShipReference->GetScore() > Ships[MostImportantAIShip]->GetScore()) |
| 65 | + { |
| 66 | + MostImportantAIShip = index; |
| 67 | + } |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + MostImportantAIShip = index; |
| 72 | + RandomAIShip = index; |
| 73 | + } |
| 74 | + // 1/8 chance |
| 75 | + if (FMath::RandBool() && FMath::RandBool() && FMath::RandBool()) |
| 76 | + { |
| 77 | + RandomAIShip = index; |
| 78 | + } |
| 79 | + } |
| 80 | + else if (GameMode->IsTileVisible(ShipReference->GetTilePosition(), EETVShipType::EnemyShip) == true) |
| 81 | + { |
| 82 | + if (MostImportantPlayerShip != -1) |
| 83 | + { |
| 84 | + if (ShipReference->GetScore() > Ships[MostImportantPlayerShip]->GetScore()) |
| 85 | + { |
| 86 | + MostImportantPlayerShip = index; |
| 87 | + } |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + MostImportantPlayerShip = index; |
| 92 | + } |
| 93 | + } |
| 94 | + ++index; |
| 95 | + } |
| 96 | + // Does AI see enemy ship |
| 97 | + if (MostImportantPlayerShip != -1) |
| 98 | + { |
| 99 | + int jndex = 0; |
| 100 | + // If possible try and attack |
| 101 | + for (auto ShipReference : Ships) |
| 102 | + { |
| 103 | + if (ShipReference->IsEnemy()) |
| 104 | + { |
| 105 | + // Currently fire laser is always 0 and torpedo is 1 on fighters |
| 106 | + int ActionIndex = 0; |
| 107 | + if(ShipReference->GetShipClass() == "Fighter" || ShipReference->GetShipClass() == "Capital") |
| 108 | + ActionIndex = 1; |
| 109 | + UETVActionTarget_Fire* Laser = Cast<UETVActionTarget_Fire>(ShipReference->GetActions()[ActionIndex]); |
| 110 | + // TODO - Add check if torped avaliable |
| 111 | + Laser->SetTarget(Ships[MostImportantPlayerShip], Ships[MostImportantPlayerShip]->GetX(), Ships[MostImportantPlayerShip]->GetY()); |
| 112 | + if (Laser->CanPerform()) |
| 113 | + { |
| 114 | + MoveInstructions.SetNum(4); |
| 115 | + MoveInstructions[0] = (Ships[MostImportantPlayerShip]->GetScore()); |
| 116 | + MoveInstructions[1] = (jndex); |
| 117 | + MoveInstructions[2] = (ActionIndex); |
| 118 | + MoveInstructions[3] = (MostImportantPlayerShip); |
| 119 | + return MoveInstructions; |
| 120 | + } |
| 121 | + } |
| 122 | + ++jndex; |
| 123 | + } |
| 124 | + } |
| 125 | + // We we didn't find enemy ship or we can't attack it |
| 126 | + // We move a ship randomly |
| 127 | + // TODO Add checker if we can heal most important ship |
| 128 | + UETVActionTarget_Move* MoveAction = Cast<UETVActionTarget_Move>(Ships[RandomAIShip]->GetActions().Last(0)); |
| 129 | + int x = -1; |
| 130 | + int y = -1; |
| 131 | + while (!MoveAction->CanPerform()) |
| 132 | + { |
| 133 | + x = Ships[RandomAIShip]->GetTilePosition().X + FMath::RandRange(1, Ships[RandomAIShip]->GetMoveRange()); |
| 134 | + y = Ships[RandomAIShip]->GetTilePosition().Y + FMath::RandRange(1, Ships[RandomAIShip]->GetMoveRange()); |
| 135 | + MoveAction->SetTarget(GameMode->GetTileMapActor(), x, y); |
| 136 | + } |
| 137 | + MoveInstructions.SetNum(5); |
| 138 | + MoveInstructions[0] = (Ships[RandomAIShip]->GetScore()); |
| 139 | + MoveInstructions[1] = (RandomAIShip); |
| 140 | + MoveInstructions[2] = (Ships[RandomAIShip]->GetActions().Num()-1); |
| 141 | + MoveInstructions[3] = (x); |
| 142 | + MoveInstructions[4] = (y); |
| 143 | + return MoveInstructions; |
| 144 | +}; |
0 commit comments